20 #ifndef GAIA_FACTORY_H 21 #define GAIA_FACTORY_H 38 template <
typename IdentifierType,
39 typename AbstractProduct,
40 typename ArgumentType >
44 typedef AbstractProduct* (*ProductCreator)(
const ArgumentType&);
45 typedef QMap<IdentifierType, ProductCreator> CreatorMap;
56 static AbstractProduct* create(
const IdentifierType&
id,
57 const ArgumentType& arg = ArgumentType()) {
58 return instance().create_i(
id, arg);
61 AbstractProduct* create_i(
const IdentifierType&
id,
const ArgumentType& arg)
const {
63 IdentifierType lid =
id.toLower();
64 typename CreatorMap::const_iterator it = _map.constBegin();
65 while (it != _map.constEnd() && it.key().toLower() != lid) {
68 if (it == _map.constEnd()) {
70 msg <<
"Identifier '" <<
id <<
"' not found in registry...\n";
71 msg <<
"Available keys: " << keys().join(
" ");
78 static QStringList keys() {
return instance()._map.keys(); }
80 template <
typename ConcreteProduct>
84 Factory::instance()._map.insert(
id, &create);
87 static AbstractProduct* create(
const ArgumentType& arg) {
88 return new ConcreteProduct(arg);
95 template <
typename IdentifierType,
96 typename AbstractProduct,
97 typename ArgumentType >
107 static AbstractProduct* create(
const IdentifierType&
id,
108 const ArgumentType& arg = ArgumentType()) {
110 AbstractProduct* result;
112 result = BaseFactory::instance().create_i(
id, arg);
114 catch (FixedLengthException& e) {
115 e.msg() = e.msg().arg(
id);
120 msg <<
"Could not instantiate " <<
id <<
" analyzer because: " << e.msg();
127 foreach (
const QString& p, arg.keys()) {
128 if (!result->validParams.contains(p)) {
130 msg <<
"Could not create algorithm " <<
id <<
" because parameter " << p
131 <<
" is not a valid parameter.\n" 132 <<
"Valid parameters are: " << result->validParams.join(
", ");
147 #endif // GAIA_FACTORY_H A basic implementation of a generic factory class.
Definition: factory.h:41
Main Gaia namespace, which contains all the library functions.
Definition: addfield.cpp:22
Exception class that can take up to 3 arguments of any type, which will be serialized into a QString ...
Definition: gaiaexception.h:46