libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Static Public Member Functions | List of all members
qi::TypeInterface Class Referenceabstract

#include <typeinterface.hpp>

Public Member Functions

virtual ~TypeInterface ()=default
 
virtual const TypeInfoinfo ()=0
 Get the TypeInfo corresponding to this type. More...
 
virtual void * initializeStorage (void *ptr=nullptr)=0
 
virtual void * ptrFromStorage (void **)=0
 
virtual void * clone (void *)=0
 Allocate a storage and copy the value given as an argument. More...
 
virtual void destroy (void *)=0
 Free all resources of a storage. More...
 
virtual TypeKind kind ()
 
virtual bool less (void *a, void *b)=0
 
const char * infoString ()
 
qi::Signature signature (void *storage=nullptr, bool resolveDynamic=false)
 

Static Public Member Functions

static TypeInterfacefromSignature (const qi::Signature &sig)
 

Detailed Description

TypeInterface base interface. Further interfaces inheriting from TypeInterface define operations specific to some type (like lists, strings, integral values...).

Type erasure is implemented using the following model. A value or a reference is represented by a void* paired with a TypeInterface to manipulate the data.

A TypeInterface implements basic operations on values of the type it represents. It manipulates said value through an opaque storage pointer, initialized by clone() or initializeStorage(). Thus, TypeInterface instances do not usually hold any data, may exist as singletons inside programs and must not be freed.

To obtain a TypeInterface for a known C++ type, use typeOf<>() or typeFromSignature().

Great care must be taken when manipulating storage: depending on the circunstances, it can point to a value allocated by the TypeInterface on the heap, or to a user-provided value on the heap or on the stack.

As an example, here is a type erased union:

struct MyUnion {
enum Type type;
union Data {
int i;
float f;
} data;
};
+-----------------------+ <-- storage of Dynamic
| Type type |
| +-------------------+ | <-- storage returned when get() is called
| | int i / float f | |
| +-------------------+ |
+-----------------------+

DynamicTypeInterface::get() checks the type with the internal Type enum and returns a reference with the right TypeInterface to manipulate the inner data and a pointer to that inner data.

This base TypeInterface has all the operations we need on any type:

Our aim is to transport arbitrary values through:

Definition at line 129 of file typeinterface.hpp.

Constructor & Destructor Documentation

virtual qi::TypeInterface::~TypeInterface ( )
virtualdefault

Member Function Documentation

virtual void* qi::TypeInterface::clone ( void *  )
pure virtual
virtual void qi::TypeInterface::destroy ( void *  )
pure virtual
static TypeInterface* qi::TypeInterface::fromSignature ( const qi::Signature sig)
static
Returns
a Type on which signature() returns sig.
virtual const TypeInfo& qi::TypeInterface::info ( )
pure virtual
const char* qi::TypeInterface::infoString ( )
inline

Definition at line 178 of file typeinterface.hpp.

virtual void* qi::TypeInterface::initializeStorage ( void *  ptr = nullptr)
pure virtual

Initialize and return a new storage, from nothing or a T*.

If ptr is not null, it should be used as a storage (the method can usually just return ptr in that case).

Implemented in qi::TypeImpl< void >, qi::DefaultTypeImpl< T, _Access >, qi::DefaultTypeImpl< int >, qi::DefaultTypeImpl< T >, and qi::detail::StaticObjectTypeBase.

TypeKind qi::TypeInterface::kind ( )
inlinevirtual
virtual bool qi::TypeInterface::less ( void *  a,
void *  b 
)
pure virtual

Return true if a is less than b

Less must always work: compare pointers if you have to.

Implemented in qi::TypeImpl< void >, qi::DefaultTypeImpl< T, _Access >, qi::DefaultTypeImpl< int >, qi::DefaultTypeImpl< T >, and qi::detail::StaticObjectTypeBase.

virtual void* qi::TypeInterface::ptrFromStorage ( void **  )
pure virtual

Get pointer to type from pointer to storage.

This allows for storing an integer value (for instance) directily into the pointer and avoid an allocation.

This method should be called on storage before casting it to a specific type.

Implemented in qi::TypeImpl< void >, qi::DefaultTypeImpl< T, _Access >, qi::DefaultTypeImpl< int >, qi::DefaultTypeImpl< T >, and qi::detail::StaticObjectTypeBase.

qi::Signature qi::TypeInterface::signature ( void *  storage = nullptr,
bool  resolveDynamic = false 
)
Returns
the serialization signature corresponding to what the type would emit
Parameters
storagethe storage of the value (used only when resolveDynamic is true)
resolveDynamicif true, resolve dynamic types as deep as possible for example a list<AnyReference> that happens to only contain int32 will return [i]
Warning
if resolveDynamic is true, a valid storage must be given

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