AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
DataMap Class Reference

#include "DataMap.h"

Classes

class  Base
 

Public Member Functions

template<class T >
T * Get (std::string const &k) const
 
template<class T , typename std::enable_if< std::is_default_constructible< T >::value, int >::type = 0>
T * GetDefault (std::string const &k)
 
void Set (std::string const &k, Base *v)
 
bool Erase (std::string const &k)
 

Private Attributes

std::unordered_map< std::string, std::unique_ptr< Base > > Container
 

Detailed Description

Member Function Documentation

◆ Erase()

bool DataMap::Erase ( std::string const &  k)
inline

Removes objects with given key and returns true if one was removed, false otherwise

70{ return Container.erase(k) != 0; }
std::unordered_map< std::string, std::unique_ptr< Base > > Container
Definition: DataMap.h:73

References Container.

◆ Get()

template<class T >
T * DataMap::Get ( std::string const &  k) const
inline

Returns a pointer to object of requested type stored with given key or nullptr

30 {
31 static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
32 if (Container.empty())
33 {
34 return nullptr;
35 }
36
37 auto it = Container.find(k);
38 if (it != Container.end())
39 {
40 return dynamic_cast<T*>(it->second.get());
41 }
42 return nullptr;
43 }

References Container.

◆ GetDefault()

template<class T , typename std::enable_if< std::is_default_constructible< T >::value, int >::type = 0>
T * DataMap::GetDefault ( std::string const &  k)
inline

Returns a pointer to object of requested type stored with given key or default constructs one and returns that one

51 {
52 static_assert(std::is_base_of<Base, T>::value, "T must derive from Base");
53 if (T* v = Get<T>(k))
54 {
55 return v;
56 }
57 T* v = new T();
58 Container.emplace(k, std::unique_ptr<T>(v));
59 return v;
60 }

References Container.

◆ Set()

void DataMap::Set ( std::string const &  k,
Base v 
)
inline

Stores a new object that inherits the Base class with the given key

65{ Container[k] = std::unique_ptr<Base>(v); }

References Container.

Member Data Documentation

◆ Container

std::unordered_map<std::string, std::unique_ptr<Base> > DataMap::Container
private

Referenced by Erase(), Get(), GetDefault(), and Set().