ifactory.h
Go to the documentation of this file.
1 #ifndef DNAI_INTERFACES_IFACTORY_H
2 #define DNAI_INTERFACES_IFACTORY_H
3 
4 #include <map>
5 #include <functional>
6 #include <type_traits>
7 
8 namespace dnai {
9  namespace interfaces {
15  template <typename FactoryType, class ClassIdentifier>
16  class IFactory
17  {
18  using Callback = std::function<FactoryType *()>;
19  using Key = typename std::conditional<std::is_enum<ClassIdentifier>::value, int, ClassIdentifier>::type;
20  public:
21  IFactory() = default;
27  FactoryType *create(Key ci)
28  {
29  return m_factoryObjects[ci]();
30  }
31 
39  template<typename TrueType, typename ... Args>
40  void registerObject(Key ci, Args&& ... args)
41  {
42  m_factoryObjects[ci] = [=]() { return static_cast<FactoryType *>(new TrueType(std::move(args)...)); };
43  }
44 
45  private:
46  std::map<Key, Callback> m_factoryObjects;
47  };
48  }
49 }
50 
51 
52 #endif //DNAI_INTERFACES_IFACTORY_H
typename std::conditional< std::is_enum< ClassIdentifier >::value, int, ClassIdentifier >::type Key
Definition: ifactory.h:19
std::function< FactoryType *()> Callback
Definition: ifactory.h:18
FactoryType * create(Key ci)
Return a new instance of type (FactoryType) with the identifier of type (ClassIdentifier) ...
Definition: ifactory.h:27
std::map< Key, Callback > m_factoryObjects
Definition: ifactory.h:46
Definition: api.h:13
void registerObject(Key ci, Args &&...args)
Register a new class of type (TrueType) witch derived of base type (FactoryType) and assign an identi...
Definition: ifactory.h:40
This class allow you to create a factory for anytype of the Base class (FactoryType) with anytype of ...
Definition: ifactory.h:16