PTLib  Version 2.14.3
 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: 31803 $
27  * $Author: rjongbloed $
28  * $Date: 2014-04-29 14:39:48 +1000 (Tue, 29 Apr 2014) $
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 {
51  public:
53  enum BasicType {
67  VarFloatExtended, // aka "long double"
75  };
76 
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);
164 
167  PVarType & operator=(char value);
168 
171  PVarType & operator=(int16_t value);
172 
175  PVarType & operator=(int32_t value);
176 
179  PVarType & operator=(int64_t value);
180 
183  PVarType & operator=(uint8_t value);
184 
187  PVarType & operator=(uint16_t value);
188 
191  PVarType & operator=(uint32_t value);
192 
195  PVarType & operator=(uint64_t value);
196 
199  PVarType & operator=(float value);
200 
203  PVarType & operator=(double value);
204 
207  PVarType & operator=(long double value);
208 
211  PVarType & operator=(const PGloballyUniqueID & value);
212 
215  PVarType & operator=(const PTime & value);
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 
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  template <typename TYPE> TYPE As() const { return AsString(); }
268 #ifndef __GNUC__
269  template <> bool As() const { return AsBoolean(); }
270  template <> int As() const { return AsInteger(); }
271  template <> unsigned As() const { return AsUnsigned(); }
272  template <> int64_t As() const { return AsInteger64(); }
273  template <> uint64_t As() const { return AsUnsigned64(); }
274  template <> double As() const { return AsFloat(); }
275  template <> PGloballyUniqueID As() const { return AsGUID(); }
276  template <> PTime As() const { return AsTime(); }
277  template <> PString As() const { return AsString(); }
278 #endif
279 
280  const void * GetPointer() const;
281  PINDEX GetSize() const;
282 
284  virtual PVarType & SetValue(const PString & value);
285 
286  virtual PVarType & SetString(const char * value, bool dynamic);
287  PVarType & SetStaticString(const char * value) { return SetString(value, false); }
288  PVarType & SetDynamicString(const char * value) { return SetString(value, true); }
289 
290  virtual PVarType & SetBinary(const void * data, PINDEX len, bool dynamic);
291  PVarType & SetBinary(const PBYTEArray & value, bool dynamic) { return SetBinary(value, value.GetSize(), dynamic); }
292  PVarType & SetStaticBinary(const void * data, PINDEX len) { return SetBinary(data, len, false); }
293  PVarType & SetStaticBinary(const PBYTEArray & value) { return SetBinary(value, false); }
294  PVarType & SetDynamicBinary(const void * data, PINDEX len) { return SetBinary(data, len, true); }
295  PVarType & SetDynamicBinary(const PBYTEArray & value) { return SetBinary(value, true); }
297 
298  protected:
300  virtual void OnGetValue();
301 
303  virtual void OnValueChanged();
304 
305  // Internal functions
306  virtual void InternalCopy(const PVarType & other);
307  void InternalDestroy();
308 
310 
311  union Variant {
312  Variant() { memset(this, 0, sizeof(*this)); }
313  bool boolean;
314  char character;
315  int8_t int8;
316  int16_t int16;
317  int32_t int32;
318  int64_t int64;
319  uint8_t uint8;
320  uint16_t uint16;
321  uint32_t uint32;
322  uint64_t uint64;
323  float floatSingle;
324  double floatDouble;
325  long double floatExtended;
327 
328  struct {
329  time_t seconds;
331  } time;
332 
333  const char * staticString;
334 
335  struct Dynamic {
336  char * Alloc(size_t sz);
337  char * Realloc(size_t sz);
338  void Copy(const Dynamic & other);
339  char * data;
340  size_t size;
341  } dynamic;
342 
343  struct {
344  const char * data;
345  size_t size;
346  } staticBinary;
347  } m_;
348 };
349 
350 
351 template <typename TYPE>
352 class PRefVar : public PVarType
353 {
355  public:
356  explicit PRefVar(TYPE & value)
357  : PVarType(value)
358  , m_value(value)
359  {
360  }
361 
362  PRefVar & operator=(const PRefVar & other)
363  {
364  PVarType::operator=(other);
365  return *this;
366  }
367 
368  PRefVar & operator=(TYPE value)
369  {
370  PVarType::operator=(value);
371  return *this;
372  }
373 
374  protected:
375  virtual void OnGetValue()
376  {
377  *reinterpret_cast<TYPE*>(&this->m_) = this->m_value;
378  }
379 
380  virtual void OnValueChanged()
381  {
382  this->m_value = *reinterpret_cast<TYPE*>(&this->m_);
383  }
384 
385  protected:
386  TYPE & m_value;
387 };
388 
389 
390 template <>
392 {
394  public:
395  explicit PRefVar(PGloballyUniqueID & value) : PVarType(value), m_value(value) { }
396  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
397  PRefVar & operator=(const PGloballyUniqueID & value) { PVarType::operator=(value); return *this; }
398 
399  protected:
400  virtual void OnGetValue() { memcpy(this->m_.guid, this->m_value, sizeof(this->m_.guid)); }
401  virtual void OnValueChanged() { memcpy(this->m_value.GetPointer(), this->m_.guid, sizeof(this->m_.guid)); }
402 
403  protected:
405 };
406 
407 
408 template <>
409 class PRefVar <PTime> : public PVarType
410 {
412  public:
413  explicit PRefVar(PTime & value) : PVarType(value), m_value(value) { }
414  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
415  PRefVar & operator=(const PTime & value) { PVarType::operator=(value); return *this; }
416 
417  protected:
418  virtual void OnGetValue() { this->m_.time.seconds = this->m_value.GetTimeInSeconds(); }
419  virtual void OnValueChanged() { this->m_value.SetTimestamp(this->m_.time.seconds); }
420 
421  protected:
423 };
424 
425 
426 template <>
427 class PRefVar <PString> : public PVarType
428 {
430  public:
431  explicit PRefVar(PString & value) : PVarType(value, false), m_value(value) { }
432  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
433  PRefVar & operator=(const PString & value) { SetString(value, false); return *this; }
434 
435  protected:
436  virtual void OnGetValue() { SetString(m_value, false); }
437  virtual void OnValueChanged() { m_value = m_.staticString; }
438 
439  protected:
441 };
442 
443 
444 template <>
445 class PRefVar <PBYTEArray> : public PVarType
446 {
448  public:
449  explicit PRefVar(PBYTEArray & value) : PVarType(value, false) , m_value(value) { }
450  PRefVar & operator=(const PRefVar & other) { PVarType::operator=(other); return *this; }
451  PRefVar & operator=(const PBYTEArray & value) { SetStaticBinary(value); return *this; }
452 
453  protected:
454  virtual void OnGetValue() { SetStaticBinary(m_value); }
455  virtual void OnValueChanged() { m_value = PBYTEArray((const BYTE *)m_.staticBinary.data, m_.staticBinary.size); }
456 
457 protected:
459 };
460 
461 
462 #endif // PTLIB_VARTYPE_H
463