Into

Modules

Documentation

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

(
  • char
)
(
  • qint64
)
(
  • bool
)
(
  • float
)
(
  • unsigned short
)
(
  • short
)
(
  • unsigned char
)
(
  • double
)
(
  • unsigned int
)

Creates an invalid variant.

(
  • quint64
)
(
  • int
)
( )

Creates a copy of other.

template<class T>
(
  • const T & value
)

Creates a variant that stores value.

template<class T>
( )

Creates a variant with a non-default type ID.

(
  • void *
)

Destroys the variant.

Public member functions

bool
( )

Returns true if this variant is a floating point type and false otherwise.

bool
( )

Returns true if this variant is an integer type (char, short, int, qint64) and false otherwise.

bool

Returns true if this variant is a primitive type, false otherwise.

bool

Returns true if this variant is an unsigned integer type and false otherwise.

bool
( )

Returns true if the type of this variant is not InvalidType, and false otherwise.

( )

Copies the contents of other to this.

template<class T>
(
  • PiiVariantValueMap
  • T
)

Returns the value of the variant as a T.

unsigned int
( )

Get the type of the variant.

template<class T>
T &
( )

Static public member functions

static bool
(
  • unsigned int type
)

Returns true if type represents a floating point type and false otherwise.

static bool
(
  • unsigned int type
)

Returns true if type represents an integer type (char, short, int, qint64) and false otherwise.

static bool
(
  • unsigned int type
)

Returns true if type represents a primitive type, false otherwise.

static bool
(
  • unsigned int type
)

Returns true if type represents an unsigned integer type and false otherwise.

static bool
(
  • unsigned int type
)

Returns true if type is different from InvalidType, and false otherwise.

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 anding with ~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 be MyType<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 T must 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 true if this variant is a floating point type and false otherwise.

    See isInteger() for more information.

  • bool isInteger

    ()
    [inline]

    Returns true if this variant is an integer type (char, short, int, qint64) and false otherwise.

    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 true if this variant is a primitive type, false otherwise.

  • bool isUnsigned

    ()
    [inline]

    Returns true if this variant is an unsigned integer type and false otherwise.

    See isInteger() for more information.

  • bool isValid

    ()
    [inline]

    Returns true if the type of this variant is not InvalidType, and false otherwise.

  • 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 const reference to the wrapped object, if the type T is 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 true if type represents a floating point type and false otherwise.

    See isInteger() for more information.

  • static bool isInteger

    (
    • unsigned int type
    )
    [inline, static]

    Returns true if type represents an integer type (char, short, int, qint64) and false otherwise.

  • static bool isPrimitive

    (
    • unsigned int type
    )
    [inline, static]

    Returns true if type represents a primitive type, false otherwise.

  • static bool isUnsigned

    (
    • unsigned int type
    )
    [inline, static]

    Returns true if type represents an unsigned integer type and false otherwise.

    See isInteger() for more information.

  • static bool isValid

    (
    • unsigned int type
    )
    [inline, static]

    Returns true if type is different from InvalidType, and false otherwise.

Notes (0)

Add a note

Not a single note added yet. Be the first, add yours.