Serialization::DataType Class Reference

Abstract reflection of a native C++ data type. More...

`#include <Serialization.h>`

## Classes

class | NativeDataTypeRegistry
| Registrator for native data types. More...

## Public Member Functions

| DataType ()
| Default constructor (as "invalid" DataType).
size_t | size () const
| Returns native memory size of the respective C++ object or variable.
bool | isValid () const
| Check if this is a valid DataType object.
bool | isPointer () const
| Whether this is reflecting a C/C++ pointer type.
bool | isClass () const
| Whether this is reflecting a C/C++ `struct` or `class` type.
bool | isPrimitive () const
| Whether this is reflecting a fundamental C/C++ data type.
bool | isString () const
| Whether this is a C++ `String` data type.
bool | isInteger () const
| Whether this is an integer C/C++ data type.
bool | isReal () const
| Whether this is a floating point based C/C++ data type.
bool | isBool () const
| Whether this is a boolean C/C++ data type.
bool | isEnum () const
| Whether this is a C/C++ `enum` data type.
bool | isArray () const
| Whether this is a C++ `Array<>` object type.
bool | isSet () const
| Whether this is a C++ `Set<>` object type.
bool | isMap () const
| Whether this is a C++ `Map<>` object type.
bool | isSigned () const
| Whether this is a signed integer C/C++ data type.
| operator bool () const
| Same as calling isValid().
bool | operator== (const DataType &other) const
| Comparison for equalness.
bool | operator!= (const DataType &other) const
| Comparison for inequalness.
bool | operator< (const DataType &other) const
| Smaller than comparison.
bool | operator> (const DataType &other) const
| Greater than comparison.
String | asLongDescr () const
| Human readable long description for this data type.
String | baseTypeName () const
| The base type name of this data type.
String | customTypeName (bool demangle=false) const
| The 1st user defined C/C++ data type name of this data type.
String | customTypeName2 (bool demangle=false) const
| The 2nd user defined C/C++ data type name of this data type.

## Static Public Member Functions

template<typename T>
static DataType | dataTypeOf (const T &data, bool registerType=true)
| Construct a DataType object for the given native C++ data.
template<typename T>
static DataType | dataType (bool registerType=true)
| Construct a DataType object for the given native C++ type.
template<typename T>
static size_t | sizeOf (const T &data)
| True size of passed native data in bytes.
template<typename T>
static void | registerNativeDataType ()
| Manual registration of native C++ data types.

## Protected Member Functions

| DataType (bool isPointer, int size, String baseType, String customType1="", String customType2="")
| Constructs a valid DataType object.
String | internalID () const
| Unique key for native data type, for internal purposes only.
Object | newInstance (Archive *archive) const
| Allocate and initialize a native instance of data type.

## Static Protected Member Functions

template<typename T, typename std::enable_if< std::is_default_constructible< T >::value, bool >::type = true>
static void | registerNativeDataType (const DataType &type, const T &nativeData)
template<typename T, typename std::enable_if<!std::is_default_constructible< T >::value, bool >::type = true>
static void | registerNativeDataType (const DataType &type, const T &nativeData)
template<typename T>
static String | rawCppTypeName ()
template<typename T>
static String | rawCppTypeNameOf (const T *const &data)
template<typename T>
static String | rawCppTypeNameOf (const T &data)

## Detailed Description

Abstract reflection of a native C++ data type.

Provides detailed information about a serialized C++ data type, whether it is a fundamental C/C++ data type (like `int`, `float`, `char`, etc.) or custom defined data types like a C++ `class`, C/C++ `struct`, `enum`, as well as other features of the respective data type like its native memory size and more.

All informations provided by this class are retrieved from the respective individual C++ objects, their members and other data when they are serialized, and all those information are stored with the serialized archive and its resulting data stream. Due to the availability of these extensive data type information within serialized archives, this framework is capable to use them in order to adapt its deserialization process upon subsequent changes to your individual C++ classes.

Definition at line 457 of file Serialization.h.

## Constructor & Destructor Documentation

