From d1adb30aa372397976b0a56ac2eea4063757ba2a Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 16 Apr 2015 01:07:59 +0200 Subject: Added functions for reading attached data with a certain expected type --- src/core/managed/Managed.cpp | 14 ++++++++++++++ src/core/managed/Managed.hpp | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) 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::readData(const std::string &key) return mgr.readData(this, key); } +Rooted Managed::readData(const std::string &key, const Rtti *type) +{ + Rooted 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> 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 + #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 @@ -170,6 +182,36 @@ public: */ Rooted 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 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 + Rooted readData(const std::string &key) + { + return Rooted(static_cast(readDataPtr(key, typeid(T)))); + } + /** * Returns a copy of all data that was attached to the node. * -- cgit v1.2.3