tue_config
include
tue
config
variant.h
Go to the documentation of this file.
1
#ifndef TUE_CONFIG_VARIANT_H_
2
#define TUE_CONFIG_VARIANT_H_
3
4
// Directly taken from http://stackoverflow.com/questions/5319216/implementing-a-variant-class
5
6
#include <boost/shared_ptr.hpp>
7
8
#include <
string
>
9
#include <
iostream
>
10
11
namespace
tue
12
{
13
namespace
config
14
{
15
16
//template <typename T>
17
//struct TypeWrapper
18
//{
19
// typedef T TYPE;
20
// typedef const T CONSTTYPE;
21
// typedef T& REFTYPE;
22
// typedef const T& CONSTREFTYPE;
23
//};
24
25
//template <typename T>
26
//struct TypeWrapper<const T>
27
//{
28
// typedef T TYPE;
29
// typedef const T CONSTTYPE;
30
// typedef T& REFTYPE;
31
// typedef const T& CONSTREFTYPE;
32
//};
33
34
//template <typename T>
35
//struct TypeWrapper<const T&>
36
//{
37
// typedef T TYPE;
38
// typedef const T CONSTTYPE;
39
// typedef T& REFTYPE;
40
// typedef const T& CONSTREFTYPE;
41
//};
42
43
//template <typename T>
44
//struct TypeWrapper<T&>
45
//{
46
// typedef T TYPE;
47
// typedef const T CONSTTYPE;
48
// typedef T& REFTYPE;
49
// typedef const T& CONSTREFTYPE;
50
//};
51
52
//class Variant2
53
//{
54
//public:
55
// Variant2() { }
56
57
// template<class T>
58
// Variant2(T inValue) :
59
// mImpl(new VariantImpl<typename TypeWrapper<T>::TYPE>(inValue))
60
// {
61
// }
62
63
// template<class T>
64
// typename TypeWrapper<T>::REFTYPE getValue()
65
// {
66
// return dynamic_cast<VariantImpl<typename TypeWrapper<T>::TYPE>&>(*mImpl.get()).mValue;
67
// }
68
69
// template<class T>
70
// typename TypeWrapper<T>::CONSTREFTYPE getValue() const
71
// {
72
// return dynamic_cast<VariantImpl<typename TypeWrapper<T>::TYPE>&>(*mImpl.get()).mValue;
73
// }
74
75
// template<class T>
76
// void setValue(typename TypeWrapper<T>::CONSTREFTYPE inValue)
77
// {
78
// mImpl.reset(new VariantImpl<typename TypeWrapper<T>::TYPE>(inValue));
79
// }
80
81
// friend std::ostream& operator<< (std::ostream& out, const Variant2& v)
82
// {
83
// v.mImpl->print(out);
84
// return out;
85
// }
86
87
88
//private:
89
// struct AbstractVariantImpl
90
// {
91
// virtual ~AbstractVariantImpl() {}
92
93
// virtual void print(std::ostream& out) const {}
94
// };
95
96
// template<class T>
97
// struct VariantImpl : public AbstractVariantImpl
98
// {
99
// VariantImpl(T inValue) : mValue(inValue) { }
100
101
// ~VariantImpl() {}
102
103
// T mValue;
104
105
// void print(std::ostream& out) const { out << mValue; }
106
// };
107
108
// boost::shared_ptr<AbstractVariantImpl> mImpl;
109
//};
110
111
class
Variant
112
{
113
114
public
:
115
116
Variant
() :
type_
(
'?'
) {}
117
118
Variant
(
const
bool
& b) :
type_
(
'b'
),
b_
(b) {}
119
Variant
(
const
double
& d) :
type_
(
'd'
),
d_
(d) {}
120
Variant
(
int
i) :
type_
(
'i'
),
i_
(i) {}
121
Variant
(
const
std::string
& s) :
type_
(
's'
),
s_
(s) {}
122
Variant
(
const
char
* s) :
type_
(
's'
),
s_
(s) {}
123
124
bool
getValue
(
int
& v) {
return
checkAndGet
(
i_
,
'i'
, v) ||
checkAndGet
((
int
)
b_
,
'b'
, v); }
125
bool
getValue
(
double
& v) {
return
checkAndGet
(
d_
,
'd'
, v) ||
checkAndGet
((
double
)
i_
,
'i'
, v); }
126
bool
getValue
(
float
& v) {
return
checkAndGet
((
float
)
d_
,
'd'
, v) ||
checkAndGet
((
float
)
i_
,
'i'
, v); }
127
bool
getValue
(
std::string
& v) {
return
checkAndGet
(
s_
,
's'
, v); }
128
129
bool
getValue
(
bool
& v)
130
{
131
if
(
checkAndGet
(
b_
,
'b'
, v))
132
return
true
;
133
int
i;
134
if
(!
checkAndGet
(
i_
,
'i'
, i))
135
return
false
;
136
v = (i == 1);
137
return
true
;
138
}
139
140
bool
isString
()
const
{
return
type_
==
's'
; }
141
bool
isInt
()
const
{
return
type_
==
'i'
; }
142
bool
isDouble
()
const
{
return
type_
==
'i'
||
type_
==
'd'
; }
143
bool
isBoolean
()
const
{
return
type_
==
'b'
; }
144
145
bool
inline
valid
()
const
{
return
type_
!=
'?'
; }
146
147
private
:
148
149
char
type_
;
150
151
union
{
152
int
i_
;
153
double
d_
;
154
bool
b_
;
155
};
156
157
std::string
s_
;
158
159
template
<
typename
T>
160
inline
bool
checkAndGet
(
const
T& v,
char
type, T& out)
161
{
162
if
(type !=
type_
)
163
return
false
;
164
out = v;
165
return
true
;
166
}
167
168
friend
std::ostream
&
operator<<
(
std::ostream
& out,
const
Variant
& v)
169
{
170
switch
(v.
type_
)
171
{
172
case
'i'
: out << v.
i_
;
173
break
;
174
case
'd'
: out << v.
d_
;
175
break
;
176
case
's'
: out << v.
s_
;
177
break
;
178
case
'b'
: out << v.
b_
;
179
break
;
180
default
: out <<
"?"
;
181
break
;
182
}
183
184
return
out;
185
}
186
187
};
188
189
}
// end namespace tue
190
191
}
// end namespace config
192
193
#endif
tue::config::Variant::getValue
bool getValue(bool &v)
Definition:
variant.h:129
tue::config::Variant::isBoolean
bool isBoolean() const
Definition:
variant.h:143
std::string
tue::config::Variant::getValue
bool getValue(float &v)
Definition:
variant.h:126
iostream
tue::config::Variant::isInt
bool isInt() const
Definition:
variant.h:141
tue::config::Variant::Variant
Variant()
Definition:
variant.h:116
tue::config::Variant::Variant
Variant(const std::string &s)
Definition:
variant.h:121
tue::config::Variant::d_
double d_
Definition:
variant.h:153
std::ostream
tue::config::Variant::getValue
bool getValue(double &v)
Definition:
variant.h:125
tue::config::Variant::isString
bool isString() const
Definition:
variant.h:140
tue::config::Variant::isDouble
bool isDouble() const
Definition:
variant.h:142
tue::config::Variant::s_
std::string s_
Definition:
variant.h:157
tue::config::Variant::operator<<
friend std::ostream & operator<<(std::ostream &out, const Variant &v)
Definition:
variant.h:168
tue::config::Variant::Variant
Variant(int i)
Definition:
variant.h:120
tue::config::Variant::Variant
Variant(const char *s)
Definition:
variant.h:122
tue::config::Variant::getValue
bool getValue(std::string &v)
Definition:
variant.h:127
tue::config::Variant::i_
int i_
Definition:
variant.h:152
tue::config::Variant::valid
bool valid() const
Definition:
variant.h:145
tue::config::Variant::type_
char type_
Definition:
variant.h:149
tue::config::Variant::getValue
bool getValue(int &v)
Definition:
variant.h:124
tue::config::Variant::b_
bool b_
Definition:
variant.h:154
tue
tue::config::Variant::Variant
Variant(const bool &b)
Definition:
variant.h:118
tue::config::Variant
Definition:
variant.h:111
tue::config::Variant::Variant
Variant(const double &d)
Definition:
variant.h:119
tue::config::Variant::checkAndGet
bool checkAndGet(const T &v, char type, T &out)
Definition:
variant.h:160
config
tue::config::ReaderWriter config
Definition:
sdf_gtest.cpp:9
string
Generated on Sun Feb 23 2025 04:34:23 for tue_config by
1.8.17