## DataType() [1/2]

Serialization::DataType::DataType | ( |  | ) |

Default constructor (as "invalid" DataType).

Initializes a DataType object as being an "invalid" DataType object. Thus calling isValid(), after creating a DataType object with this constructor, would return `false`.

To create a valid and meaningful DataType object instead, call the static function DataType::dataTypeOf() instead.

Definition at line 95 of file Serialization.cpp.

Referenced by dataType(), dataTypeOf(), operator!=(), operator<(), operator==(), operator>(), registerNativeDataType(), and sizeOf().

## DataType() [2/2]

Serialization::DataType::DataType | ( | bool | isPointer,
|  | int | size,
|  | String | baseType,
|  | String | customType1 = "",
|  | String | customType2 = "" )

protected

Constructs a valid DataType object.

Initializes this object as "valid" DataType object, with specific and useful data type information.

This is a protected constructor which should not be called directly by applications, as its argument list is somewhat implementation specific and might change at any time. Applications should call the static function DataType::dataTypeOf() instead.

Parameters

  isPointer | - whether pointer type (i.e. a simple memory address)
  size | - native size of data type in bytes (i.e. according to `sizeof()` C/C++ keyword)
  baseType | - this framework's internal name for specifying the base type in a coarse way, which must be either one of: "int8", "uint8", "int16", "uint16", "int32", "uint32", "int64", "uint64", "bool", "real32", "real64", "String", "Array", "Set", "enum", "union" or "class"
  customType1 | - this is only used for base types "enum", "union", "class", "Array", "Set" or "Map", in which case this identifies the user defined type name (e.g. "Foo" for `class` `Foo` or e.g. "Bar" for `Array<Bar>` respectively), for all other types this is empty
  customType2 | - this is only used for `Map<>` objects in which case it identifies the map's value type (i.e. 2nd template parameter of map)

Definition at line 127 of file Serialization.cpp.

References isPointer(), and size().

## Member Function Documentation

## asLongDescr()

String Serialization::DataType::asLongDescr | ( |  | ) | const

Human readable long description for this data type.

Returns a human readable long description for this data type, designed for the purpose for being displayed to the user. Note that the implementation for this method and thus the precise textual strings returned by this method, may change at any time. So you should not rely on precise strings for certain data types, and you should not use the return values of this method for comparing data types with each other.

This class implements various comparison operators, so you should use them for comparing DataTypes objects instead.

See also
  baseTypeName(), customTypeName()

Definition at line 465 of file Serialization.cpp.

References customTypeName(), customTypeName2(), and isPointer().

Referenced by newInstance().

## baseTypeName()

String Serialization::DataType::baseTypeName | ( |  | ) | const

The base type name of this data type.

Returns a textual short string identifying the basic type of name of this data type. For example for a 32 bit signed integer data type this method would return `"int32"`. For all user defined C/C++ `enum` types this method would return "enum". For all user defined C/C++ `struct` and `class` types this method would return "class" for both. Note that the precise user defined type name (of i.e. `enum`, `struct` and `class` types) is not included in the string returned by this method, use customTypeName() to retrieve that information instead.

The precise textual strings returned by this method are guaranteed to retain equal with future versions of this framework. So you can rely on them for using the return values of this method for comparison tasks in your application. Note however that this class also implements various comparison operators.

Further it is important to know that this method returns the same string for pointers and non-pointers of the same underlying data type. So in the following example:

#include <stdint.h>
uint64_t i;
uint64_t* pi;

this method would return for both `i` and `pi` the string `"uint64"` !

See also
  isPointer(), customTypeName(), customTypeName2()

Definition at line 505 of file Serialization.cpp.

## customTypeName()

String Serialization::DataType::customTypeName | ( | bool | demangle = false | ) | const

The 1st user defined C/C++ data type name of this data type.

Call this method on user defined C/C++ data types like `enum`, `struct`, `class` or `Array<>` types to retrieve the user defined type name portion of those data types. Note that this method is only intended for such user defined data types. For all fundamental, primitive data types (like i.e. `int`) this method returns an empty string instead.

