EzGL
ComponentFactory.hpp
Go to the documentation of this file.
1 /* EzGL/ComponentFactory.hpp
2  *
3  * Copyright (c) 2018 Kirk Lange <github.com/kirklange>
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 
22 #ifndef EZGL_COMPONENTFACTORY_HPP
23 #define EZGL_COMPONENTFACTORY_HPP
24 
29 #include "EzGL/IComponent.hpp"
30 
31 #include <map>
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 
37 
43 #define EZGL_COMPONENT_ENLIST(Obj) \
44 EzGL::ComponentFactory::Key const Obj##ComponentID = \
45  EzGL::ComponentFactory::Enlist<Component<class Obj>>(#Obj)
46 
47 
48 
49 namespace EzGL
50 {
51 
53 using ComponentPtr = std::unique_ptr<class IComponent>;
54 
56 using ComponentVec = std::vector<ComponentPtr>;
57 
63 class ComponentFactory final
64 {
65 public:
71  using Key = std::string;
72 
77  static ComponentPtr Create(Key const &key);
78 
85  template <typename T>
86  static ComponentVec Create(T const &keys)
87  {
88  ComponentVec components;
89  for (Key const &key : keys)
90  components.push_back(ComponentFactory::Create(key));
91  return components;
92  }
93 
104  template <class T>
105  static Key Enlist(Key const &key)
106  {
107  if (ComponentFactory::GetComponentMap().count(key) == 0)
108  ComponentFactory::GetComponentMap()[key] = T::Create;
109  return key;
110  }
111 
112 private:
113  ComponentFactory() = default;
114  ComponentFactory(ComponentFactory const &) = delete;
115  ComponentFactory& operator=(ComponentFactory const &) = delete;
116 
117  using ComponentMap = std::map<Key, ComponentPtr (*)()>;
118  static ComponentMap& GetComponentMap();
119 };
120 
121 }; /* namespace EzGL */
122 
123 
124 
125 #endif /* EZGL_COMPONENTFACTORY_HPP */
static Key Enlist(Key const &key)
Component enlister.
Definition: ComponentFactory.hpp:105
Component enlister and smart pointer factory.
Definition: ComponentFactory.hpp:63
std::vector< ComponentPtr > ComponentVec
Component smart pointer container.
Definition: ComponentFactory.hpp:56
Allows Component to utilize dynamic polymorphism.
std::string Key
Component key type.
Definition: ComponentFactory.hpp:71
static ComponentVec Create(T const &keys)
Component container creator.
Definition: ComponentFactory.hpp:86
static ComponentPtr Create(Key const &key)
Component smart pointer creator.
Definition: ComponentFactory.cpp:31
std::unique_ptr< class IComponent > ComponentPtr
Component smart pointer type.
Definition: ComponentFactory.hpp:53