classPiiVariant
#include <PiiVariant.h>
An extensible variant class that can store any data type.
Description
A type ID is used in determining the type of the stored object. To check the type of a variant, do the following:
PiiVariant variant(3); if (variant.type() == PiiVariant::IntType) int i = variant.valueAs<int>();
Any type can be made compatible with PiiVariant. For this, one needs to first choose a unique variant type ID for the type. The mechanism differs from QVariant's type registration technique 1) for run-time performance reasons and 2) to ensure a static type id across all computing environments. Use the PII_DECLARE_VARIANT_TYPE macro to assign a type ID to a type. The new type must then be registered by the PII_REGISTER_VARIANT_TYPE macro.
// MyClass.h class MyClass {}; // First do whatever it takes to make the class itself // serializable. In the simplest case just this: #define PII_SERIALIZABLE_CLASS MyClass #include <PiiSerializableRegistration.h> PII_DECLARE_VARIANT_TYPE(MyClass, 0x666); // In MyClass.cc #include "MyClass.h" PII_REGISTER_VARIANT_TYPE(MyClass);
Classes
| struct | |
| struct |
Public types
|
enum
|
{ CharType = 0x00, ShortType, IntType, Int64Type, UnsignedCharType
= 0x08, UnsignedShortType, UnsignedIntType, UnsignedInt64Type,
FloatType = 0x10, DoubleType, BoolType = 0x18, VoidPtrType = 0x19,
LastPrimitiveType = VoidPtrType, InvalidType = 0xffffffff }
An enumeration for primitive types. |
Constructors and destructor
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
(
|
|
|
( )
Creates an invalid variant. |
|
|
(
|
|
|
(
|
|
|
Creates a copy of other. |
|
|
template<class T>
|
(
Creates a variant that stores value. |
|
template<class T>
|
Creates a variant with a non-default type ID. |
|
(
|
|
|
( )
Destroys the variant. |
Public member functions
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
bool
|
( )
Returns |
|
Copies the contents of other to |
|
|
template<class T>
|
(
Returns the value of the variant as a |
|
unsigned int
|
( )
Get the type of the variant. |
|
template<class T>
T &
|
( )
|
Static public member functions
|
static bool
|
(
Returns |
|
static bool
|
(
Returns |
|
static bool
|
(
Returns |
|
static bool
|
(
Returns |
|
static bool
|
(
Returns |
Friends
|
friend struct
|
|
Enumeration details
-
enum PrimitiveType
An enumeration for primitive types.
The names correspond to primitive C++ types.
InvalidType(0xffffffff) indicates an unknown type. The type IDs are composed so that their categories can be quickly determined by bit masking. The system is analogous to the netaddress/netmask system with IPv4 addresses.Primitive types cover the IDs 0-31. Thus, they have a "netaddress" of 0 and a netmask of ~0x1f. Testing for "primitiveness" is just
andingwith ~0x1f (0xffffffe0) and comparing for zero.When adding template instances as custom variant types, it is adviced to follow the convention that the lowest bits of the type ID always tell the primitive type, if possible. It is then possible to quickly check for "integerness" or "floatness" of any template type. For example, assume that the "subnet" 1/~0xff (IDs 0x100 - 0x1ff) is reserved for your template type
MyType<T>. Then, 0x100 (0x100 +CharType) should beMyType<char> and so on.
Function details
-
PiiVariant
(- char
[inline, explicit] -
PiiVariant
(- qint64
[inline, explicit] -
PiiVariant
(- bool
[inline, explicit] -
PiiVariant
(- float
[inline, explicit] -
PiiVariant
(- unsigned short
[inline, explicit] -
PiiVariant
(- short
[inline, explicit] -
PiiVariant
(- unsigned char
[inline, explicit] -
PiiVariant
(- double
[inline, explicit] -
PiiVariant
(- unsigned int
[inline, explicit] -
PiiVariant
()Creates an invalid variant.
-
PiiVariant
(- quint64
[inline, explicit] -
PiiVariant
(- int
[inline, explicit] -
PiiVariant
Creates a copy of other.
-
template<class T>
PiiVariant
(- const T & value
[explicit]Creates a variant that stores value.
The type
Tmust be declared with the PII_DECLARE_VARIANT_TYPE macro and registered with the PII_REGISTER_VARIANT_TYPE macro. -
template<class T>
PiiVariant
Creates a variant with a non-default type ID.
Only primitive types can be stored this way. If you want to give a special meaning to a variant while still storing its actual value as, say, an
int, you can do this:const int MyCustomTypeId = 0x31415927; PiiVariant var(3, MyCustomTypeId);
-
PiiVariant
(- void *
[inline, explicit] -
~PiiVariant
()Destroys the variant.
-
template<class Archive>
void serialize
(- Archive & archive
- const unsigned int
[inline] -
bool isFloat
()[inline]Returns
trueif this variant is a floating point type andfalseotherwise.See isInteger() for more information.
-
bool isInteger
()[inline]Returns
trueif this variant is an integer type (char, short, int, qint64) andfalseotherwise.Note that the function may return true even if the type is not primitive, because it is sometimes useful to be able to check for "integerness" of a template type. To check whether the variant is a "primitive integer", use
if (variant.isPrimitive() && variant.isInteger()) dealWithAnIntegerType(variant);
-
bool isPrimitive
()[inline]Returns
trueif this variant is a primitive type,falseotherwise. -
bool isUnsigned
()[inline]Returns
trueif this variant is an unsigned integer type andfalseotherwise.See isInteger() for more information.
-
bool isValid
()[inline]Returns
trueif the type of this variant is notInvalidType, andfalseotherwise. -
PiiVariant & operator=
Copies the contents of other to
this. -
template<class T>
PII_MAP_TYPE
(- PiiVariantValueMap
- T
[inline]Returns the value of the variant as a
T.This function doesn't check that the stored value actually is of type
T. The entire responsibility for checking the type is at the caller.Returns
a
constreference to the wrapped object, if the typeTis not one of the primitive types supported by PiiVariant. Otherwise, returns the the primitive type as a value. -
unsigned int type
()[inline]Get the type of the variant.
To compare for a certain type, you may do the following:
if (variant.type() == PiiVariant::DoubleType) calculateAccurately(variant.valueAs<double>()); else if (variant.type() == MyOwnTypeId) doSomethingElse(variant.valueAs<MyOwnTypeId>());
-
template<class T>
T & valueAs
()[inline]This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
-
static bool isFloat
(- unsigned int type
[inline, static]Returns
trueiftyperepresents a floating point type andfalseotherwise.See isInteger() for more information.
-
static bool isInteger
(- unsigned int type
[inline, static]Returns
trueif type represents an integer type (char, short, int, qint64) andfalseotherwise. -
static bool isPrimitive
(- unsigned int type
[inline, static]Returns
trueif type represents a primitive type,falseotherwise. -
static bool isUnsigned
(- unsigned int type
[inline, static]Returns
trueif type represents an unsigned integer type andfalseotherwise.See isInteger() for more information.
-
static bool isValid
(- unsigned int type
[inline, static]Returns
trueif type is different fromInvalidType, andfalseotherwise.
Add a note
Not a single note added yet. Be the first, add yours.