This method takes an optional boolean argument demangle, which allows you define whether you are interested in the raw C++ type name or rather the demangled custom type name. By default this method returns the raw C++ type name. The raw C++ type name is the one that is actually used in the compiled binaries and should be preferred for comparions tasks. The demangled C++ type name is a human readable representation of the type name instead, which you may use for displaying the user defined type name portion to the user, however you should not use the demangled representation for comparison tasks.

Note that in the following example:

struct Foo {
int a;
bool b;
};
Foo foo;
Foo* pFoo;

this method would return the same string for both `foo` and `pFoo` ! In the latter example `customTypeName(true)` would return for both `foo` and `pFoo` the string `"Foo"` as return value of this method.

Windows: please note that the current implementation of this method on Windows is not thread safe!

See also
  baseTypeName(), customTypeName2(), isPointer()

Definition at line 568 of file Serialization.cpp.

Referenced by asLongDescr().

## customTypeName2()

String Serialization::DataType::customTypeName2 | ( | bool | demangle = false | ) | const

The 2nd user defined C/C++ data type name of this data type.

This is currently only used for `Map<>` data types in which case this method returns the map's value type (i.e. map's 2nd template parameter).

See also
  baseTypeName(), customTypeName()

Definition at line 580 of file Serialization.cpp.

Referenced by asLongDescr().

## dataType()

template<typename T>

DataType Serialization::DataType::dataType | ( | bool | registerType = true | ) |

inline static

Construct a DataType object for the given native C++ type.

This methods basically behaves similar to above's method, whith the difference that above's method takes an argument to real native data and resolves its run-time type information (RTTI), whereas this version here just takes a compile-time constant template argument.

Parameters

  registerType | - wether type information shall be registered (default: yes)

Returns
  corresponding DataType object for the supplied C/C++ type

See also
  dataTypeOf() for run-time variant data types
  registerNativeDataType() for registration of type information

Definition at line 522 of file Serialization.h.

References DataType(), and registerNativeDataType().

Referenced by registerNativeDataType().

## dataTypeOf()

template<typename T>

DataType Serialization::DataType::dataTypeOf | ( | const T & | data,
|  | bool | registerType = true )

inline static

Construct a DataType object for the given native C++ data.

Use this function to create corresponding DataType objects for native C/C++ objects, members and variables. Run-time type information (RTTI) is used to resolve its type information.

Parameters

  data | - native C/C++ object/member/variable a DataType object shall be created for
  registerType | - wether type information shall be registered (default: yes)

Returns
  corresponding DataType object for the supplied native C/C++ object/member/variable

See also
  dataType() for compile-time known data types
  registerNativeDataType() for registration of type information

Definition at line 501 of file Serialization.h.

References DataType(), and registerNativeDataType().

Referenced by registerNativeDataType(), Serialization::Archive::serializeAnonymousObject(), Serialization::Archive::serializeHeapMember(), Serialization::Archive::serializeMember(), Serialization::Archive::setMinVersion(), Serialization::Archive::setVersion(), and sizeOf().

## internalID()

String Serialization::DataType::internalID | ( |  | ) | const

protected

Unique key for native data type, for internal purposes only.

This is more or less just a random string acting as a key for the native data type. The way this string is assembled may change at any time in future, so it is only constant for the data type during current runtime.

The generated key is used to store lambdas for each data type, e.g. for allocation and initialization of the actual native data type via the reflection API.

See also
  newInstance()

Definition at line 396 of file Serialization.cpp.

References isClass().

Referenced by newInstance(), registerNativeDataType(), and sizeOf().

## isArray()

bool Serialization::DataType::isArray | ( |  | ) | const

Whether this is a C++ `Array<>` object type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a C++ `Array<>` container object type.

Note
  : This framework handles `Array<>` types neither as primitive types, nor as class types. So `isPrimitive()` and `isClass()` both return `false` for arrays.

See also
  isPointer()

Definition at line 304 of file Serialization.cpp.

Referenced by isPrimitive().

## isBool()

