12 #ifndef SST_CORE_FACTORY_INFO_H
13 #define SST_CORE_FACTORY_INFO_H
15 #include "sst/core/eli/elibase.h"
16 #include <type_traits>
21 template <
class Base,
class... Args>
24 typedef Base* (*createFxn)(Args...);
26 virtual Base* create(Args... ctorArgs) = 0;
28 template <
class NewBase>
32 template <
class Base,
class... CtorArgs>
44 auto iter = factories_.find(name);
45 if (iter == factories_.end()){
52 const std::map<std::string, BaseBuilder*>& getMap()
const {
56 void readdBuilder(
const std::string& name,
BaseBuilder* fact){
57 factories_[name] = fact;
60 bool addBuilder(
const std::string& elem,
BaseBuilder* fact){
61 readdBuilder(elem, fact);
62 return addLoader(name_, elem, fact);
65 template <
class NewBase>
69 bool addLoader(
const std::string& elemlib,
const std::string& elem,
BaseBuilder* fact);
71 std::map<std::string, BaseBuilder*> factories_;
76 template <
class Base,
class... CtorArgs>
81 using Map=std::map<std::string,Library*>;
83 static Library* getLibrary(
const std::string& name){
85 libraries =
new std::map<std::string,Library*>;
87 auto iter = libraries->find(name);
88 if (iter == libraries->end()){
90 (*libraries)[name] = info;
97 template <
class NewBase>
102 static Map* libraries;
108 template <
class Base,
class Builder,
class... CtorArgs>
111 const std::string& elem,
113 elemlib_(elemlib), elem_(elem), builder_(builder)
117 void load()
override {
119 ->readdBuilder(elem_,builder_);
123 std::string elemlib_;
128 template <
class Base,
class... CtorArgs>
131 auto loader =
new BuilderLoader<Base,BaseBuilder,CtorArgs...>(elemlib, elem, fact);
135 template <
class Base,
class T>
137 static bool isLoaded() {
141 static const bool loaded;
145 = Base::Ctor::template add<T>();
147 template <
class Base,
class T,
class Enable=
void>
150 template <
class... Args>
151 T* operator()(Args&&... args){
152 return new T(std::forward<Args>(args)...);
156 template <
class Base,
class T>
159 template <
class... Args>
160 Base* operator()(Args&&... ctorArgs) {
162 cached_ =
new T(std::forward<Args>(ctorArgs)...);
167 static Base* cached_;
169 template <
class Base,
class T>
172 template <
class T,
class Base,
class... Args>
175 Base* create(Args... ctorArgs)
override {
181 template <
class T,
class U>
184 template <
class T,
class... Args>
186 public std::is_constructible<T, Args...>
191 template <
class T,
class... Args>
192 static BuilderLibrary<T,Args...>* getLibrary(
const std::string& name){
197 template <
class Base,
class CtorTuple>
200 template <
class Base,
class... Args>
203 static BuilderLibrary<Base,Args...>* getLibrary(
const std::string& name){
207 template <
class T>
static Builder<Base,Args...>* makeBuilder(){
220 template <
class NewCtor,
class OldCtor>
224 using is_constructible =
typename NewCtor::template is_constructible<T>;
232 static typename std::enable_if<OldCtor::template is_constructible<T>::value,
bool>::type
235 return NewCtor::template add<T>() && OldCtor::template add<T>();
239 static typename std::enable_if<!OldCtor::template is_constructible<T>::value,
bool>::type
242 return NewCtor::template add<T>();
245 template <
class __NewCtor>
246 using ExtendCtor = ExtendedCtor<__NewCtor, ExtendedCtor<NewCtor,OldCtor>>;
248 template <
class NewBase>
249 using ChangeBase =
typename NewCtor::template ChangeBase<NewBase>;
252 template <
class Base,
class... Args>
256 using is_constructible = std::is_constructible<T,Args...>;
258 template <
class T>
static bool add(){
261 return Base::addBuilder(T::ELI_getLibrary(),T::ELI_getName(),fact);
264 template <
class NewBase>
267 template <
class NewCtor>
271 template <
class Base,
class Ctor,
class... Ctors>
275 using is_constructible =
typename std::conditional<is_tuple_constructible<T,Ctor>::value,
277 typename CtorList<Base,Ctors...>::template is_constructible<T>
280 template <
class T,
int NumVal
id=0,
class U=T>
285 Base::addBuilder(T::ELI_getLibrary(),T::ELI_getName(),fact);
286 return CtorList<Base,Ctors...>::template add<T,NumValid+1>();
289 template <
class T,
int NumVal
id=0,
class U=T>
292 return CtorList<Base,Ctors...>::template add<T,NumValid>();
295 template <
class NewBase>
300 template <
int NumVal
id>
302 static constexpr
bool atLeastOneValidCtor =
true;
310 using is_constructible = std::false_type;
312 template <
class T,
int numVal
idCtors>
321 #define ELI_CTOR(...) std::tuple<__VA_ARGS__>
322 #define ELI_DEFAULT_CTOR() std::tuple<>
324 #define SST_ELI_CTORS_COMMON(...) \
325 using Ctor = ::SST::ELI::CtorList<__LocalEliBase,__VA_ARGS__,void>; \
326 template <class __TT, class... __CtorArgs> \
327 using DerivedBuilder = ::SST::ELI::DerivedBuilder<__LocalEliBase,__TT,__CtorArgs...>; \
328 template <class... __InArgs> static SST::ELI::BuilderLibrary<__LocalEliBase,__InArgs...>* \
329 getBuilderLibraryTemplate(const std::string& name){ \
330 return ::SST::ELI::BuilderDatabase::getLibrary<__LocalEliBase,__InArgs...>(name); \
332 template <class __TT> static bool addDerivedBuilder(const std::string& lib, const std::string& elem){ \
333 return Ctor::template add<0,__TT>(lib,elem); \
336 #define SST_ELI_DECLARE_CTORS(...) \
337 SST_ELI_CTORS_COMMON(ELI_FORWARD_AS_ONE(__VA_ARGS__)) \
338 template <class... Args> static bool addBuilder(const std::string& elemlib, const std::string& elem, \
339 SST::ELI::Builder<__LocalEliBase,Args...>* builder){ \
340 return getBuilderLibraryTemplate<Args...>(elemlib)->addBuilder(elem, builder); \
343 #define SST_ELI_DECLARE_CTORS_EXTERN(...) \
344 SST_ELI_CTORS_COMMON(ELI_FORWARD_AS_ONE(__VA_ARGS__))
349 #define SST_ELI_BUILDER_TYPEDEFS(...) \
350 using BaseBuilder = ::SST::ELI::Builder<__VA_ARGS__>; \
351 using BuilderLibrary = ::SST::ELI::BuilderLibrary<__VA_ARGS__>; \
352 using BuilderLibraryDatabase = ::SST::ELI::BuilderLibraryDatabase<__VA_ARGS__>; \
353 template <class __TT> using DerivedBuilder = ::SST::ELI::DerivedBuilder<__TT,__VA_ARGS__>;
355 #define SST_ELI_BUILDER_FXNS() \
356 static BuilderLibrary* getBuilderLibrary(const std::string& name){ \
357 return BuilderLibraryDatabase::getLibrary(name); \
359 static bool addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder){ \
360 return getBuilderLibrary(elemlib)->addBuilder(elem,builder); \
364 #define SST_ELI_DECLARE_CTOR(...) \
365 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase,__VA_ARGS__>; \
366 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase,__VA_ARGS__) \
367 SST_ELI_BUILDER_FXNS()
369 #define SST_ELI_BUILDER_FXNS_EXTERN() \
370 static BuilderLibrary* getBuilderLibrary(const std::string& name); \
371 static bool addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder);
373 #define SST_ELI_DECLARE_CTOR_EXTERN(...) \
374 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase,__VA_ARGS__>; \
375 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase,__VA_ARGS__); \
376 SST_ELI_BUILDER_FXNS_EXTERN()
378 #define SST_ELI_DEFINE_CTOR_EXTERN(base) \
379 bool base::addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder){ \
380 return getBuilderLibrary(elemlib)->addBuilder(elem,builder); \
382 base::BuilderLibrary* base::getBuilderLibrary(const std::string& elemlib){ \
383 return BuilderLibraryDatabase::getLibrary(elemlib); \
387 #define SST_ELI_DECLARE_DEFAULT_CTOR() \
388 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase>; \
389 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase) \
390 SST_ELI_BUILDER_FXNS()
392 #define SST_ELI_DECLARE_DEFAULT_CTOR_EXTERN() \
393 SST_ELI_DEFAULT_CTOR_COMMON() \
394 SST_ELI_BUILDER_FXNS_EXTERN()
396 #define SST_ELI_EXTEND_CTOR() \
397 using Ctor = ::SST::ELI::ExtendedCtor<LocalCtor, __ParentEliBase::Ctor>;
399 #define SST_ELI_SAME_BASE_CTOR() \
400 using LocalCtor = __ParentEliBase::Ctor::ChangeBase<__LocalEliBase>; \
401 SST_ELI_EXTEND_CTOR() \
402 using BaseBuilder = typename __ParentEliBase::BaseBuilder::template ChangeBase<__LocalEliBase>; \
403 using BuilderLibrary = __ParentEliBase::BuilderLibrary::ChangeBase<__LocalEliBase>; \
404 using BuilderLibraryDatabase = __ParentEliBase::BuilderLibraryDatabase::ChangeBase<__LocalEliBase>; \
405 SST_ELI_BUILDER_FXNS()
407 #define SST_ELI_NEW_BASE_CTOR(...) \
408 using LocalCtor = ::SST::ELI::SingleCtor<__LocalEliBase,__VA_ARGS__>; \
409 SST_ELI_EXTEND_CTOR() \
410 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase, __VA_ARGS__) \
411 SST_ELI_BUILDER_FXNS()
413 #define SST_ELI_DEFAULT_BASE_CTOR() \
414 using LocalCtor = ::SST::ELI::SingleCtor<__LocalEliBase>; \
415 SST_ELI_EXTEND_CTOR() \
416 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase) \
417 SST_ELI_BUILDER_FXNS()
Definition: elementbuilder.h:182
Definition: elementbuilder.h:198
Definition: elementbuilder.h:109
Definition: elementbuilder.h:272
Definition: elementbuilder.h:33
Definition: elementbuilder.h:173
Definition: elementbuilder.h:190
Definition: elementbuilder.h:301
Definition: elibase.h:107
Definition: elementbuilder.h:148
Definition: elementbuilder.h:136
static std::enable_if< OldCtor::template is_constructible< T >::value, bool >::type add()
The derived Ctor can "block" the more abstract Ctor, meaning an object should only be instantiated as...
Definition: elementbuilder.h:233
Definition: elementbuilder.h:77
Definition: elementbuilder.h:253
Definition: elementbuilder.h:22
Implements a constructor for a derived base as usually happens with subcomponents, e.g.
Definition: elementbuilder.h:221
Definition: elementbuilder.h:157
static bool addLoader(const std::string &lib, const std::string &name, LibraryLoader *loader)
Definition: elibase.cc:29