blob: 2deb99904e2f614c37eb0466401a73fb1663cdfe (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* Originally written by Rochet2 - Copyright (C) 2018+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: http://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
*/
#ifndef _DATA_MAP_H_
#define _DATA_MAP_H_
#include <memory>
#include <string>
#include <type_traits>
#include <unordered_map>
class DataMap
{
public:
/**
* Base class that you should inherit in your script.
* Inheriting classes can be stored to DataMap
*/
class Base
{
public:
virtual ~Base() = default;
};
/**
* Returns a pointer to object of requested type stored with given key or nullptr
*/
template<class T> T* Get(std::string const& k) const
{
static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
if (Container.empty())
{
return nullptr;
}
auto it = Container.find(k);
if (it != Container.end())
{
return dynamic_cast<T*>(it->second.get());
}
return nullptr;
}
/**
* Returns a pointer to object of requested type stored with given key
* or default constructs one and returns that one
*/
template<class T, typename std::enable_if<std::is_default_constructible<T>::value, int>::type = 0>
T * GetDefault(std::string const& k)
{
static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
if (T* v = Get<T>(k))
{
return v;
}
T* v = new T();
Container.emplace(k, std::unique_ptr<T>(v));
return v;
}
/**
* Stores a new object that inherits the Base class with the given key
*/
void Set(std::string const& k, Base* v) { Container[k] = std::unique_ptr<Base>(v); }
/**
* Removes objects with given key and returns true if one was removed, false otherwise
*/
bool Erase(std::string const& k) { return Container.erase(k) != 0; }
private:
std::unordered_map<std::string, std::unique_ptr<Base>> Container;
};
#endif
|