2 #ifndef GRAMODS_CORE_OFACTORYNODE
3 #define GRAMODS_CORE_OFACTORYNODE
5 #include <gmCore/config.hh>
6 #include <gmCore/Object.hh>
7 #include <gmCore/Stringify.hh>
8 #include <gmCore/InvalidArgument.hh>
20 BEGIN_NAMESPACE_GMCORE;
30 #define GM_OFI_DECLARE \
31 static gramods::gmCore::OFactory::OFactoryInformation _gm_ofi;
42 #define GM_OFI_DEFINE(NAME) \
43 gramods::gmCore::OFactory::OFactoryInformation \
44 NAME::_gm_ofi(#NAME, new gramods::gmCore::OFactory::ObjectCreator<NAME>());
54 #define GM_OFI_DEFINE_ABSTRACT(NAME) \
55 gramods::gmCore::OFactory::OFactoryInformation \
56 NAME::_gm_ofi(#NAME, nullptr);
69 #define GM_OFI_DEFINE_SUB(NAME, BASE) \
70 gramods::gmCore::OFactory::OFactoryInformation \
71 NAME::_gm_ofi(#NAME, new gramods::gmCore::OFactory::ObjectCreator<NAME>(), &BASE::_gm_ofi);
83 #define GM_OFI_DEFINE_ABSTRACT_SUB(NAME, BASE) \
84 gramods::gmCore::OFactory::OFactoryInformation \
85 NAME::_gm_ofi(#NAME, nullptr, &BASE::_gm_ofi);
106 #define GM_OFI_PARAM(CLASS, NAME, TYPE, FUNC) \
107 gramods::gmCore::OFactory::ParamSetterInsert gm_ofi_##CLASS##_param_##NAME( \
110 new gramods::gmCore::OFactory::ParamSetter<CLASS, TYPE>(&FUNC));
131 #define GM_OFI_PARAM2(CLASS, NAME, TYPE, FUNC) \
132 gramods::gmCore::OFactory::ParamSetterInsert gm_ofi_##CLASS##_param_##NAME( \
135 new gramods::gmCore::OFactory::ParamSetter<CLASS, TYPE>(&CLASS::FUNC));
156 #define GM_OFI_POINTER(CLASS, NAME, TYPE, FUNC) \
157 gramods::gmCore::OFactory::PointerSetterInsert gm_ofi_##CLASS##_pointer_##NAME \
158 (&CLASS::_gm_ofi, #NAME, \
159 new gramods::gmCore::OFactory::PointerSetter<CLASS, TYPE>(&FUNC));
180 #define GM_OFI_POINTER2(CLASS, NAME, TYPE, FUNC) \
181 gramods::gmCore::OFactory::PointerSetterInsert gm_ofi_##CLASS##_pointer_##NAME \
182 (&CLASS::_gm_ofi, #NAME, \
183 new gramods::gmCore::OFactory::PointerSetter<CLASS, TYPE>(&CLASS::FUNC));
210 struct ParamSetterBase {
211 virtual void setValueFromString(
Object *n, std::string s)
const = 0;
219 struct PointerSetterBase {
220 virtual void setPointer(
Object *n, std::shared_ptr<Object> o)
const = 0;
226 struct ObjectCreatorBase {
229 virtual Object * create()
const = 0;
249 ObjectCreatorBase *creator,
266 void registerParamSetter(std::string name, ParamSetterBase *setter);
271 void registerPointerSetter(std::string name, PointerSetterBase *setter);
280 bool setParamValueFromString(
Object *node, std::string name, std::string value)
const;
288 bool setPointerValue(
Object *node, std::string name, std::shared_ptr<Object> ptr)
const;
292 const std::string name;
293 const std::unique_ptr<const ObjectCreatorBase> creator;
296 std::map<std::string, std::unique_ptr<ParamSetterBase>> param_setters;
297 std::map<std::string, std::unique_ptr<PointerSetterBase>> pointer_setters;
312 ParamSetterBase *setter) {
329 PointerSetterBase *setter) {
342 template<
class Node,
class T>
348 void setValueFromString(
Object *n, std::string s)
const;
350 void (Node::*method)(T val);
363 void setValueFromString(
Object *n, std::string s)
const;
365 void (Node::*method)(std::string val);
373 struct ParamSetter<Node, std::filesystem::path> : ParamSetterBase {
375 ParamSetter(
void (Node::*m)(std::filesystem::path val))
378 void setValueFromString(
Object *n, std::string s)
const;
380 void (Node::*method)(std::filesystem::path val);
393 void setValueFromString(
Object *n, std::string s)
const;
395 void (Node::*method)(
bool val);
402 template<
class Node,
class T>
408 void setPointer(
Object *n, std::shared_ptr<Object> ptr)
const;
410 void (Node::*method)(std::shared_ptr<T> ptr);
425 static Object * createObject(std::string name);
429 static std::map<std::string, OFactoryInformation *> &getOFIByNameMap();
431 static void registerOFI(std::string name, OFactoryInformation *info);
432 static void unregisterOFI(std::string name);
433 static OFactoryInformation* getOFI(std::string name);
436 friend struct OFactoryInformation;
442 template<
class Node,
class T>
443 void OFactory::ParamSetter<Node, T>::setValueFromString
444 (Object *n, std::string s)
const {
445 assert(
dynamic_cast<Node*
>(n) !=
nullptr);
446 Node *node =
static_cast<Node*
>(n);
447 std::stringstream ss(s);
449 ss >> std::setbase(0) >> val;
454 (node->*method)(val);
458 void OFactory::ParamSetter<Node, std::string>::setValueFromString
459 (Object *n, std::string s)
const {
460 assert(
dynamic_cast<Node*
>(n) !=
nullptr);
461 Node *node =
static_cast<Node*
>(n);
467 void OFactory::ParamSetter<Node, std::filesystem::path>::setValueFromString
468 (Object *n, std::string s)
const {
469 assert(
dynamic_cast<Node*
>(n) !=
nullptr);
470 Node *node =
static_cast<Node*
>(n);
476 void OFactory::ParamSetter<Node, bool>::setValueFromString
477 (Object *n, std::string s)
const {
478 assert(
dynamic_cast<Node*
>(n) !=
nullptr);
479 Node *node =
static_cast<Node*
>(n);
488 (node->*method)(
true);
489 }
else if (s ==
"false" ||
496 (node->*method)(
false);
498 std::stringstream ss;
499 ss <<
"Cannot convert " << s <<
" to boolean";
500 throw gmCore::InvalidArgument(ss.str());
504 template<
class Node,
class T>
505 void OFactory::PointerSetter<Node, T>::setPointer
506 (Object *n, std::shared_ptr<Object> ptr)
const {
507 assert(
dynamic_cast<Node*
>(n) !=
nullptr);
508 Node *node =
static_cast<Node*
>(n);
509 std::shared_ptr<T> _ptr = std::dynamic_pointer_cast<T>(ptr);
512 throw gmCore::InvalidArgument(GM_STR(
"cannot cast " <<
typeid(ptr).name() <<
" to type " <<
typeid(T).name()));
514 (node->*method)(_ptr);
517 END_NAMESPACE_GMCORE;
A wrapper for the XML parser that also creates and configures the system objects and holds temporary ...
Definition: Configuration.hh:41
This is an object factory for classes with Object as base type, instantiating them by name and callin...
Definition: OFactory.hh:203
Base type for objects in the Gramods package for standardized handling of construction,...
Definition: Object.hh:42
Standard exception for invalid arguments in a call to a function or object.
Definition: InvalidArgument.hh:15
Actual object creator, templated for the type to instantiate.
Definition: OFactory.hh:417
Object * create() const
Creates and returns an instance of the template argument class.
Definition: OFactory.hh:420
Convenience class that registers a parameter setter upon instantiation.
Definition: OFactory.hh:304
ParamSetterInsert(OFactoryInformation *ofi, std::string name, ParamSetterBase *setter)
Constructor registering the specified parameter setter with the specified OFI, associating it with th...
Definition: OFactory.hh:310
General parameter setter, templated to determine the type to set value for.
Definition: OFactory.hh:343
Convenience class that registers a pointer setter upon instantiation.
Definition: OFactory.hh:321
PointerSetterInsert(OFactoryInformation *ofi, std::string name, PointerSetterBase *setter)
Constructor registering the specified pointer setter with the specified OFI, associating it with the ...
Definition: OFactory.hh:327
General pointer setter, templated to determine the type of the pointer.
Definition: OFactory.hh:403