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 
106 #if defined(__cplusplus) && __cplusplus<=201402
107 #if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
108 #pragma GCC diagnostic push
109 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
110 #endif
111  template <class T>
112  struct access_pack<std::auto_ptr<T> >
113  {
114  template <class U>
115  void operator()(cd::pack_t& x, const cd::string& d, U& a)
116  {pack_smart_ptr(x,d,a);}
117  };
118 
119  template <class T>
120  struct access_unpack<std::auto_ptr<T> >
121  {
122  template <class U>
123  void operator()(cd::unpack_t& x, const cd::string& d, U& a)
124  {unpack_smart_ptr(x,d,a);}
125  };
126 #if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
127 #pragma GCC diagnostic pop
128 #endif
129 #endif
130 
131 #if defined(__cplusplus) && __cplusplus >= 201103L
132  template <class T, class D>
133  struct access_pack<std::unique_ptr<T,D>>
134  {
135  template <class U>
136  void operator()(cd::pack_t& x, const cd::string& d, U& a)
137  {pack_smart_ptr(x,d,a);}
138  };
139 
140  template <class T, class D>
141  struct access_unpack<std::unique_ptr<T,D>>
142  {
143  template <class U>
144  void operator()(cd::unpack_t& x, const cd::string& d, U& a)
145  {unpack_smart_ptr(x,d,a);}
146  };
147 #endif
148 
149 }
150 
151 #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 pack(pack_t &targ, const string &desc, is_treenode dum, const T *const &arg)
serialise a tree (or DAG)
Definition: pack_graph.h:28
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: pack_base.h:124
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55
void unpack(unpack_t &targ, const string &desc, is_treenode dum, T *&arg)
unserialise a tree.
Definition: pack_graph.h:44