aboutsummaryrefslogtreecommitdiff
path: root/src/stable/variant.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable/variant.cpp')
-rw-r--r--src/stable/variant.cpp99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/stable/variant.cpp b/src/stable/variant.cpp
new file mode 100644
index 0000000..5cdaa5b
--- /dev/null
+++ b/src/stable/variant.cpp
@@ -0,0 +1,99 @@
1/*
2 * Copyright (C) 2007-2011 Xagasoft, All rights reserved.
3 *
4 * This file is part of the libbu++ library and is released under the
5 * terms of the license contained in the file LICENSE.
6 */
7
8#include "bu/variant.h"
9#include "bu/formatter.h"
10#include "bu/membuf.h"
11
12namespace Bu
13{
14 Formatter &operator<<( Formatter &f, const String &s );
15};
16
17Bu::VariantTypeRoot::VariantTypeRoot()
18{
19}
20
21Bu::VariantTypeRoot::~VariantTypeRoot()
22{
23}
24
25Bu::Variant::Variant() :
26 pCore( NULL )
27{
28}
29
30Bu::Variant::Variant( const Variant &v ) :
31 pCore( NULL )
32{
33 if( v.pCore )
34 {
35 pCore = v.pCore->clone();
36 }
37}
38
39Bu::Variant::Variant( const char *t ) :
40 pCore( NULL )
41{
42 set( Bu::String( t ) );
43}
44
45Bu::Variant::~Variant()
46{
47 if( pCore )
48 {
49 delete pCore;
50 pCore = NULL;
51 }
52}
53
54Bu::String Bu::Variant::toString() const
55{
56 Bu::MemBuf mb;
57 Bu::Formatter f( mb );
58 f << *this;
59 return mb.getString();
60}
61
62bool Bu::Variant::isSet() const
63{
64 return pCore != NULL;
65}
66
67const std::type_info &Bu::Variant::getType() const
68{
69 if( !pCore )
70 {
71 throw Bu::ExceptionBase("No data!");
72 }
73 return pCore->getType();
74}
75
76Bu::Variant &Bu::Variant::operator=( const Bu::Variant &rhs )
77{
78 if( pCore )
79 {
80 delete pCore;
81 pCore = NULL;
82 }
83 if( rhs.pCore )
84 {
85 pCore = rhs.pCore->clone();
86 }
87
88 return *this;
89}
90
91Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::Variant &v )
92{
93 if( !v.pCore )
94 return f << "(null)";
95
96 v.pCore->format( f );
97 return f;
98}
99