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
4#include <map>
5#include <string>
6
7namespace oe
8{
9 namespace util
10 {
11 template <typename Base, typename Key>
12 class Factory
13 {
14 public:
18 Base* createEntity(const Key& key)
19 {
20 return _map.count(key) == 1 ? _map[key].first->create() : nullptr;
21 }
22
26 void remove(Base* entity)
27 {
28 delete entity;
29 }
30
34 template <typename ElementClass>
35 void linkEntityToClass(const std::string& name, const int& priority = 0)
36 {
37 _map.insert(std::pair<std::string, std::pair<Base_deliverer*, int>>(name, {new Entity_deliverer<ElementClass>, priority}));
38 }
39
44 std::map<int, Key> getEntitiesNames()
45 {
46 std::map<int, Key> result;
47
48 for (auto& it : _map)
49 {
50 result[it.second.second] = it.first;
51 }
52
53 return result;
54 }
55
56 ~Factory()
57 {
58 for (auto it = _map.cbegin(); it != _map.cend();)
59 {
60 delete it->second.first;
61 _map.erase(it++);
62 }
63 }
64 private:
65 struct Base_deliverer
66 {
67 virtual Base* create() const = 0;
68 virtual ~Base_deliverer() {}
69 };
70
71 template <typename T>
72 struct Entity_deliverer : public Base_deliverer
73 {
74 virtual Base* create() const { return new T; };
75 };
76
77 std::map<Key, std::pair<Base_deliverer*, int>> _map;
78 };
79 }
80}
81#endif
Definition factory.h:13
void linkEntityToClass(const std::string &name, const int &priority=0)
Definition factory.h:35
void remove(Base *entity)
Definition factory.h:26
Base * createEntity(const Key &key)
Definition factory.h:18
std::map< int, Key > getEntitiesNames()
Definition factory.h:44
Oxygen Engine common namespace.
Definition debug.h:17