30 #ifndef _GranOO_libCore_ObjectFactory_hpp_
31 #define _GranOO_libCore_ObjectFactory_hpp_
43 #define GRANOO_OBJECT_FACTORY(CLASS) \
44 static const bool register_object_factory_; \
45 static CLASS& glob(const std::string& id) \
46 {return Core::XmlObjectManager<CLASS>::get(id);} \
47 virtual void add_glob(const std::string& id) \
48 {Core::XmlObjectManager<CLASS>::add_item(id, this);}
50 #define GRANOO_OBJECT_FACTORY_REGISTER(BASE, CLASS) \
51 const bool CLASS::register_object_factory_ = \
52 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS>::record(CLASS::class_ID(), __FILE__);
54 #define GRANOO_OBJECT_FACTORY_REGISTER_DESC(BASE, CLASS, DESC) \
55 const bool CLASS::register_object_factory_ = \
56 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS>::record(CLASS::class_ID(), __FILE__, DESC);
59 #define GRANOO_OBJECT_FACTORY_REGISTER_T(BASE, CLASS, TPL) \
60 template<> const bool CLASS<TPL>::register_object_factory_ = \
61 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL> >::record(TPL::class_ID(), __FILE__);
63 #define GRANOO_OBJECT_FACTORY_REGISTER_DESC_T(BASE, CLASS, TPL, DESC) \
64 template<> const bool CLASS<TPL>::register_object_factory_ = \
65 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL> >::record(TPL::class_ID(), __FILE__, DESC);
68 #define GRANOO_OBJECT_FACTORY_REGISTER_T2(BASE, CLASS, TPL) \
69 template<> const bool CLASS<TPL>::register_object_factory_ = \
70 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL> >::record(CLASS<TPL>::class_ID(), __FILE__);
72 #define GRANOO_OBJECT_FACTORY_REGISTER_DESC_T2(BASE, CLASS, TPL, DESC) \
73 template<> const bool CLASS<TPL>::register_object_factory_ = \
74 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL> >::record(CLASS<TPL>::class_ID(), __FILE__, DESC);
76 #define GRANOO_OBJECT_FACTORY_REGISTER_TT(BASE, CLASS, TPL1, TPL2) \
77 template<> const bool CLASS<TPL1, TPL2>::register_object_factory_ = \
78 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL1,TPL2> >::record(CLASS<TPL1,TPL2>::class_ID(), __FILE__);
80 #define GRANOO_OBJECT_FACTORY_REGISTER_DESC_TT(BASE, CLASS, TPL1, TPL2, DESC) \
81 template<> const bool CLASS<TPL1, TPL2>::register_object_factory_ = \
82 GranOO3::Core::ObjectFactoryInterface<BASE, CLASS<TPL1,TPL2> >::record(CLASS<TPL1,TPL2>::class_ID(), __FILE__, DESC);
84 # define GRANOO_OBJECT_FACTORY_BASE_CLASS_DOC(DOC) \
85 static std::string Doc() {return DOC;}
97 template<
class BaseClass>
101 static void record(std::function<BaseClass* ()>,
const std::string&,
102 const std::string file =
"",
const std::string
desc =
"");
103 static BaseClass*
new_object(
const std::string&);
104 static std::vector<std::string>
get_ID();
114 static std::map<
const std::string, std::function<BaseClass * ()> >&
get_map();
117 template<
class BaseClass>
120 const std::string file_name,
const std::string
desc) {
122 "the id=" +
id +
" is already used");
129 template<
class BaseClass>
132 if (get_map().count(
id)!=1) {
135 for (
auto& it : get_ID())
140 std::function<BaseClass * ()> f = get_map()[id];
141 BaseClass* instance = f();
145 template<
class BaseClass>
146 std::map<
const std::string, std::function<BaseClass * ()> >&
148 static std::map<
const std::string, std::function<BaseClass * ()> > map;
152 template<
class BaseClass>
153 std::vector<std::string>
155 typedef std::map<
const std::string, std::function<BaseClass * ()> > MAP;
157 std::vector<std::string> vec;
158 for (
typename MAP::iterator it = get_map().begin(); it!=get_map().end(); ++it)
159 vec.push_back(it->first);
165 template<
class ChildClass>
190 template<
class ChildClass>
194 template<
class ChildClass>
202 template<
class ChildClass>
204 : _registered_object() {
208 template<
class ChildClass>
214 template<
class ChildClass>
217 _registered_object.push_back(&obj);
220 template<
class ChildClass>
224 return *_registered_object[rank];
227 template<
class ChildClass>
228 std::vector<ChildClass*>&
230 return _registered_object;
233 template<
class BaseClass,
class ChildClass>
238 static bool record(
const std::string&
id, std::string file_name =
"", std::string
desc =
"");
247 template<
class BaseClass,
class ChildClass>
251 return new ChildClass();
257 template<
class BaseClass,
class ChildClass>
262 template<
class BaseClass,
class ChildClass>
266 template<
class BaseClass,
class ChildClass>
274 template<
class BaseClass,
class ChildClass>
277 std::string file_name, std::string
desc) {
278 std::function<BaseClass * ()> f;
#define InternAssert(condition)
Definition: Macro.hpp:81
#define UserAssert(condition, message)
Definition: Macro.hpp:54
static bool add(const std::string §ion, const std::string &subsection, const std::string &file_name, const std::string &description)
Definition: Doc.cpp:60
Definition: ObjectFactory.hpp:99
static std::vector< std::string > get_ID()
Definition: ObjectFactory.hpp:154
static BaseClass * new_object(const std::string &)
Definition: ObjectFactory.hpp:131
static std::map< const std::string, std::function< BaseClass *()> > & get_map()
Definition: ObjectFactory.hpp:147
ObjectFactory()
Definition: ObjectFactory.hpp:107
static void record(std::function< BaseClass *()>, const std::string &, const std::string file="", const std::string desc="")
Definition: ObjectFactory.hpp:119
virtual ~ObjectFactory()
Definition: ObjectFactory.hpp:110
Definition: ObjectFactory.hpp:235
ObjectFactoryInterface()
Definition: ObjectFactory.hpp:258
static BaseClass * build()
Definition: ObjectFactory.hpp:268
virtual ~ObjectFactoryInterface()
Definition: ObjectFactory.hpp:263
static bool record(const std::string &id, std::string file_name="", std::string desc="")
Definition: ObjectFactory.hpp:276
Definition: ObjectFactory.hpp:167
std::vector< ChildClass * > _registered_object
Definition: ObjectFactory.hpp:186
std::vector< ChildClass * > & get_all_object()
Definition: ObjectFactory.hpp:229
static ObjectFactoryRegistered< ChildClass > * _me
Definition: ObjectFactory.hpp:183
void operator=(const ObjectFactoryRegistered< ChildClass > &)=delete
virtual ~ObjectFactoryRegistered()
Definition: ObjectFactory.hpp:209
void add_object(ChildClass &)
Definition: ObjectFactory.hpp:216
ChildClass & get_object(unsigned int=0)
Definition: ObjectFactory.hpp:222
ObjectFactoryRegistered(const ObjectFactoryRegistered< ChildClass > &)=delete
ObjectFactoryRegistered()
Definition: ObjectFactory.hpp:203
static ObjectFactoryRegistered< ChildClass > & get()
Definition: ObjectFactory.hpp:196
static GranOO3::Core::Out cerr
Definition: Out.hpp:101
static granoo_endl endl
Definition: Out.hpp:106
static const char * desc
Definition: Between2SetOf.cpp:37
Definition: Common.hpp:198
Definition: ObjectFactory.hpp:249
static ChildClass * build()
Definition: ObjectFactory.hpp:250