/* * Originally written by Rochet2 - Copyright (C) 2018+ AzerothCore , released under GNU AGPL v3 license: http://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE */ #ifndef _DATA_MAP_H_ #define _DATA_MAP_H_ #include #include #include #include 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 T* Get(std::string const& k) const { static_assert(std::is_base_of::value, "T must derive from Base"); if (Container.empty()) { return nullptr; } auto it = Container.find(k); if (it != Container.end()) { return dynamic_cast(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::value, int>::type = 0> T * GetDefault(std::string const& k) { static_assert(std::is_base_of::value, "T must derive from Base"); if (T* v = Get(k)) { return v; } T* v = new T(); Container.emplace(k, std::unique_ptr(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(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> Container; }; #endif