libcppa
Version 0.1
|
Classes | |
class | cppa::deserializer |
Technology-independent deserialization interface. More... | |
class | cppa::primitive_variant |
An union container for primitive data types. More... | |
class | cppa::serializer |
Technology-independent serialization interface. More... | |
class | cppa::uniform_type_info |
Provides a platform independent type name and a (very primitive) kind of reflection in combination with object. More... | |
Enumerations | |
enum | cppa::primitive_type { cppa::pt_int8, cppa::pt_int16, cppa::pt_int32, cppa::pt_int64, cppa::pt_uint8, cppa::pt_uint16, cppa::pt_uint32, cppa::pt_uint64, cppa::pt_float, cppa::pt_double, cppa::pt_long_double, cppa::pt_u8string, cppa::pt_u16string, cppa::pt_u32string, cppa::pt_null } |
Functions | |
bool | cppa::announce (const std::type_info &tinfo, uniform_type_info *utype) |
template<class C , class Parent , typename... Args> | |
std::pair< C Parent::*, util::abstract_uniform_type_info < C > * > | cppa::compound_member (C Parent::*c_ptr, const Args &...args) |
template<typename T , typename... Args> | |
bool | cppa::announce (const Args &...args) |
constexpr const char * | cppa::primitive_type_name (primitive_type ptype) |
template<typename T > | |
const T & | get (const primitive_variant &pv) |
template<typename T > | |
T & | get_ref (primitive_variant &pv) |
template<primitive_type PT> | |
const T & | get_ref (const primitive_variant &pv) |
libcppa
provides a fully network transparent communication between actors. Thus, libcppa
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
, libcppa
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 cppa::object i = cppa::uniform_type_info::by_name("@i32")->create();
However, you should rarely if ever need to use object or uniform_type_info.
There is one exception, though, where you need to care about libcppa
's type system: using custom data types in messages. The source code below compiles fine, but crashes with an exception during runtime.
#include "cppa/cppa.hpp" using namespace cppa; struct foo { int a; int b; }; int main() { send(self(), foo{1,2}); return 0; }
Depending on your platform, the error message looks somewhat like this:
terminate called after throwing an instance of 'std::runtime_error'
what(): uniform_type_info::by_type_info(): foo is an unknown typeid name
The user-defined struct foo
is not known by the libcppa
type system. Thus, libcppa
is unable to serialize/deserialize foo
and rejects it.
Fortunately, there is an easy way to add foo
the type system, without needing to implement serialize/deserialize by yourself:
cppa::announce<foo>(&foo::a, &foo::b);
announce() takes the class as template parameter and pointers to all members (or getter/setter pairs) as arguments. This works for all primitive data types and STL compliant containers. See the announce example 1, example 2, example 3 and example 4 for more details.
Obviously, there are limitations. If your class does implement an unsupported data structure, you have to implement serialize/deserialize by yourself. Example 5 shows, how to announce a tree data structure to the libcppa
type system.
enum primitive_type [related] |
Represents a type flag of primitive_variant.
Includes integers (signed and unsigned), floating points and unicode strings (std::string, std::u16string and std::u32string).
bool cppa::announce | ( | const std::type_info & | tinfo, |
uniform_type_info * | utype | ||
) |
Adds a new type mapping to the libcppa type system.
tinfo | C++ RTTI for the new type |
utype | Corresponding uniform_type_info instance. |
true
if uniform_type
was added as known instance (mapped to plain_type
); otherwise false
is returned and uniform_type
was deleted. bool cppa::announce | ( | const Args &... | args | ) |
Adds a new type mapping for T
to the libcppa type system.
args | Members of T . |
true
if T
was added to the libcppa type system; otherwise false
. std::pair<C Parent::*, util::abstract_uniform_type_info<C>*> cppa::compound_member | ( | C Parent::* | c_ptr, |
const Args &... | args | ||
) |
Creates meta informations for a non-trivial member C
.
c_ptr | Pointer to the non-trivial member. |
args | "Sub-members" of c_ptr |
c_ptr
and the created meta informations. const T & get | ( | const primitive_variant & | pv | ) | [related] |
Casts a primitive variant to its C++ type.
pv | A primitive variant of type T . |
pv
of type T
. <tt>std::logic_error</tt> | if pv is not of type T . |
T & get_ref | ( | primitive_variant & | pv | ) | [related] |
Casts a non-const primitive variant to its C++ type.
pv | A primitive variant of type T . |
pv
of type T
. <tt>std::logic_error</tt> | if pv is not of type T . |
const T & get_ref | ( | const primitive_variant & | pv | ) | [related] |
Casts a primitive variant to its C++ type.
T | C++ type equivalent of PT . |
pv | A primitive variant of type T . |
pv
of type T
. constexpr const char* cppa::primitive_type_name | ( | primitive_type | ptype | ) |
Maps a primitive_type value to its name.
ptype | Requestet primitive_type . |
ptype
.