Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
factory.h
1#ifndef OE_UTIL_FACTORY_H
2#define OE_UTIL_FACTORY_H
3
4namespace oe
5{
6 namespace util
7 {
8 template <typename Base, typename Key>
9 class Factory
10 {
11 public:
15 Base* createEntity(const Key& key)
16 {
17 return _map.count(key) == 1 ? _map[key].first->create() : nullptr;
18 }
19
23 void remove(Base* entity)
24 {
25 delete entity;
26 }
27
31 template <typename ElementClass>
32 void linkEntityToClass(const std::string& name, const int& priority = 0)
33 {
34 _map.insert(std::pair<std::string, std::pair<Base_deliverer*, int>>(name, {new Entity_deliverer<ElementClass>, priority}));
35 }
36
41 std::map<int, Key> getEntitiesNames()
42 {
43 std::map<int, Key> result;
44
45 for (auto& it : _map)
46 {
47 result[it.second.second] = it.first;
48 }
49
50 return result;
51 }
52
53 ~Factory()
54 {
55 for (auto it = _map.cbegin(); it != _map.cend();)
56 {
57 delete it->second.first;
58 _map.erase(it++);
59 }
60 }
61 private:
62 struct Base_deliverer
63 {
64 virtual Base* create() const = 0;
65 virtual ~Base_deliverer() {}
66 };
67
68 template <typename T>
69 struct Entity_deliverer : public Base_deliverer
70 {
71 virtual Base* create() const { return new T; };
72 };
73
74 std::map<Key, std::pair<Base_deliverer*, int>> _map;
75 };
76 }
77}
78#endif
Definition factory.h:10
void linkEntityToClass(const std::string &name, const int &priority=0)
Definition factory.h:32
void remove(Base *entity)
Definition factory.h:23
Base * createEntity(const Key &key)
Definition factory.h:15
std::map< int, Key > getEntitiesNames()
Definition factory.h:41
Oxygen Engine common namespace.
Definition cursor.h:8