1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
/*
* (C) Copyright 1996- ECMWF.
*
* This software is licensed under the terms of the Apache Licence Version 2.0
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation nor
* does it submit to any jurisdiction.
*/
#include <algorithm>
#include <cassert>
#include <exception>
#include <iostream>
#include <map>
#include "eckit/exception/Exceptions.h"
#include "TestFactory.h"
struct TestFactory::PImpl {
typedef std::map<std::string, TestBuilder*> Store;
Store register_;
~PImpl() {
for (Store::iterator it = register_.begin(); it != register_.end(); ++it) {
delete it->second;
}
}
void regist(const std::string& name, TestBuilder* builder) {
std::cout << "registering builder [" << name << "]" << std::endl;
Store::iterator itr = register_.find(name);
if (itr != register_.end()) {
throw std::bad_alloc();
}
register_[name] = builder;
}
TestBuilder& get(const std::string& name) {
std::cout << "get builder [" << name << "]" << std::endl;
Store::iterator itr = register_.find(name);
if (itr == register_.end()) {
throw std::bad_alloc();
}
ASSERT(itr->second);
return *(itr->second);
}
};
//----------------------------------------------------------------------------------------------------------------------
TestFactory::TestFactory() : pimpl_(new TestFactory::PImpl()) {}
TestFactory& TestFactory::instance() {
// autolock mutex
static TestFactory factory;
return factory;
}
void TestFactory::regist(const std::string& name, TestBuilder* builder) {
pimpl_->regist(name, builder);
}
TestBuilder& TestFactory::get(const std::string& name) {
return pimpl_->get(name);
}
|