Ocular Engine
ComponentFactory.hpp
1 
17 #pragma once
18 #ifndef __H__OCULAR_CORE_SCENE_COMPONENT_FACTORY__H__
19 #define __H__OCULAR_CORE_SCENE_COMPONENT_FACTORY__H__
20 
21 #include <unordered_map>
22 #include <string>
23 #include <functional>
24 
25 //------------------------------------------------------------------------------------------
26 
31 namespace Ocular
32 {
37  namespace Core
38  {
42  template<class T>
44  {
45  public:
46 
47  ComponentFactory() { }
48  ~ComponentFactory() { }
49 
53  T* createComponent(std::string const& name)
54  {
55  T* result = nullptr;
56  const auto find = m_ComponentMap.find(name);
57 
58  if(find != m_ComponentMap.end())
59  {
60  result = find->second();
61  }
62 
63  return result;
64  }
65 
69  template<typename S>
70  bool registerComponent(std::string name)
71  {
72  bool result = false;
73  const auto find = m_ComponentMap.find(name);
74 
75  if(find == m_ComponentMap.end())
76  {
77  m_ComponentMap.insert(std::make_pair(name, [](){ return new S; }));
78  result = true;
79  }
80 
81  return result;
82  }
83 
84  std::vector<std::string> getRegisteredKeys() const
85  {
86  std::vector<std::string> result;
87  result.reserve(m_ComponentMap.size());
88 
89  for(auto keyvalue : m_ComponentMap)
90  {
91  result.emplace_back(keyvalue.first);
92  }
93 
94  return result;
95  }
96 
97  protected:
98 
99  private:
100 
101  std::unordered_map<std::string, std::function<T*()>> m_ComponentMap;
102  };
103  }
107 }
112 //------------------------------------------------------------------------------------------
113 
114 #endif
Definition: ComponentFactory.hpp:43
Note: Once this library is made dynamic, this will no longer be needed.
Definition: Common.hpp:70