PTLib  Version 2.18.8
 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 
27 #ifndef PTLIB_VARTYPE_H
28 #define PTLIB_VARTYPE_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <ptlib.h>
35 
36 #if P_VARTYPE
37 
38 #include <ptclib/guid.h>
39 
40 
42 
47 class PVarType : public PObject
48 {
49  PCLASSINFO(PVarType, PObject)
50  public:
52  enum BasicType {
53  VarNULL,
54  VarBoolean,
55  VarChar,
56  VarInt8,
57  VarInt16,
58  VarInt32,
59  VarInt64,
60  VarUInt8,
61  VarUInt16,
62  VarUInt32,
63  VarUInt64,
64  VarFloatSingle,
65  VarFloatDouble,
66  VarFloatExtended, // aka "long double"
67  VarGUID,
68  VarTime,
69  VarStaticString,
70  VarFixedString,
71  VarDynamicString,
72  VarStaticBinary,
73  VarDynamicBinary
74  };
75 
80  PVarType() : m_type(VarNULL) { }
81 
84  PVarType(bool value) : m_type(VarBoolean) { m_.boolean = value; }
85 
88  PVarType(char value) : m_type(VarChar) { m_.character = value; }
89 
92  PVarType(int16_t value) : m_type(VarInt16) { m_.int16 = value; }
93 
96  PVarType(int32_t value) : m_type(VarInt32) { m_.int32 = value; }
97 
100  PVarType(int64_t value) : m_type(VarInt64) { m_.int64 = value; }
101 
104  PVarType(uint8_t value) : m_type(VarUInt8) { m_.uint8 = value; }
105 
108  PVarType(uint16_t value) : m_type(VarUInt16) { m_.uint16 = value; }
109 
112  PVarType(uint32_t value) : m_type(VarUInt32) { m_.uint32 = value; }
113 
116  PVarType(uint64_t value) : m_type(VarUInt64) { m_.uint64 = value; }
117 
120  PVarType(float value) : m_type(VarFloatSingle) { m_.floatSingle = value; }
121 
124  PVarType(double value) : m_type(VarFloatDouble) { m_.floatDouble = value; }
125 
128  PVarType(long double value) : m_type(VarFloatExtended) { m_.floatExtended = value; }
129 
132  PVarType(const PGloballyUniqueID & value) : m_type(VarGUID) { memcpy(m_.guid, value, value.GetSize()); }
133 
136  PVarType(const PTime & value) : m_type(VarTime) { m_.time.seconds = value.GetTimeInSeconds(); }
137 
140  PVarType(const char * value, bool dynamic = false) : m_type(VarNULL) { SetString(value, dynamic); }
141 
144  PVarType(const PString & value, bool dynamic = true) : m_type(VarNULL) { SetString(value, dynamic); }
145 
148  PVarType(const void * value, PINDEX len, bool dynamic = false) : m_type(VarNULL) { SetBinary(value, len, dynamic); }
149 
152  PVarType(const PBYTEArray & value, bool dynamic = true) : m_type(VarNULL) { SetBinary(value, dynamic); }
153 
158  PVarType(const PVarType & other) : PObject(other), m_type(VarNULL) { InternalCopy(other); }
159 
162  PVarType & operator=(bool value);
163 
166  PVarType & operator=(char value);
167 
170  PVarType & operator=(int16_t value);
171 
174  PVarType & operator=(int32_t value);
175 
178  PVarType & operator=(int64_t value);
179 
182  PVarType & operator=(uint8_t value);
183 
186  PVarType & operator=(uint16_t value);
187 
190  PVarType & operator=(uint32_t value);
191 
194  PVarType & operator=(uint64_t value);
195 
198  PVarType & operator=(float value);
199 
202  PVarType & operator=(double value);
203 
206  PVarType & operator=(long double value);
207 
210  PVarType & operator=(const PGloballyUniqueID & value);
211 
214  PVarType & operator=(const PTime & value);
215 
219  PVarType & operator=(const char * str) { return SetDynamicString(str); }
220 
224  PVarType & operator=(const PString & str) { return SetDynamicString(str); }
225 
230  PVarType & operator=(const PVarType & other) { InternalCopy(other); return *this; }
231 
233  ~PVarType() { InternalDestroy(); }
235 
238  virtual void PrintOn(ostream & strm) const;
239  virtual void ReadFrom(istream & strm);
240  virtual PObject * Clone() const;
242 
245  BasicType GetType() const { return m_type; }
247 
254  virtual bool SetType(BasicType type, PINDEX option = 0);
255 
256  bool AsBoolean() const;
257  int AsInteger() const;
258  unsigned AsUnsigned() const;
259  int64_t AsInteger64() const;
260  uint64_t AsUnsigned64() const;
261  double AsFloat() const;
262  PGloballyUniqueID AsGUID() const;
263  PTime AsTime() const;
264  PString AsString() const;
265 
266  template <typename TYPE> TYPE As() const { return AsString(); }
267 #ifndef __GNUC__
268  template <> bool As() const { return AsBoolean(); }
269  template <> int As() const { return AsInteger(); }
270  template <> unsigned As() const { return AsUnsigned(); }
271  template <> int64_t As() const { return AsInteger64(); }
272  template <> uint64_t As() const { return AsUnsigned64(); }
273  template <> double As() const { return AsFloat(); }
274  template <> PGloballyUniqueID As() const { return AsGUID(); }
275  template <> PTime As() const { return AsTime(); }
276  template <> PString As() const { return AsString(); }
277 #endif
278 
279  const void * GetPointer() const;
280  PINDEX GetSize() const;
281 
283  virtual PVarType & SetValue(const PString & value);
284 
285  virtual PVarType & SetString(const char * value, bool dynamic);
286  PVarType & SetStaticString(const char * value) { return SetString(value, false); }
287  PVarType & SetDynamicString(const char * value) { return SetString(value, true); }
288 
289  virtual PVarType & SetBinary(const void * data, PINDEX len, bool dynamic);
290  PVarType & SetBinary(const PBYTEArray & value, bool dynamic) { return SetBinary(value, value.GetSize(), dynamic); }
291  PVarType & SetStaticBinary(const void * data, PINDEX len) { return SetBinary(data, len, false); }
292  PVarType & SetStaticBinary(const PBYTEArray & value) { return SetBinary(value, false); }
293  PVarType & SetDynamicBinary(const void * data, PINDEX len) { return SetBinary(data, len, true); }
294  PVarType & SetDynamicBinary(const PBYTEArray & value) { return SetBinary(value, true); }
296 
297  protected:
299  virtual void OnGetValue();
300 
302  virtual void OnValueChanged();
303 
304  // Internal functions
305  virtual void InternalCopy(const PVarType & other);
306  void InternalDestroy();
307 
308  BasicType m_type;
309 
310  union Variant {
311  Variant() { memset(this, 0, sizeof(*this)); }
312  bool boolean;
313  char character;
314  int8_t int8;
315  int16_t int16;
316  int32_t int32;
317  int64_t int64;
318  uint8_t uint8;
319  uint16_t uint16;
320  uint32_t uint32;
321  uint64_t uint64;
322  float floatSingle;
323  double floatDouble;
324  long double floatExtended;
325  uint8_t guid[PGloballyUniqueID::Size];
326 
327  struct {
328  time_t seconds;
329  PTime::TimeFormat format;
330  } time;
331 
332  const char * staticString;
333 
334  struct Dynamic {
335  char * Alloc(size_t sz);
336  char * Realloc(size_t sz);
337  void Copy(const Dynamic & other);
338  char * data;
339  size_t size;
340  } dynamic;
341 
342  struct {
343  const char * data;
344  size_t size;
345  } staticBinary;
346  } m_;
347 };
348 
349 
350 template <typename TYPE>
351 class PRefVar : public PVarType
352 {
353  PCLASSINFO_WITH_CLONE(PRefVar, PVarType)
354  public:
355  explicit PRefVar(TYPE & value)
356  : PVarType(value)
357  , m_value(value)
358  {
359  }
360 
361  PRefVar & operator=(const PRefVar & other)
362  {
363  PVarType::operator=(other);
364  return *this;
365  }
366 
367  PRefVar & operator=(TYPE value)
368  {
369  PVarType::operator=(value);
370  return *this;
371  }
372 
373  protected:
374  virtual void OnGetValue()
375  {
376  *reinterpret_cast<TYPE*>(&this->m_) = this->m_value;
377  }
378 
379  virtual void OnValueChanged()
380  {
381  this->m_value = *reinterpret_cast<TYPE*>(&this->m_);
382  }
383 
384  protected:
385  TYPE & m_value;
386 };
387 
388 
389 template <>
390 class PRefVar <PGloballyUniqueID> : public PVarType
391 {
392  PCLASSINFO_WITH_CLONE(PRefVar, PVarType)
393  public:
394  explicit PRefVar(PGloballyUniqueID & value) : PVarType(value), m_value(value) { }
395  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
396  PRefVar & operator=(const PGloballyUniqueID & value) { PVarType::operator=(value); return *this; }
397 
398  protected:
399  virtual void OnGetValue() { memcpy(this->m_.guid, this->m_value, sizeof(this->m_.guid)); }
400  virtual void OnValueChanged() { memcpy(this->m_value.GetPointer(), this->m_.guid, sizeof(this->m_.guid)); }
401 
402  protected:
403  PGloballyUniqueID & m_value;
404 };
405 
406 
407 template <>
408 class PRefVar <PTime> : public PVarType
409 {
410  PCLASSINFO_WITH_CLONE(PRefVar, PVarType)
411  public:
412  explicit PRefVar(PTime & value) : PVarType(value), m_value(value) { }
413  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
414  PRefVar & operator=(const PTime & value) { PVarType::operator=(value); return *this; }
415 
416  protected:
417  virtual void OnGetValue() { this->m_.time.seconds = this->m_value.GetTimeInSeconds(); }
418  virtual void OnValueChanged() { this->m_value.SetTimestamp(this->m_.time.seconds); }
419 
420  protected:
421  PTime & m_value;
422 };
423 
424 
425 template <>
426 class PRefVar <PString> : public PVarType
427 {
428  PCLASSINFO_WITH_CLONE(PRefVar, PVarType)
429  public:
430  explicit PRefVar(PString & value) : PVarType(value, false), m_value(value) { }
431  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
432  PRefVar & operator=(const PString & value) { SetString(value, false); return *this; }
433 
434  protected:
435  virtual void OnGetValue() { SetString(m_value, false); }
436  virtual void OnValueChanged() { m_value = m_.staticString; }
437 
438  protected:
439  PString & m_value;
440 };
441 
442 
443 template <>
444 class PRefVar <PBYTEArray> : public PVarType
445 {
446  PCLASSINFO_WITH_CLONE(PRefVar, PVarType)
447  public:
448  explicit PRefVar(PBYTEArray & value) : PVarType(value, false) , m_value(value) { }
449  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
450  PRefVar & operator=(const PBYTEArray & value) { SetStaticBinary(value); return *this; }
451 
452  protected:
453  virtual void OnGetValue() { SetStaticBinary(m_value); }
454  virtual void OnValueChanged() { m_value = PBYTEArray((const BYTE *)m_.staticBinary.data, m_.staticBinary.size); }
455 
456 protected:
457  PBYTEArray & m_value;
458 };
459 
460 #endif // P_VARTYPE
461 
462 #endif // PTLIB_VARTYPE_H
463 
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
This class defines an absolute time and date.
Definition: ptime.h:49
time_t GetTimeInSeconds() const
Get the total seconds since the epoch.
virtual void ReadFrom(istream &strm)
Input the contents of the object from the stream.
TimeFormat
Standard time formats for string representations of a time and date.
Definition: ptime.h:433
Array of unsigned characters.
Definition: array.h:605
virtual PINDEX GetSize() const
Get the current size of the container.
The character string class.
Definition: pstring.h:108
#define PCLASSINFO_WITH_CLONE(cls, par)
Declare all the standard PTLib class information, plus Clone().
Definition: object.h:2167
virtual PObject * Clone() const
Create a copy of the class on the heap.
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
virtual void PrintOn(ostream &strm) const
Output the contents of the object to the stream.