diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/managed/Managed.cpp | 14 | ||||
-rw-r--r-- | src/core/managed/Managed.hpp | 42 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/core/managed/Managed.cpp b/src/core/managed/Managed.cpp index 64e287d..731947b 100644 --- a/src/core/managed/Managed.cpp +++ b/src/core/managed/Managed.cpp @@ -42,6 +42,20 @@ Rooted<Managed> Managed::readData(const std::string &key) return mgr.readData(this, key); } +Rooted<Managed> Managed::readData(const std::string &key, const Rtti *type) +{ + Rooted<Managed> res = mgr.readData(this, key); + if (res != nullptr && res->type()->isa(type)) { + return res; + } + return nullptr; +} + +Managed* Managed::readDataPtr(const std::string &key, const std::type_info &type) +{ + return readData(key, RttiStore::lookup(type)).get(); +} + std::map<std::string, Rooted<Managed>> Managed::readData() { auto map = mgr.readData(this); diff --git a/src/core/managed/Managed.hpp b/src/core/managed/Managed.hpp index e38efea..7622e0e 100644 --- a/src/core/managed/Managed.hpp +++ b/src/core/managed/Managed.hpp @@ -28,6 +28,8 @@ #ifndef _OUSIA_MANAGED_HPP_ #define _OUSIA_MANAGED_HPP_ +#include <typeinfo> + #include "Manager.hpp" namespace ousia { @@ -67,6 +69,16 @@ protected: */ Manager &mgr; + /** + * Used internally to retrieve the pointer of a stored data element. Behaves + * just like readData but returns a pointer. + * + * @param key is the key specifying the data slot. + * @param type is the type that is expected for the data with the given key. + * @return previously stored data or nullptr. + */ + Managed* readDataPtr(const std::string &key, const std::type_info &type); + public: /** * Constructor of the Managed class. Associates the new instance with the @@ -171,6 +183,36 @@ public: Rooted<Managed> readData(const std::string &key); /** + * Returns data previously stored under the given key. Makes sure the data + * is of the given type. + * + * @param key is the key specifying the slot from which the data should be + * read. + * @param type is the type that is expected for the data with the given key. + * @return previously stored data or nullptr if no data was stored for this + * key. Nullptr is returned if the stored data does not have the correct + * type. + */ + Rooted<Managed> readData(const std::string &key, const Rtti *type); + + /** + * Returns data previously stored under the given key. Makes sure the data + * is of the given type. + * + * @param key is the key specifying the slot from which the data should be + * read. + * @param type is the type that is expected for the data with the given key. + * @return previously stored data or nullptr if no data was stored for this + * key. Nullptr is returned if the stored data does not have the correct + * type. + */ + template<typename T> + Rooted<T> readData(const std::string &key) + { + return Rooted<T>(static_cast<T*>(readDataPtr(key, typeid(T)))); + } + + /** * Returns a copy of all data that was attached to the node. * * @return a map between keys and stored data. |