summaryrefslogtreecommitdiff
path: root/src/variant.h
blob: 80f1bb91ea37dabcaed6253d18b05ed9c0cd5dd6 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * 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/string.h>
#include <typeinfo>
#include <bu/membuf.h>
#include <bu/formatter.h>

namespace Bu
{
	class Formatter;
	class Variant;
	/** @cond DEVEL */
	template<class t> class VariantType;

	class VariantTypeRoot
	{
	public:
		VariantTypeRoot();
		virtual ~VariantTypeRoot();

		virtual Bu::String 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::String 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;
	};
	/** @endcond */

	/**
	 * Store any data type and access it safely.  Variant gives you a way to
	 * pass arbitrary data types around without having to worry about what
	 * type a variable is.  It allows code to be easily extended and to manage
	 * data without having to know what type it is ahead of time.
	 *
	 * Because of the generic method that this class was implemented it may seem
	 * to have some drawbacks compared to other Variant classes you may have
	 * seen, however it is fairly easy to get it to do just about anything you
	 * may need.  It is also very low overhead.  On most compilers the class
	 * itself has only 3 words of overhead + the size of the variable you store
	 * in it.  And, since many parts of it are templatized they can often be
	 * optimized quite a bit.
	 */
	class Variant
	{
	public:
		Variant();
		Variant( const Variant &v );
		Variant( const char *t );
		template<class t>
		Variant( const t &v ) :
			pCore( new VariantType<t>() )
		{
			(*dynamic_cast<VariantType<t> *>(pCore)) = v;
		}
		virtual ~Variant();

		bool isSet() const;
		Bu::String 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>
		t &get() 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();
		}
		
		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>
		bool isType() const
		{
			return pCore->getType() == typeid(t);
		}

		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::String VariantType<int>::toString() const;
	template<> Bu::String VariantType<bool>::toString() const;
};

#endif