To migrate an object from one thread to another, Graphcode needs to be
able to create an object of the correct type in the destination
address space. This is achieved by means of a run time type
identification (RTTI) system. Given a type token
t
, an object of that type can be created by the call:
object *o=archetype[t]->lnew();
Instead of using C++'s built-in RTTI system, where tokens are compound
objects of somewhat indeterminate size, Graphcode implements a simple
RTTI system using template programming, in which a type token is a
simple unsigned integer. This implies that each type of object
to be used with Graph must be registered first, before use. This is
taken care for you automatically if you use Graph::AddObject()
to
add your object to the Graph.
Adding the virtual type method to your class is also easy:
class foo: public object { ... virtual int type() {return vtype(*this);} };
The first vtype
is called on an object, an object
of that type is created (via its lcopy
method), and added to
the archetype vector. The index of that object within
the archetype vector become the type token. It is vitally
important that types are added to the archetype vector in the same
order on all threads. Clearly this is a trivial requirement if only
one type is used, but slightly more care needs to be taken in the case
of multiple types of object.
If you have multiple object types, consider using the
register_type
template to ensure a consistent type registration
across the different address spaces.