bool Serialization::DataType::isBool | ( |  | ) | const

Whether this is a boolean C/C++ data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a (fundamental, primitive) boolean data type. So this is the case for the C++ `bool` data type. It does not include integer or floating point types though.

Note that this method also returns `true` on `bool` pointer types!

See also
  isPointer()

Definition at line 273 of file Serialization.cpp.

Referenced by Serialization::Archive::setAutoValue().

## isClass()

bool Serialization::DataType::isClass | ( |  | ) | const

Whether this is reflecting a C/C++ `struct` or `class` type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a C/C++ `struct` or `class` type.

Note
  : Data types which enjoy out of the box serialization support by this framework, like `String` and `Array<>` are NOT handled as class data types by this framwork. So `isClass()` returns `false` for e.g. `String` and any `Array<>` based data type.

Note that in the following example:

struct Foo {
int a;
bool b;
};
Foo foo;
Foo* pFoo;

the DataType objects of both `foo`, as well as of the C/C++ pointer `pFoo` would both return `true` for isClass() here!

See also
  isPointer()

Definition at line 185 of file Serialization.cpp.

Referenced by internalID(), isPrimitive(), and operator==().

## isEnum()

bool Serialization::DataType::isEnum | ( |  | ) | const

Whether this is a C/C++ `enum` data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a user defined enumeration data type. So this is the case for all C/C++ `enum` data types. It does not include integer (or even floating point) types though.

Note that this method also returns `true` on `enum` pointer types!

See also
  isPointer()

Definition at line 288 of file Serialization.cpp.

Referenced by Serialization::Archive::setAutoValue().

## isInteger()

bool Serialization::DataType::isInteger | ( |  | ) | const

Whether this is an integer C/C++ data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a (fundamental, primitive) integer data type. So these are all `int` and `unsigned` `int` types of any size. It does not include floating point ("real") types though.

You may use isSigned() to further check whether this data type allows negative numbers.

Note that this method also returns `true` on integer pointer types!

See also
  isPointer()

Definition at line 241 of file Serialization.cpp.

Referenced by Serialization::Archive::setAutoValue().

## isMap()

bool Serialization::DataType::isMap | ( |  | ) | const

Whether this is a C++ `Map<>` object type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is an associative sorted C++ `Map<>` container object type.

Note
  : This framework handles `Map<>` types neither as primitive types, nor as class types. So `isPrimitive()` and `isClass()` both return `false` for maps.

See also
  isPointer()

Definition at line 336 of file Serialization.cpp.

Referenced by isPrimitive().

## isPointer()

bool Serialization::DataType::isPointer | ( |  | ) | const

Whether this is reflecting a C/C++ pointer type.

Returns @true if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a C/C++ pointer type.

Definition at line 156 of file Serialization.cpp.

Referenced by asLongDescr(), and DataType().

## isPrimitive()

bool Serialization::DataType::isPrimitive | ( |  | ) | const

Whether this is reflecting a fundamental C/C++ data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a primitive, fundamental C/C++ data type. Those are fundamental data types which are already predefined by the C/C++ language, for example: `char`, `int`, `float`, `double`, `bool`, but also `String` objects and any pointer types like `int*`, `double**`, but including pointers to user defined types like:

struct Foo {
int a;
bool b;
};
Foo* pFoo;

So the DataType object of `pFoo` in the latter example would also return `true` for isPrimitive() here!

See also
  isPointer()

Definition at line 209 of file Serialization.cpp.

References isArray(), isClass(), isMap(), and isSet().

## isReal()

bool Serialization::DataType::isReal | ( |  | ) | const

Whether this is a floating point based C/C++ data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a (fundamental, primitive) floating point based data type. So these are currently the C/C++ `float` and `double` types. It does not include integer types though.

Note that this method also returns `true` on `float` pointer and `double` pointer types!

See also
  isPointer()

Definition at line 258 of file Serialization.cpp.

Referenced by isSigned(), and Serialization::Archive::setAutoValue().

## isSet()

bool Serialization::DataType::isSet | ( |  | ) | const

