11 #include "classdesc.h"
19 typedef std::vector<shared_ptr<object> > Factory;
20 inline Factory& factory()
31 virtual TypeID type()
const=0;
32 static object* create(TypeID);
33 virtual object* clone()
const=0;
34 object* cloneT()
const {
return clone();}
35 virtual void pack(
pack_t& b)
const=0;
36 virtual void unpack(
pack_t& b)=0;
40 inline void register_with_factory(shared_ptr<object> o)
42 if (o->type()>=int(factory().size()))
43 factory().resize(o->type()+1);
44 factory()[o->type()]=o;;
49 inline object* object::create(object::TypeID t) {
50 assert(t<object::TypeID(factory().size()) && t>=0);
51 return factory()[t]->clone();
57 Register() {register_with_factory(shared_ptr<object>(
new T));}
75 template <
class This,
class Base=
object>
80 virtual typename Base::TypeID
type()
const {
81 static typename Base::TypeID t=-1;
84 t=
typename Base::TypeID(factory().size());
85 register_with_factory(shared_ptr<object>(clone()));
89 virtual object* clone()
const {
90 return new This(*dynamic_cast<const This*>(
this));}
92 This *
cloneT()
const {
return dynamic_cast<This*
>(clone());}
93 virtual void pack(
pack_t& b)
const {
94 ::pack(b,
"",*dynamic_cast<const This*>(
this));}
95 virtual void unpack(pack_t& b) {
96 ::unpack(b,
"",*dynamic_cast<This*>(
this));}
101 {
static const bool value=is_base_of<object,T>::value;};
106 template <
class T>
typename
116 b<<classdesc::object::TypeID(0);
119 template <
class T>
typename
123 classdesc::object::TypeID t;
127 a.reset(classdesc::object::create(t-1));
132 template <
class T>
typename
136 classdesc::object::TypeID t;
140 std::auto_ptr<T> a(classdesc::object::create(t-1));
146 namespace classdesc_access
148 namespace cd=classdesc;
153 template <
class T,
class B>
157 void operator()(
cd::pack_t& b,
const cd::string& d, U& a)
161 template <
class T,
class B>
165 void operator()(
cd::unpack_t& b,
const cd::string& d, U& a)
172 #pragma omit pack classdesc::object
173 #pragma omit unpack classdesc::object
174 #pragma omit pack classdesc::Object
175 #pragma omit unpack classdesc::Object
Definition: classdesc.h:778
virtual Base::TypeID type() const
Definition: object.h:80
class to allow access to private members
Definition: classdesc_access.h:21
helper for constructing null descriptors
Definition: classdesc.h:768
This * cloneT() const
same as clone(), but returning fully typed pointer
Definition: object.h:92
class to allow access to private members
Definition: classdesc_access.h:22
Definition: pack_base.h:438
void unpack(unpack_t &targ, const string &desc, is_treenode dum, T *&arg)
unserialise a tree.
Definition: pack_graph.h:44
void pack(pack_t &targ, const string &desc, is_treenode dum, const T *const &arg)
serialise a tree (or DAG)
Definition: pack_graph.h:28
Definition: pack_base.h:124
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:274