Classdesc  3.D29
polyAccessPack.h
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of Classdesc
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
9 #ifndef POLY_ACCESS_PACK_H
10 #define POLY_ACCESS_PACK_H
11 #include "polyPackBase.h"
12 
13 namespace classdesc
14 {
15 #ifdef POLYPACKBASE_H
16  template <class T>
17  typename enable_if<is_base_of<PolyPackBase, typename T::element_type> >::T
18  pack_smart_ptr(pack_t& x, const string& d, const T& a,
19  dummy<0> dum=0)
20  {
21  bool valid=a.get();
22  ::pack(x,d,valid);
23  if (valid)
24  {
25  typename T::element_type::Type t=a->type();
26  ::pack(x,d,t);
27  a->pack(x,d);
28  }
29  }
30 
31  template <class T>
32  typename enable_if<Not<is_base_of<PolyPackBase, typename T::element_type> > >::T
33 #else
34  template <class T>
35  void //if Poly not defined, just define shared_ptr non-polymorphically
36 #endif
37  pack_smart_ptr(pack_t& x, const string& d, const T& a,
38  dummy<1> dum=0)
39  {
40  bool valid=a.get();
41  pack(x,d,valid);
42  if (valid)
43  pack(x,d,*a);
44  }
45 
46 #ifdef POLYPACKBASE_H
47  template <class T>
48  typename enable_if<is_base_of<PolyPackBase, typename T::element_type> >::T
49  unpack_smart_ptr(unpack_t& x, const string& d, T& a, dummy<0> dum=0)
50  {
51  bool valid;
52  unpack(x,d,valid);
53  if (valid)
54  {
55  typename T::element_type::Type t;
56  unpack(x,d,t);
57  a.reset(T::element_type::create(t));
58  a->unpack(x,d);
59  }
60  else
61  a.reset();
62  }
63 
64  template <class T>
65  typename enable_if<Not<is_base_of<PolyPackBase, typename T::element_type> > >::T
66 #else
67  template <class T>
68  void //if Poly not defined, just define smart_ptr non-polymorphically
69 #endif
70  unpack_smart_ptr(pack_t& x, const string& d, T& a, dummy<1> dum=0)
71  {
72  bool valid;
73  unpack(x,d,valid);
74  if (valid)
75  {
76  a.reset(new typename T::element_type);
77  unpack(x,d,*a);
78  }
79  else
80  a.reset();
81  }
82 
83 }
84 
85 namespace classdesc_access
86 {
87  namespace cd = classdesc;
88 
89  template <class T>
90  struct access_pack<cd::shared_ptr<T> >
91  {
92  template <class U>
93  void operator()(cd::pack_t& x, const cd::string& d, U& a)
94  {pack_smart_ptr(x,d,a);}
95  };
96 
97  template <class T>
98  struct access_unpack<cd::shared_ptr<T> >
99  {
100  template <class U>
101  void operator()(cd::unpack_t& x, const cd::string& d, U& a)
102  {unpack_smart_ptr(x,d,a);}
103  };
104 
105  template <class T>
106  struct access_pack<std::auto_ptr<T> >
107  {
108  template <class U>
109  void operator()(cd::pack_t& x, const cd::string& d, U& a)
110  {pack_smart_ptr(x,d,a);}
111  };
112 
113  template <class T>
114  struct access_unpack<std::auto_ptr<T> >
115  {
116  template <class U>
117  void operator()(cd::unpack_t& x, const cd::string& d, U& a)
118  {unpack_smart_ptr(x,d,a);}
119  };
120 
121 #if defined(__cplusplus) && __cplusplus >= 201103L
122  template <class T, class D>
123  struct access_pack<std::unique_ptr<T,D>>
124  {
125  template <class U>
126  void operator()(cd::pack_t& x, const cd::string& d, U& a)
127  {pack_smart_ptr(x,d,a);}
128  };
129 
130  template <class T, class D>
131  struct access_unpack<std::unique_ptr<T,D>>
132  {
133  template <class U>
134  void operator()(cd::unpack_t& x, const cd::string& d, U& a)
135  {unpack_smart_ptr(x,d,a);}
136  };
137 #endif
138 
139 }
140 
141 #endif
class to allow access to private members
Definition: classdesc_access.h:21
class to allow access to private members
Definition: classdesc_access.h:22
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