Whether this is a C++ `Set<>` object type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a C++ `Set<>` unique container object type.

Note
  : This framework handles `Set<>` types neither as primitive types, nor as class types. So `isPrimitive()` and `isClass()` both return `false` for sets.

See also
  isPointer()

Definition at line 320 of file Serialization.cpp.

Referenced by isPrimitive().

## isSigned()

bool Serialization::DataType::isSigned | ( |  | ) | const

Whether this is a signed integer C/C++ data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a (fundamental, primitive) signed integer data type. This is the case for are all `unsigned` `int` C/C++ types of any size. For all floating point ("real") based types this method returns `false` though!

Note that this method also returns `true` on signed integer pointer types!

See also
  isInteger();

Definition at line 353 of file Serialization.cpp.

References isReal().

Referenced by Serialization::Archive::setIntValue().

## isString()

bool Serialization::DataType::isString | ( |  | ) | const

Whether this is a C++ `String` data type.

Returns `true` if the respective native C/C++ object, member or variable (this DataType instance is reflecting) is a C++ `String` object (a.k.a. `std::string` from the C++ STL).

Note that this framework handles `String` objects as if they were a fundamental, primitive C/C++ data type, so `isPrimitive()` returns `true` for strings.

Definition at line 223 of file Serialization.cpp.

Referenced by Serialization::Archive::setAutoValue().

## isValid()

bool Serialization::DataType::isValid | ( |  | ) | const

Check if this is a valid DataType object.

Returns `true` if this DataType object is reflecting a valid data type. The default constructor creates DataType objects initialized to be "invalid" DataType objects by default. That way one can detect whether a DataType object was ever assigned to something meaningful.

Note that this class also implements the `bool` operator, both return the same boolean result.

Definition at line 147 of file Serialization.cpp.

Referenced by operator bool().

## newInstance()

Object Serialization::DataType::newInstance | ( | Archive * | archive | ) | const

protected

Allocate and initialize a native instance of data type.

This is basically like calling the C++ `new` operator for the native data type, though by calling this reflection API instead. Thus memory for one instance of the data type is allocated on the heap and initialized by data types's default constructor.

Consequently this method can only be called for native data types that do have a default constructor. For data types that do not have a default constructor, calling this method will cause an assertion fault.

Returns
  new Object reflecting the newly allocated native data instance

Definition at line 598 of file Serialization.cpp.

References asLongDescr(), and internalID().

## operator bool()

Serialization::DataType::operator bool | ( |  | ) | const

inline

Same as calling isValid().

Definition at line 474 of file Serialization.h.

References isValid().

## operator!=()

bool Serialization::DataType::operator!= | ( | const DataType & | other | ) | const

Comparison for inequalness.

Returns the inverse result of what DataType::operator==() would return. So refer to the latter for more details.

Definition at line 409 of file Serialization.cpp.

References DataType(), and operator==().

## operator<()

bool Serialization::DataType::operator< | ( | const DataType & | other | ) | const

Smaller than comparison.

Returns `true` if this DataType object can be consider to be "smaller" than the other DataType object being compared with. This operator is actually quite arbitrarily implemented and may change at any time, and thus result for the same data types may change in future at any time.

This operator is basically implemented for allowing this DataType class to be used with various standard template library (STL) classes, which require sorting operators to be implemented.

Definition at line 424 of file Serialization.cpp.

References DataType().

Referenced by operator>().

## operator==()

bool Serialization::DataType::operator== | ( | const DataType & | other | ) | const

Comparison for equalness.

Returns `true` if the two DataType objects being compared can be considered to be "equal" C/C++ data types. They are considered to be equal if their underlying C/C++ data types are exactly identical. For example comparing `int` and `unsigned` int data types are considere to be not equal, since they are differently signed. Furthermore `short` `int` and `long` `int` would also not be considered to be equal, since they do have a different memory size. Additionally pointer type characteristic is compared as well. So a `double` type and `double*` type are also considered to be not equal data types and hence this method would return `false`.

