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 108 pack(
classdesc::pack_t& b,
const classdesc::string& d,
const classdesc::shared_ptr<T>& a)
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 134 unpack(
classdesc::unpack_t& b,
const classdesc::string& d,
const classdesc::shared_ptr<T>& a)
136 classdesc::object::TypeID t;
140 #if defined(__cplusplus) && __cplusplus>=201103L 141 std::unique_ptr<T> a(classdesc::object::create(t-1));
143 std::auto_ptr<T> a(classdesc::object::create(t-1));
157 template <
class T,
class B>
161 void operator()(
cd::pack_t& b,
const cd::string& d, U& a)
165 template <
class T,
class B>
169 void operator()(
cd::unpack_t& b,
const cd::string& d, U& a)
176 #pragma omit pack classdesc::object 177 #pragma omit unpack classdesc::object 178 #pragma omit pack classdesc::Object 179 #pragma omit unpack classdesc::Object
Definition: classdesc.h:794
class to allow access to private members
Definition: classdesc_access.h:21
helper for constructing null descriptors
Definition: classdesc.h:784
class to allow access to private members
Definition: classdesc_access.h:22
Definition: pack_base.h:438
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: pack_base.h:124
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:249
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55
virtual Base::TypeID type() const
Definition: object.h:80
This * cloneT() const
same as clone(), but returning fully typed pointer
Definition: object.h:92