PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vartype.h
Go to the documentation of this file.
1 /*
2 * vartype.h
3 *
4 * Interface library for Variable Type class wrapper
5 *
6 * Portable Tools Library
7 *
8 * Copyright (C) 2012 by Vox Lucida Pty. Ltd.
9 *
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.0 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/
14 *
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17 * the License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * The Original Code is Portable Tools Library.
21 *
22 * The Initial Developer of the Original Code is Vox Lucida Pty. Ltd.
23 *
24 * Contributor(s): Robert Jongbloed
25 *
26 * $Revision: 29392 $
27 * $Author: rjongbloed $
28 * $Date: 2013-03-30 16:15:09 +1100 (Sat, 30 Mar 2013) $
29 */
30
31 #ifndef PTLIB_VARTYPE_H
32 #define PTLIB_VARTYPE_H
33
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37
38 #include <ptlib.h>
39 #include <ptclib/guid.h>
40
41
43
48 class PVarType : public PObject
49 {
50 PCLASSINFO(PVarType, PObject)
51 public:
53 enum BasicType {
54 VarNULL,
55 VarBoolean,
56 VarChar,
57 VarInt8,
58 VarInt16,
59 VarInt32,
60 VarInt64,
61 VarUInt8,
62 VarUInt16,
63 VarUInt32,
64 VarUInt64,
65 VarFloatSingle,
66 VarFloatDouble,
67 VarFloatExtended, // aka "long double"
68 VarGUID,
69 VarTime,
70 VarStaticString,
71 VarFixedString,
72 VarDynamicString,
73 VarStaticBinary,
74 VarDynamicBinary
75 };
76
81 PVarType() : m_type(VarNULL) { }
82
85 PVarType(bool value) : m_type(VarBoolean) { m_.boolean = value; }
86
89 PVarType(char value) : m_type(VarChar) { m_.character = value; }
90
93 PVarType(int16_t value) : m_type(VarInt16) { m_.int16 = value; }
94
97 PVarType(int32_t value) : m_type(VarInt32) { m_.int32 = value; }
98
101 PVarType(int64_t value) : m_type(VarInt64) { m_.int64 = value; }
102
105 PVarType(uint8_t value) : m_type(VarUInt8) { m_.uint8 = value; }
106
109 PVarType(uint16_t value) : m_type(VarUInt16) { m_.uint16 = value; }
110
113 PVarType(uint32_t value) : m_type(VarUInt32) { m_.uint32 = value; }
114
117 PVarType(uint64_t value) : m_type(VarUInt64) { m_.uint64 = value; }
118
121 PVarType(float value) : m_type(VarFloatSingle) { m_.floatSingle = value; }
122
125 PVarType(double value) : m_type(VarFloatDouble) { m_.floatDouble = value; }
126
129 PVarType(long double value) : m_type(VarFloatExtended) { m_.floatExtended = value; }
130
133 PVarType(const PGloballyUniqueID & value) : m_type(VarGUID) { memcpy(m_.guid, value, value.GetSize()); }
134
137 PVarType(const PTime & value) : m_type(VarTime) { m_.time.seconds = value.GetTimeInSeconds(); }
138
141 PVarType(const char * value, bool dynamic = false) : m_type(VarNULL) { SetString(value, dynamic); }
142
145 PVarType(const PString & value, bool dynamic = true) : m_type(VarNULL) { SetString(value, dynamic); }
146
149 PVarType(const void * value, PINDEX len, bool dynamic = false) : m_type(VarNULL) { SetBinary(value, len, dynamic); }
150
153 PVarType(const PBYTEArray & value, bool dynamic = true) : m_type(VarNULL) { SetBinary(value, dynamic); }
154
159 PVarType(const PVarType & other) : PObject(other), m_type(VarNULL) { InternalCopy(other); }
160
163 PVarType & operator=(bool value) { if (SetType(VarBoolean)) m_.boolean = value; return *this; }
164
167 PVarType & operator=(char value) { if (SetType(VarChar)) m_.character = value; return *this; }
168
171 PVarType & operator=(int16_t value) { if (SetType(VarInt16)) m_.int16 = value; return *this; }
172
175 PVarType & operator=(int32_t value) { if (SetType(VarInt32)) m_.int32 = value; return *this; }
176
179 PVarType & operator=(int64_t value) { if (SetType(VarInt64)) m_.int64 = value; return *this; }
180
183 PVarType & operator=(uint8_t value) { if (SetType(VarUInt8)) m_.uint8 = value; return *this; }
184
187 PVarType & operator=(uint16_t value) { if (SetType(VarUInt16)) m_.uint16 = value; return *this; }
188
191 PVarType & operator=(uint32_t value) { if (SetType(VarUInt32)) m_.uint32 = value; return *this; }
192
195 PVarType & operator=(uint64_t value) { if (SetType(VarUInt64)) m_.uint64 = value; return *this; }
196
199 PVarType & operator=(float value) { if (SetType(VarFloatSingle)) m_.floatSingle = value; return *this; }
200
203 PVarType & operator=(double value) { if (SetType(VarFloatDouble)) m_.floatDouble = value; return *this; }
204
207 PVarType & operator=(long double value) { if (SetType(VarFloatExtended)) m_.floatExtended = value; return *this; }
208
211 PVarType & operator=(const PGloballyUniqueID & value) { if (SetType(VarGUID)) memcpy(m_.guid, value, value.GetSize()); return *this; }
212
215 PVarType & operator=(const PTime & value) { if (SetType(VarTime)) m_.time.seconds = value.GetTimeInSeconds(); return *this; }
216
220 PVarType & operator=(const char * str) { return SetDynamicString(str); }
221
225 PVarType & operator=(const PString & str) { return SetDynamicString(str); }
226
231 PVarType & operator=(const PVarType & other) { InternalCopy(other); return *this; }
232
234 ~PVarType() { InternalDestroy(); }
236
239 virtual void PrintOn(ostream & strm) const;
240 virtual void ReadFrom(istream & strm);
241 virtual PObject * Clone() const;
243
246
247 BasicType GetType() const { return m_type; }
248
255 virtual bool SetType(BasicType type, PINDEX option = 0);
256
257 bool AsBoolean() const;
258 int AsInteger() const;
259 unsigned AsUnsigned() const;
260 int64_t AsInteger64() const;
261 uint64_t AsUnsigned64() const;
262 double AsFloat() const;
263 PGloballyUniqueID AsGUID() const;
264 PTime AsTime() const;
265 PString AsString() const;
266
267 const void * GetPointer() const;
268 PINDEX GetSize() const;
269
271 virtual PVarType & SetValue(const PString & value);
272
273 virtual PVarType & SetString(const char * value, bool dynamic);
274 PVarType & SetStaticString(const char * value) { return SetString(value, false); }
275 PVarType & SetDynamicString(const char * value) { return SetString(value, true); }
276
277 virtual PVarType & SetBinary(const void * data, PINDEX len, bool dynamic);
278 PVarType & SetBinary(const PBYTEArray & value, bool dynamic) { return SetBinary(value, value.GetSize(), dynamic); }
279 PVarType & SetStaticBinary(const void * data, PINDEX len) { return SetBinary(data, len, false); }
280 PVarType & SetStaticBinary(const PBYTEArray & value) { return SetBinary(value, false); }
281 PVarType & SetDynamicBinary(const void * data, PINDEX len) { return SetBinary(data, len, true); }
282 PVarType & SetDynamicBinary(const PBYTEArray & value) { return SetBinary(value, true); }
284
285 protected:
287 virtual void OnGetValue();
288
290 virtual void OnValueChanged();
291
292 // Internal functions
293 virtual void InternalCopy(const PVarType & other);
294 void InternalDestroy();
295
296 BasicType m_type;
297
298 union Variant {
299 Variant() { memset(this, 0, sizeof(*this)); }
300 bool boolean;
301 char character;
302 int8_t int8;
303 int16_t int16;
304 int32_t int32;
305 int64_t int64;
306 uint8_t uint8;
307 uint16_t uint16;
308 uint32_t uint32;
309 uint64_t uint64;
310 float floatSingle;
311 double floatDouble;
312 long double floatExtended;
313 uint8_t guid[PGloballyUniqueID::Size];
314
315 struct {
316 time_t seconds;
317 PTime::TimeFormat format;
318 } time;
319
320 const char * staticString;
321
322 struct Dynamic {
323 char * Alloc(size_t sz);
324 char * Realloc(size_t sz);
325 void Copy(const Dynamic & other);
326 char * data;
327 size_t size;
328 } dynamic;
329
330 struct {
331 const char * data;
332 size_t size;
333 } staticBinary;
334 } m_;
335 };
336
337
338 #endif // PTLIB_VARTYPE_H
339