aboutsummaryrefslogtreecommitdiff
path: root/src/variant.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-09-25 22:14:26 +0000
committerMike Buland <eichlan@xagasoft.com>2009-09-25 22:14:26 +0000
commite99509abde688315ac7a82d764b352e2e3312e61 (patch)
tree9dc82509b89d96f138eefe07674319fc987afb6a /src/variant.cpp
parent1b7caf3368675eb6825e0dca77782a141dfa0392 (diff)
downloadlibbu++-e99509abde688315ac7a82d764b352e2e3312e61.tar.gz
libbu++-e99509abde688315ac7a82d764b352e2e3312e61.tar.bz2
libbu++-e99509abde688315ac7a82d764b352e2e3312e61.tar.xz
libbu++-e99509abde688315ac7a82d764b352e2e3312e61.zip
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.
Diffstat (limited to 'src/variant.cpp')
-rw-r--r--src/variant.cpp77
1 files changed, 77 insertions, 0 deletions
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 @@
1#include "bu/variant.h"
2
3namespace Bu
4{
5 Formatter &operator<<( Formatter &f, const FString &s );
6};
7
8Bu::VariantTypeRoot::VariantTypeRoot()
9{
10}
11
12Bu::VariantTypeRoot::~VariantTypeRoot()
13{
14}
15
16Bu::Variant::Variant() :
17 pCore( NULL )
18{
19}
20
21Bu::Variant::~Variant()
22{
23}
24
25bool Bu::Variant::isSet()
26{
27 return pCore != NULL;
28}
29
30Bu::FString Bu::Variant::toString() const
31{
32 if( !pCore )
33 return "***NO DATA***";
34 return pCore->toString();
35}
36
37const std::type_info &Bu::Variant::getType() const
38{
39 if( !pCore )
40 {
41 throw Bu::ExceptionBase("No data!");
42 }
43 return pCore->getType();
44}
45
46Bu::Variant &Bu::Variant::operator=( const Bu::Variant &rhs )
47{
48 if( pCore )
49 {
50 delete pCore;
51 pCore = NULL;
52 }
53 if( rhs.pCore )
54 {
55 pCore = rhs.pCore->clone();
56 }
57
58 return *this;
59}
60
61Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v )
62{
63 return f << v.toString();
64}
65
66template<> Bu::FString Bu::VariantType<int>::toString() const
67{
68 Bu::FString s;
69 s.format("%d", data );
70 return s;
71}
72
73template<> Bu::FString Bu::VariantType<bool>::toString() const
74{
75 return data?"true":"false";
76}
77