As an exception here, classes and structs with the same class/struct name but different sizes are also considered to be "equal". This relaxed requirement is necessary to retain backward compatibility to older versions of the same native C++ classes/structs.

Definition at line 376 of file Serialization.cpp.

References DataType(), and isClass().

Referenced by operator!=(), and operator>().

## operator>()

bool Serialization::DataType::operator> | ( | const DataType & | other | ) | const

Greater than comparison.

Returns `true` if this DataType object can be consider to be "greater" than the other DataType object being compared with. This operator is actually quite arbitrarily implemented and may change at any time, and thus result for the same data types may change in future at any time.

This operator is basically implemented for allowing this DataType class to be used with various standard template library (STL) classes, which require sorting operators to be implemented.

Definition at line 447 of file Serialization.cpp.

References DataType(), operator<(), and operator==().

## rawCppTypeName()

template<typename T>

String Serialization::DataType::rawCppTypeName | ( |  | ) |

inline static protected

Definition at line 775 of file Serialization.h.

## rawCppTypeNameOf() [1/2]

template<typename T>

String Serialization::DataType::rawCppTypeNameOf | ( | const T & | data | ) |

inline static protected

Definition at line 795 of file Serialization.h.

## rawCppTypeNameOf() [2/2]

template<typename T>

String Serialization::DataType::rawCppTypeNameOf | ( | const T *const & | data | ) |

inline static protected

Definition at line 789 of file Serialization.h.

## registerNativeDataType() [1/2]

template<typename T>

void Serialization::DataType::registerNativeDataType | ( |  | ) |

static

Manual registration of native C++ data types.

This method allows applications to explicitly register native C/C++ data types to this framework, for the purpose to allow creating new instances of those data types by this framework's reflection API at runtime.

@discussion Mostly it is not necessary to manually register native C/C++ data types, as this framework automatically collects required information about applications' data types at compile-time. There are rare cases though where an application needs to register some native data types explicitly to this Serialization framework by either calling `DataType::registerNativeType()` or by using class `NativeDataTypeRegistry`, before doing any (de)serialization.

For instance: if the application serializes only base class pointers and all of the sudden some of these pointers point to objects of derived classes AND this framework did not get into touch with those derived class(es) elsewhere, in this example the deriving classe(s) either might need to be registered manually, or some member variable of that derived type needs to be serialized somewhere for this framework to gather the required type information at compile time instead.

Example:

void appsInitCode() {
Serialization::DataType::registerNativeType<MyType>();
}

See also
  NativeDataTypeRegistry for registration at global C++ declaration space

Definition at line 2020 of file Serialization.h.

References DataType(), dataType(), dataTypeOf(), internalID(), and Serialization::Archive::objectByUID().

Referenced by dataType(), and dataTypeOf().

## registerNativeDataType() [2/2]

template<typename T, typename std::enable_if<!std::is_default_constructible< T >::value, bool >::type>

void Serialization::DataType::registerNativeDataType | ( | const DataType & | type,
|  | const T & | nativeData )

static protected

Definition at line 2054 of file Serialization.h.

## size()

size_t Serialization::DataType::size | ( |  | ) | const

inline

Returns native memory size of the respective C++ object or variable.

Definition at line 460 of file Serialization.h.

Referenced by DataType(), Serialization::Archive::setBoolValue(), Serialization::Archive::setEnumValue(), Serialization::Archive::setIntValue(), and Serialization::Archive::setRealValue().

## sizeOf()

template<typename T>

size_t Serialization::DataType::sizeOf | ( | const T & | data | ) |

inline static

True size of passed native data in bytes.

In most cases the result is equal to C++ sizeof(data). However for polymomorphic objects being referenced as (some) base pointer, the C++ sizeof(data) operator would potentially return a value that's smaller than the actual size of the referenced object. In such kind of scenarios this method instead returns the correct, actual (larger) size of the referenced object.

Definition at line 540 of file Serialization.h.

References DataType(), dataTypeOf(), and internalID().

The documentation for this class was generated from the following files:

- Serialization.h
- Serialization.cpp