27 #ifndef PTLIB_VARTYPE_H
28 #define PTLIB_VARTYPE_H
80 PVarType() : m_type(VarNULL) { }
84 PVarType(
bool value) : m_type(VarBoolean) { m_.boolean = value; }
88 PVarType(
char value) : m_type(VarChar) { m_.character = value; }
92 PVarType(int16_t value) : m_type(VarInt16) { m_.int16 = value; }
96 PVarType(int32_t value) : m_type(VarInt32) { m_.int32 = value; }
100 PVarType(int64_t value) : m_type(VarInt64) { m_.int64 = value; }
104 PVarType(uint8_t value) : m_type(VarUInt8) { m_.uint8 = value; }
108 PVarType(uint16_t value) : m_type(VarUInt16) { m_.uint16 = value; }
112 PVarType(uint32_t value) : m_type(VarUInt32) { m_.uint32 = value; }
116 PVarType(uint64_t value) : m_type(VarUInt64) { m_.uint64 = value; }
120 PVarType(
float value) : m_type(VarFloatSingle) { m_.floatSingle = value; }
124 PVarType(
double value) : m_type(VarFloatDouble) { m_.floatDouble = value; }
128 PVarType(
long double value) : m_type(VarFloatExtended) { m_.floatExtended = value; }
132 PVarType(
const PGloballyUniqueID & value) : m_type(VarGUID) { memcpy(m_.guid, value, value.GetSize()); }
140 PVarType(
const char * value,
bool dynamic =
false) : m_type(VarNULL) { SetString(value, dynamic); }
144 PVarType(
const PString & value,
bool dynamic =
true) : m_type(VarNULL) { SetString(value, dynamic); }
148 PVarType(
const void * value, PINDEX len,
bool dynamic =
false) : m_type(VarNULL) { SetBinary(value, len, dynamic); }
152 PVarType(
const PBYTEArray & value,
bool dynamic =
true) : m_type(VarNULL) { SetBinary(value, dynamic); }
158 PVarType(
const PVarType & other) :
PObject(other), m_type(VarNULL) { InternalCopy(other); }
162 PVarType & operator=(
bool value);
166 PVarType & operator=(
char value);
170 PVarType & operator=(int16_t value);
174 PVarType & operator=(int32_t value);
178 PVarType & operator=(int64_t value);
182 PVarType & operator=(uint8_t value);
186 PVarType & operator=(uint16_t value);
190 PVarType & operator=(uint32_t value);
194 PVarType & operator=(uint64_t value);
198 PVarType & operator=(
float value);
202 PVarType & operator=(
double value);
206 PVarType & operator=(
long double value);
210 PVarType & operator=(
const PGloballyUniqueID & value);
214 PVarType & operator=(
const PTime & value);
219 PVarType & operator=(
const char * str) {
return SetDynamicString(str); }
224 PVarType & operator=(
const PString & str) {
return SetDynamicString(str); }
230 PVarType & operator=(
const PVarType & other) { InternalCopy(other);
return *
this; }
233 ~PVarType() { InternalDestroy(); }
238 virtual void PrintOn(ostream & strm)
const;
239 virtual void ReadFrom(istream & strm);
245 BasicType GetType()
const {
return m_type; }
254 virtual bool SetType(BasicType type, PINDEX option = 0);
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;
266 template <
typename TYPE> TYPE As()
const {
return AsString(); }
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(); }
279 const void * GetPointer()
const;
280 PINDEX GetSize()
const;
283 virtual PVarType & SetValue(
const PString & value);
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); }
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); }
299 virtual void OnGetValue();
302 virtual void OnValueChanged();
305 virtual void InternalCopy(
const PVarType & other);
306 void InternalDestroy();
311 Variant() { memset(
this, 0,
sizeof(*
this)); }
324 long double floatExtended;
325 uint8_t guid[PGloballyUniqueID::Size];
332 const char * staticString;
335 char * Alloc(
size_t sz);
336 char * Realloc(
size_t sz);
337 void Copy(
const Dynamic & other);
350 template <
typename TYPE>
351 class PRefVar :
public PVarType
355 explicit PRefVar(TYPE & value)
361 PRefVar & operator=(
const PRefVar & other)
363 PVarType::operator=(other);
367 PRefVar & operator=(TYPE value)
369 PVarType::operator=(value);
374 virtual void OnGetValue()
376 *
reinterpret_cast<TYPE*
>(&this->m_) = this->m_value;
379 virtual void OnValueChanged()
381 this->m_value = *
reinterpret_cast<TYPE*
>(&this->m_);
390 class PRefVar <PGloballyUniqueID> :
public PVarType
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; }
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)); }
403 PGloballyUniqueID & m_value;
408 class PRefVar <
PTime> :
public PVarType
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; }
417 virtual void OnGetValue() { this->m_.time.seconds = this->m_value.GetTimeInSeconds(); }
418 virtual void OnValueChanged() { this->m_value.SetTimestamp(this->m_.time.seconds); }
426 class PRefVar <
PString> :
public PVarType
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; }
435 virtual void OnGetValue() { SetString(m_value,
false); }
436 virtual void OnValueChanged() { m_value = m_.staticString; }
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; }
453 virtual void OnGetValue() { SetStaticBinary(m_value); }
454 virtual void OnValueChanged() { m_value =
PBYTEArray((
const BYTE *)m_.staticBinary.data, m_.staticBinary.size); }
462 #endif // PTLIB_VARTYPE_H
#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.