From e99509abde688315ac7a82d764b352e2e3312e61 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 25 Sep 2009 22:14:26 +0000 Subject: New Bu::Variant class. Store anything in it, get it out again, find out it's type. It's really just that easy. More info, docs, and tweaks to come. --- src/variant.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/variant.cpp (limited to 'src/variant.cpp') diff --git a/src/variant.cpp b/src/variant.cpp new file mode 100644 index 0000000..95eea88 --- /dev/null +++ b/src/variant.cpp @@ -0,0 +1,77 @@ +#include "bu/variant.h" + +namespace Bu +{ + Formatter &operator<<( Formatter &f, const FString &s ); +}; + +Bu::VariantTypeRoot::VariantTypeRoot() +{ +} + +Bu::VariantTypeRoot::~VariantTypeRoot() +{ +} + +Bu::Variant::Variant() : + pCore( NULL ) +{ +} + +Bu::Variant::~Variant() +{ +} + +bool Bu::Variant::isSet() +{ + return pCore != NULL; +} + +Bu::FString Bu::Variant::toString() const +{ + if( !pCore ) + return "***NO DATA***"; + return pCore->toString(); +} + +const std::type_info &Bu::Variant::getType() const +{ + if( !pCore ) + { + throw Bu::ExceptionBase("No data!"); + } + return pCore->getType(); +} + +Bu::Variant &Bu::Variant::operator=( const Bu::Variant &rhs ) +{ + if( pCore ) + { + delete pCore; + pCore = NULL; + } + if( rhs.pCore ) + { + pCore = rhs.pCore->clone(); + } + + return *this; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v ) +{ + return f << v.toString(); +} + +template<> Bu::FString Bu::VariantType::toString() const +{ + Bu::FString s; + s.format("%d", data ); + return s; +} + +template<> Bu::FString Bu::VariantType::toString() const +{ + return data?"true":"false"; +} + -- cgit v1.2.3