From e6a7bbd753cba97f7bda154341d93d3f1241d2c4 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sun, 11 Jan 2015 13:52:03 +0100 Subject: Added getRttiType function to the Variant class --- src/core/common/Variant.cpp | 31 +++++++++++++++++++++++++++++++ src/core/common/Variant.hpp | 5 ++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/core/common/Variant.cpp b/src/core/common/Variant.cpp index c5c7b80..44a77d8 100644 --- a/src/core/common/Variant.cpp +++ b/src/core/common/Variant.cpp @@ -25,6 +25,7 @@ #include "Variant.hpp" #include "VariantConverter.hpp" #include "VariantWriter.hpp" +#include "Rtti.hpp" namespace ousia { @@ -69,6 +70,8 @@ const char *Variant::getTypeName(VariantType type) return "unknown"; } +/* Conversion functions */ + Variant::boolType Variant::toBool() const { ExceptionLogger logger; @@ -101,6 +104,34 @@ Variant::stringType Variant::toString(bool escape) const return res.asString(); } +/* Type management */ + +const RttiType& Variant::getRttiType() const +{ + switch (type) { + case VariantType::NULLPTR: + return RttiTypes::Nullptr; + case VariantType::BOOL: + return RttiTypes::Bool; + case VariantType::INT: + return RttiTypes::Int; + case VariantType::DOUBLE: + return RttiTypes::Double; + case VariantType::STRING: + case VariantType::MAGIC: + return RttiTypes::String; + case VariantType::ARRAY: + return RttiTypes::Array; + case VariantType::MAP: + return RttiTypes::Map; + case VariantType::FUNCTION: + return RttiTypes::Function; + case VariantType::OBJECT: + return asObject()->type(); + } + return RttiTypes::None; +} + /* Output stream operators */ std::ostream &operator<<(std::ostream &os, const Variant &v) diff --git a/src/core/common/Variant.hpp b/src/core/common/Variant.hpp index 98ee49b..da88449 100644 --- a/src/core/common/Variant.hpp +++ b/src/core/common/Variant.hpp @@ -859,8 +859,11 @@ public: * Returns the current Rtti type descriptor of the Variant. * * @return the Rtti type descriptor. Either one of RttiTypes::Int, - * RttiTypes::Bool, RttiTypes::Double, RttiTypes:: + * RttiTypes::Bool, RttiTypes::Double, RttiTypes::String, RttiTypes::Array + * or RttiTypes::Function or -- in case an object is stored inside the + * variant -- the RttiType of that object. */ + const RttiType& getRttiType() const; /** * Returns the name of the given variant type as C-style string. -- cgit v1.2.3