From 04248309bdedb58be2fe1aa1fad0342ac936b541 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sat, 20 Dec 2014 00:16:37 +0100 Subject: improved RTTI system --- src/core/managed/Managed.hpp | 13 ++++---- src/core/managed/ManagedType.cpp | 2 +- src/core/managed/ManagedType.hpp | 72 +++++++++++++++++++++++++++++++++++----- 3 files changed, 71 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/core/managed/Managed.hpp b/src/core/managed/Managed.hpp index 4818c3d..cc55ae0 100644 --- a/src/core/managed/Managed.hpp +++ b/src/core/managed/Managed.hpp @@ -114,19 +114,19 @@ public: * @return a reference to the registered ManagedType for this particular * Managed class. */ - const ManagedType& type() const { - return ManagedType::typeOf(typeid(*this)); + const ManagedType &type() const + { + return ManagedType::typeOf(*this); } /** * Returns true if this Managed instance is of the given ManagedType. * * @param true if the ManagedType registered for this particular Managed - * class is + * class is of the given type or one of the registered parent types is of + * the given type. */ - bool isa(const ManagedType &t) const { - return type().isa(t); - } + bool isa(const ManagedType &t) const { return type().isa(t); } }; /** @@ -512,7 +512,6 @@ public: */ Managed *getOwner() const { return owner; } }; - } #endif /* _OUSIA_MANAGED_HPP_ */ diff --git a/src/core/managed/ManagedType.cpp b/src/core/managed/ManagedType.cpp index ed4c7da..53e75a3 100644 --- a/src/core/managed/ManagedType.cpp +++ b/src/core/managed/ManagedType.cpp @@ -26,7 +26,7 @@ const ManagedType ManagedType::None; /* Class ManagedType */ -const ManagedType &ManagedType::typeOf(const std::type_info &nativeType) +const ManagedType &ManagedType::rttiLookup(const std::type_info &nativeType) { auto it = table().find(std::type_index{nativeType}); if (it == table().end()) { diff --git a/src/core/managed/ManagedType.hpp b/src/core/managed/ManagedType.hpp index f3ed5fd..0a0d445 100644 --- a/src/core/managed/ManagedType.hpp +++ b/src/core/managed/ManagedType.hpp @@ -19,10 +19,12 @@ #ifndef _OUSIA_MANAGED_TYPE_HPP_ #define _OUSIA_MANAGED_TYPE_HPP_ +#include + #include #include #include -#include +#include namespace ousia { @@ -36,7 +38,8 @@ private: * Used internally to store all registered native types and their * corresponding type information. */ - static std::unordered_map& table() { + static std::unordered_map &table() + { static std::unordered_map table; return table; } @@ -49,7 +52,14 @@ private: /** * Set containing references to the parent types. */ - const std::unordered_set parents; + const std::vector parents; + + /** + * Returns the ManagedType for the given type_info structure. + * + * @param nativeType is a pointer at the C++ RTTI information. + */ + static const ManagedType &rttiLookup(const std::type_info &nativeType); public: /** @@ -58,9 +68,30 @@ public: static const ManagedType None; /** - * Returns the ManagedType for the given type_info structure. + * Returns the ManagedType for the given native type. + * + * @tparam T is the C++ type for which the type information should be + * returned. + * @param obj is a dummy object for which the type information should be + * returned. */ - static const ManagedType &typeOf(const std::type_info &nativeType); + template + static const ManagedType &typeOf(const T &obj) + { + return rttiLookup(typeid(obj)); + } + + /** + * Returns the ManagedType for the given native type. + * + * @tparam T is the C++ type for which the type information should be + * returned. + */ + template + static const ManagedType &typeOf() + { + return rttiLookup(typeid(T)); + } /** * Default constructor. Creates a ManagedType instance with name "unknown" @@ -92,7 +123,7 @@ public: * @param parents is a list of parent types. */ ManagedType(std::string name, const std::type_info &nativeType, - std::unordered_set parents) + const std::vector &parents) : name(std::move(name)), parents(parents) { table().emplace(std::make_pair(std::type_index{nativeType}, this)); @@ -105,14 +136,39 @@ public: /** * Returns true if this ManagedType instance is the given type or has the - *given - * type as one of its parents. + * given type as one of its parents. * * @param other is the other type for which the relation to this type * should be checked. */ bool isa(const ManagedType &other) const; }; + +/** + * Function that can be used to retrieve the RTTI information of a Managed + * object. + * + * @tparam T is the C++ type for which the type information should be returned. + */ +template +inline const ManagedType &typeOf() +{ + return ManagedType::typeOf(); +} + +/** + * Function that can be used to retrieve the RTTI information of a Managed + * object. + * + * @tparam T is the C++ type for which the type information should be returned. + * @param obj is a dummy object for which the type information should be + * returned. + */ +template +inline const ManagedType &typeOf(const T &obj) +{ + return ManagedType::typeOf(obj); +} } #endif /* _OUSIA_MANAGED_TYPE_HPP_ */ -- cgit v1.2.3