blob: 1eda584724bd5574623ffd26e7d034a0d817899f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*
* Copyright (C) 2007-2010 Xagasoft, All rights reserved.
*
* This file is part of the libbu++ library and is released under the
* terms of the license contained in the file LICENSE.
*/
#ifndef BU_VARIANT_H
#define BU_VARIANT_H
#include <bu/fstring.h>
#include <typeinfo>
#include <bu/membuf.h>
#include <bu/formatter.h>
namespace Bu
{
class Formatter;
class Variant;
template<class t> class VariantType;
class VariantTypeRoot
{
public:
VariantTypeRoot();
virtual ~VariantTypeRoot();
virtual Bu::FString toString() const=0;
virtual const std::type_info &getType() const=0;
virtual VariantTypeRoot *clone() const=0;
};
template<class t>
class VariantType : public VariantTypeRoot
{
friend class Variant;
private:
VariantType()
{
}
VariantType( const t &d ) :
data( d )
{
}
VariantType( const VariantType<t> &vt ) :
data( vt.data )
{
}
virtual ~VariantType()
{
}
public:
t &getData()
{
return data;
}
const t &getData() const
{
return data;
}
virtual Bu::FString toString() const
{
MemBuf mb;
Formatter f( mb );
f << data;
return mb.getString();
}
virtual const std::type_info &getType() const
{
return typeid( data );
}
VariantType<t> operator=( const t &rhs )
{
data = rhs;
return *this;
}
virtual VariantTypeRoot *clone() const
{
return new VariantType<t>( *this );
}
private:
t data;
};
class Variant
{
public:
Variant();
Variant( const Variant &v );
virtual ~Variant();
bool isSet();
Bu::FString toString() const;
const std::type_info &getType() const;
Variant &operator=( const Variant &rhs );
template<class t>
Variant &operator=( const t &rhs )
{
if( pCore ) // && pCore->getType() != typeid(t) )
{
delete pCore;
pCore = NULL;
}
pCore = new VariantType<t>();
(*dynamic_cast<VariantType<t> *>(pCore)) = rhs;
return *this;
}
template<class t>
t &get()
{
if( !pCore )
{
throw Bu::ExceptionBase("No data!");
}
if( pCore->getType() != typeid(t) )
{
throw Bu::ExceptionBase("Invalid type conversion.");
}
return dynamic_cast<VariantType<t> *>(pCore)->getData();
}
template<class t>
void set( const t &val )
{
if( pCore && pCore->getType() != typeid(t) )
{
delete pCore;
pCore = NULL;
}
pCore = new VariantType<t>();
(*dynamic_cast<VariantType<t> *>(pCore)) = val;
}
template<class t>
operator t()
{
if( !pCore )
{
throw Bu::ExceptionBase("No data!");
}
if( pCore->getType() != typeid(t) )
{
throw Bu::ExceptionBase("Invalid type conversion.");
}
return dynamic_cast<VariantType<t> *>(pCore)->getData();
}
template<class t>
operator t() const
{
if( !pCore )
{
throw Bu::ExceptionBase("No data!");
}
if( pCore->getType() != typeid(t) )
{
throw Bu::ExceptionBase("Invalid type conversion.");
}
return dynamic_cast<VariantType<t> *>(pCore)->getData();
}
private:
VariantTypeRoot *pCore;
};
template<class t>
Bu::Formatter &operator<<( Bu::Formatter &f, const VariantType<t> &vt )
{
return f << vt.toString();
}
Bu::Formatter &operator<<( Bu::Formatter &f, const Variant &v );
template<> Bu::FString VariantType<int>::toString() const;
template<> Bu::FString VariantType<bool>::toString() const;
};
#endif
|