12 #ifndef SST_CORE_ELI_ELEMENTBUILDER_H
13 #define SST_CORE_ELI_ELEMENTBUILDER_H
15 #include "sst/core/eli/elibase.h"
17 #include <type_traits>
22 template <
class Base,
class... Args>
25 typedef Base* (*createFxn)(Args...);
27 virtual Base* create(Args... ctorArgs) = 0;
29 template <
class NewBase>
33 template <
class Base,
class... CtorArgs>
43 auto iter = factories_.find(name);
44 if ( iter == factories_.end() ) {
return nullptr; }
50 const std::map<std::string, BaseBuilder*>& getMap()
const {
return factories_; }
52 void readdBuilder(
const std::string& name,
BaseBuilder* fact) { factories_[name] = fact; }
54 bool addBuilder(
const std::string& elem,
BaseBuilder* fact)
56 readdBuilder(elem, fact);
57 return addLoader(name_, elem, fact);
60 template <
class NewBase>
64 bool addLoader(
const std::string& elemlib,
const std::string& elem,
BaseBuilder* fact);
66 std::map<std::string, BaseBuilder*> factories_;
71 template <
class Base,
class... CtorArgs>
77 using Map = std::map<std::string, Library*>;
79 static Library* getLibrary(
const std::string& name)
81 if ( !libraries ) { libraries =
new std::map<std::string, Library*>; }
82 auto iter = libraries->find(name);
83 if ( iter == libraries->end() ) {
85 (*libraries)[name] = info;
93 template <
class NewBase>
98 static Map* libraries;
101 template <
class Base,
class... CtorArgs>
104 template <
class Base,
class Builder,
class... CtorArgs>
119 std::string elemlib_;
124 template <
class Base,
class... CtorArgs>
128 auto loader =
new BuilderLoader<Base, BaseBuilder, CtorArgs...>(elemlib, elem, fact);
132 template <
class Base,
class T>
135 static bool isLoaded() {
return loaded; }
137 static const bool loaded;
140 template <
class Base,
class T>
143 template <
class Base,
class T,
class Enable =
void>
146 template <
class... Args>
147 T* operator()(Args&&... args)
149 return new T(std::forward<Args>(args)...);
153 template <
class Base,
class T>
156 template <
class... Args>
157 Base* operator()(Args&&... ctorArgs)
159 if ( !cached_ ) { cached_ =
new T(std::forward<Args>(ctorArgs)...); }
163 static Base* cached_;
165 template <
class Base,
class T>
168 template <
class T,
class Base,
class... Args>
171 Base* create(Args... ctorArgs)
override {
return Allocator<Base, T>()(std::forward<Args>(ctorArgs)...); }
174 template <
class T,
class U>
178 template <
class T,
class... Args>
184 template <
class T,
class... Args>
185 static BuilderLibrary<T, Args...>* getLibrary(
const std::string& name)
191 template <
class Base,
class CtorTuple>
195 template <
class Base,
class... Args>
198 static BuilderLibrary<Base, Args...>* getLibrary(
const std::string& name)
204 static Builder<Base, Args...>* makeBuilder()
216 template <
class NewCtor,
class OldCtor>
220 using is_constructible =
typename NewCtor::template is_constructible<T>;
228 static typename std::enable_if<OldCtor::template is_constructible<T>::value,
bool>::type
add()
231 return NewCtor::template add<T>() && OldCtor::template add<T>();
235 static typename std::enable_if<!OldCtor::template is_constructible<T>::value,
bool>::type
add()
238 return NewCtor::template add<T>();
241 template <
class __NewCtor>
242 using ExtendCtor = ExtendedCtor<__NewCtor, ExtendedCtor<NewCtor, OldCtor>>;
244 template <
class NewBase>
245 using ChangeBase =
typename NewCtor::template ChangeBase<NewBase>;
248 template <
class Base,
class... Args>
252 using is_constructible = std::is_constructible<T, Args...>;
259 return Base::addBuilder(T::ELI_getLibrary(), T::ELI_getName(), fact);
262 template <
class NewBase>
265 template <
class NewCtor>
269 template <
class Base,
class Ctor,
class... Ctors>
273 using is_constructible =
typename std::conditional<
276 typename CtorList<Base, Ctors...>::template is_constructible<T>
279 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>
293 return CtorList<Base, Ctors...>::template add<T, NumValid>();
296 template <
class NewBase>
300 template <
int NumVal
id>
303 static constexpr
bool atLeastOneValidCtor =
true;
310 template <
class Base>
314 using is_constructible = std::false_type;
316 template <
class T,
int numVal
idCtors>
326 #define ELI_CTOR(...) std::tuple<__VA_ARGS__>
327 #define ELI_DEFAULT_CTOR() std::tuple<>
329 #define SST_ELI_CTORS_COMMON(...) \
330 using Ctor = ::SST::ELI::CtorList<__LocalEliBase, __VA_ARGS__, void>; \
331 template <class __TT, class... __CtorArgs> \
332 using DerivedBuilder = ::SST::ELI::DerivedBuilder<__LocalEliBase, __TT, __CtorArgs...>; \
333 template <class... __InArgs> \
334 static SST::ELI::BuilderLibrary<__LocalEliBase, __InArgs...>* getBuilderLibraryTemplate(const std::string& name) \
336 return ::SST::ELI::BuilderDatabase::getLibrary<__LocalEliBase, __InArgs...>(name); \
338 template <class __TT> \
339 static bool addDerivedBuilder(const std::string& lib, const std::string& elem) \
341 return Ctor::template add<0, __TT>(lib, elem); \
344 #define SST_ELI_DECLARE_CTORS(...) \
345 SST_ELI_CTORS_COMMON(ELI_FORWARD_AS_ONE(__VA_ARGS__)) \
346 template <class... Args> \
347 static bool addBuilder( \
348 const std::string& elemlib, const std::string& elem, SST::ELI::Builder<__LocalEliBase, Args...>* builder) \
350 return getBuilderLibraryTemplate<Args...>(elemlib)->addBuilder(elem, builder); \
353 #define SST_ELI_DECLARE_CTORS_EXTERN(...) SST_ELI_CTORS_COMMON(ELI_FORWARD_AS_ONE(__VA_ARGS__))
358 #define SST_ELI_BUILDER_TYPEDEFS(...) \
359 using BaseBuilder = ::SST::ELI::Builder<__VA_ARGS__>; \
360 using BuilderLibrary = ::SST::ELI::BuilderLibrary<__VA_ARGS__>; \
361 using BuilderLibraryDatabase = ::SST::ELI::BuilderLibraryDatabase<__VA_ARGS__>; \
362 template <class __TT> \
363 using DerivedBuilder = ::SST::ELI::DerivedBuilder<__TT, __VA_ARGS__>;
365 #define SST_ELI_BUILDER_FXNS() \
366 static BuilderLibrary* getBuilderLibrary(const std::string& name) \
368 return BuilderLibraryDatabase::getLibrary(name); \
370 static bool addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder) \
372 return getBuilderLibrary(elemlib)->addBuilder(elem, builder); \
376 #define SST_ELI_DECLARE_CTOR(...) \
377 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase, __VA_ARGS__>; \
378 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase, __VA_ARGS__) \
379 SST_ELI_BUILDER_FXNS()
381 #define SST_ELI_BUILDER_FXNS_EXTERN() \
382 static BuilderLibrary* getBuilderLibrary(const std::string& name); \
383 static bool addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder);
385 #define SST_ELI_DECLARE_CTOR_EXTERN(...) \
386 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase, __VA_ARGS__>; \
387 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase, __VA_ARGS__); \
388 SST_ELI_BUILDER_FXNS_EXTERN()
390 #define SST_ELI_DEFINE_CTOR_EXTERN(base) \
391 bool base::addBuilder(const std::string& elemlib, const std::string& elem, BaseBuilder* builder) \
393 return getBuilderLibrary(elemlib)->addBuilder(elem, builder); \
395 base::BuilderLibrary* base::getBuilderLibrary(const std::string& elemlib) \
397 return BuilderLibraryDatabase::getLibrary(elemlib); \
401 #define SST_ELI_DECLARE_DEFAULT_CTOR() \
402 using Ctor = ::SST::ELI::SingleCtor<__LocalEliBase>; \
403 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase) \
404 SST_ELI_BUILDER_FXNS()
406 #define SST_ELI_DECLARE_DEFAULT_CTOR_EXTERN() \
407 SST_ELI_DEFAULT_CTOR_COMMON() \
408 SST_ELI_BUILDER_FXNS_EXTERN()
410 #define SST_ELI_EXTEND_CTOR() using Ctor = ::SST::ELI::ExtendedCtor<LocalCtor, __ParentEliBase::Ctor>;
412 #define SST_ELI_SAME_BASE_CTOR() \
413 using LocalCtor = __ParentEliBase::Ctor::ChangeBase<__LocalEliBase>; \
414 SST_ELI_EXTEND_CTOR() \
415 using BaseBuilder = typename __ParentEliBase::BaseBuilder::template ChangeBase<__LocalEliBase>; \
416 using BuilderLibrary = __ParentEliBase::BuilderLibrary::ChangeBase<__LocalEliBase>; \
417 using BuilderLibraryDatabase = __ParentEliBase::BuilderLibraryDatabase::ChangeBase<__LocalEliBase>; \
418 SST_ELI_BUILDER_FXNS()
420 #define SST_ELI_NEW_BASE_CTOR(...) \
421 using LocalCtor = ::SST::ELI::SingleCtor<__LocalEliBase, __VA_ARGS__>; \
422 SST_ELI_EXTEND_CTOR() \
423 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase, __VA_ARGS__) \
424 SST_ELI_BUILDER_FXNS()
426 #define SST_ELI_DEFAULT_BASE_CTOR() \
427 using LocalCtor = ::SST::ELI::SingleCtor<__LocalEliBase>; \
428 SST_ELI_EXTEND_CTOR() \
429 SST_ELI_BUILDER_TYPEDEFS(__LocalEliBase) \
430 SST_ELI_BUILDER_FXNS()
432 #endif // SST_CORE_ELI_ELEMENTBUILDER_H
Definition: elementbuilder.h:175
Definition: elementbuilder.h:192
Definition: elementbuilder.h:105
Definition: elementbuilder.h:270
Definition: elementbuilder.h:34
Definition: elementbuilder.h:169
Definition: elementbuilder.h:182
Definition: elementbuilder.h:301
Definition: elibase.h:119
Definition: elementbuilder.h:144
Definition: elementbuilder.h:133
Definition: elementbuilder.h:72
Definition: elementbuilder.h:249
Definition: elementbuilder.h:23
Implements a constructor for a derived base as usually happens with subcomponents, e.g.
Definition: elementbuilder.h:217
Definition: elementbuilder.h:154
static bool addLoader(const std::string &lib, const std::string &name, LibraryLoader *loader)
Definition: elibase.cc:28
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:228