libcaf  0.14.4
Classes | Functions
Platform-independent type system.

libcaf provides a fully network transparent communication between actors. More...

Classes

class  caf::deserializer
 Technology-independent deserialization interface. More...
 
class  caf::serializer
 Technology-independent serialization interface. More...
 
class  caf::uniform_type_info
 Provides a platform independent type name and a (very primitive) kind of reflection in combination with uniform_value. More...
 

Functions

const uniform_type_info * caf::announce (const std::type_info &tinfo, uniform_type_info_ptr utype)
 Adds a new mapping to the type system. More...
 
template<class Member , class Parent , class... Ts>
std::pair< Member Parent::*, abstract_uniform_type_info< Member > * > caf::compound_member (Member Parent::*memptr, const Ts &...xs)
 Creates meta information for a non-trivial Member, whereas xs are the "sub-members" of Member. More...
 
template<class Member , class Parent , class... Ts>
std::pair< Member &(Parent::*)(), abstract_uniform_type_info< Member > * > caf::compound_member (Member &(Parent::*getter)(), const Ts &...xs)
 Creates meta information for a non-trivial Member accessed via the getter member function getter returning a mutable reference, whereas xs are the "sub-members" of Member. More...
 
template<class Parent , class GRes , class SRes , class SArg , class... Ts>
std::pair< std::pair< GRes(Parent::*)() const, SRes(Parent::*)(SArg)>, abstract_uniform_type_info< typename std::decay< GRes >::type > * > caf::compound_member (const std::pair< GRes(Parent::*)() const, SRes(Parent::*)(SArg)> &gspair, const Ts &...xs)
 Creates meta information for a non-trivial Member accessed via the pair of getter and setter member function pointers gspair, whereas xs are the "sub-members" of Member. More...
 
template<class T , class... Ts>
const uniform_type_info * caf::announce (std::string tname, const Ts &...xs)
 Adds a new type mapping for T to the type system using tname as its uniform name and the list of member pointers xs. More...
 

Detailed Description

libcaf provides a fully network transparent communication between actors.

Thus, it needs to serialize and deserialize message objects. Unfortunately, this is not possible using the C++ RTTI system.

Since it is not possible to extend std::type_info,libcaf uses its own type abstraction:uniform_type_info`.

Unlike std::type_info::name(), uniform_type_info::name() is guaranteed to return the same name on all supported platforms. Furthermore, it allows to create an instance of a type by name:

// creates a signed, 32 bit integer
uniform_value i = caf::uniform_type_info::by_name("@i32")->create();

However, you should rarely if ever need to use uniform_value or uniform_type_info.

There is one exception, though, where you need to care about the type system: using custom data types in messages. The source code below compiles fine, but crashes with an exception during runtime.

struct foo { int a; int b; };
int main() {
send(self, foo{1, 2});
return 0;
}

The user-defined struct foo is not known by the type system. Thus, foo cannot be serialized and the code above code above will throw a std::runtime_error.

Fortunately, there is an easy way to add simple data structures like foo the type system without implementing serialize/deserialize yourself: caf::announce<foo>("foo", &foo::a, &foo::b).

The function announce() takes the class as template parameter. The name of the announced type is the first argument, followed by pointers to all members (or getter/setter pairs). This works for all primitive data types and STL compliant containers. See the announce example for more details. Complex data structures usually require a custom serializer class.

Function Documentation

const uniform_type_info* caf::announce ( const std::type_info &  tinfo,
uniform_type_info_ptr  utype 
)

Adds a new mapping to the type system.

Returns utype.get() on success, otherwise a pointer to the previously installed singleton.

Warning
announce is not thead-safe!
Examples:
announce_5.cpp.
template<class T , class... Ts>
const uniform_type_info* caf::announce ( std::string  tname,
const Ts &...  xs 
)

Adds a new type mapping for T to the type system using tname as its uniform name and the list of member pointers xs.

Warning
announce is not thead-safe!
template<class Member , class Parent , class... Ts>
std::pair<Member Parent::*, abstract_uniform_type_info<Member>*> caf::compound_member ( Member Parent::*  memptr,
const Ts &...  xs 
)

Creates meta information for a non-trivial Member, whereas xs are the "sub-members" of Member.

See also
announce example 4
Examples:
announce_4.cpp.
template<class Member , class Parent , class... Ts>
std::pair<Member& (Parent::*)(), abstract_uniform_type_info<Member>*> caf::compound_member ( Member &(Parent::*)()  getter,
const Ts &...  xs 
)

Creates meta information for a non-trivial Member accessed via the getter member function getter returning a mutable reference, whereas xs are the "sub-members" of Member.

See also
announce example 4
template<class Parent , class GRes , class SRes , class SArg , class... Ts>
std::pair<std::pair<GRes (Parent::*)() const, SRes (Parent::*)(SArg)>, abstract_uniform_type_info<typename std::decay<GRes>::type>*> caf::compound_member ( const std::pair< GRes(Parent::*)() const, SRes(Parent::*)(SArg)> &  gspair,
const Ts &...  xs 
)

Creates meta information for a non-trivial Member accessed via the pair of getter and setter member function pointers gspair, whereas xs are the "sub-members" of Member.

See also
announce example 4