9#ifndef CLASSDESC_OBJECT_H
10#define CLASSDESC_OBJECT_H
19 typedef std::vector<shared_ptr<object> > ObjectFactory;
20 inline ObjectFactory& factory()
23 static ObjectFactory f;
31 virtual TypeID type()
const=0;
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()));
90 return new This(*
dynamic_cast<const This*
>(
this));}
92 This *
cloneT()
const {
return dynamic_cast<This*
>(clone());}
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;};
105template <
class T>
typename
107pack(
classdesc::pack_t& b,
const classdesc::string& d,
const classdesc::shared_ptr<T>& a)
115 b<<classdesc::object::TypeID(0);
118template <
class T>
typename
120unpack(classdesc::unpack_t& b,
const classdesc::string& d, classdesc::shared_ptr<T>& a)
122 classdesc::object::TypeID t;
126 classdesc::shared_ptr<classdesc::object> tmp(classdesc::object::create(t-1));
127#if defined(__cplusplus) && __cplusplus>=201103L
128 a=classdesc::dynamic_pointer_cast<T>(std::move(tmp));
130 a=classdesc::dynamic_pointer_cast<T>(tmp);
136template <
class T>
typename
138unpack(classdesc::unpack_t& b,
const classdesc::string& d,
const classdesc::shared_ptr<T>& a)
140 classdesc::object::TypeID t;
144#if defined(__cplusplus) && __cplusplus>=201103L
145 std::unique_ptr<classdesc::object> a(classdesc::object::create(t-1));
147 std::auto_ptr<classdesc::object> a(classdesc::object::create(t-1));
155#pragma omit pack classdesc::object
156#pragma omit unpack classdesc::object
157#pragma omit pack classdesc::Object
158#pragma omit unpack classdesc::Object
Definition pack_base.h:138
Contains definitions related to classdesc functionality.
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
virtual Base::TypeID type() const
Definition object.h:80
This * cloneT() const
same as clone(), but returning fully typed pointer
Definition object.h:92
controlled template specialisation: stolen from boost::enable_if.
Definition classdesc.h:282
Definition pack_base.h:486