Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wrapper-traits.hxx
Go to the documentation of this file.
1 // file : odb/wrapper-traits.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_WRAPPER_TRAITS_HXX
6 #define ODB_WRAPPER_TRAITS_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <memory> // std::auto_ptr, std::unique_ptr, std::shared_ptr/weak_ptr
11 
12 #include <odb/nullable.hxx>
13 
14 #include <odb/details/config.hxx> // ODB_CXX11
15 #include <odb/details/meta/remove-const.hxx>
16 
17 namespace odb
18 {
19  template <typename T>
21 
22  // Sample specialization for raw pointers. It is not enabled by default
23  // since it makes many assumptions that may not always hold true (such
24  // as that instances are allocated with new and freed with delete).
25  // This makes it too dangerous to be enabled unconditionally. If you
26  // need this functionality, you can copy the below code into your
27  // application. Also consider changing it to only specialize for
28  // specific types instead of for any pointer (it will almost always
29  // do the wrong thing for char*).
30  //
31 #if 0
32  template <typename T>
33  class wrapper_traits<T*>
34  {
35  public:
36  typedef T wrapped_type;
37  typedef T* wrapper_type;
38 
39  // T can be const.
40  //
41  typedef
42  typename details::meta::remove_const<T>::result
43  unrestricted_wrapped_type;
44 
45  static const bool null_handler = true;
46  static const bool null_default = false;
47 
48  static bool
49  get_null (const wrapper_type& p)
50  {
51  return p == 0;
52  }
53 
54  static void
55  set_null (wrapper_type& p)
56  {
57  delete p;
58  p = 0;
59  }
60 
61  static const wrapped_type&
62  get_ref (const wrapper_type& p)
63  {
64  return *p;
65  }
66 
67  static unrestricted_wrapped_type&
68  set_ref (wrapper_type& p)
69  {
70  if (p == 0)
71  p = new unrestricted_wrapped_type;
72 
73  return const_cast<unrestricted_wrapped_type&> (*p);
74  }
75  };
76 #endif
77 
78  // Specialization for std::auto_ptr.
79  //
80  template <typename T>
81  class wrapper_traits< std::auto_ptr<T> >
82  {
83  public:
84  // T can be const.
85  //
86  typedef T wrapped_type;
87  typedef std::auto_ptr<T> wrapper_type;
88 
89  // T can be const.
90  //
91  typedef
92  typename odb::details::meta::remove_const<T>::result
94 
95  static const bool null_handler = true;
96  static const bool null_default = false;
97 
98  static bool
99  get_null (const wrapper_type& p)
100  {
101  return p.get () == 0;
102  }
103 
104  static void
105  set_null (wrapper_type& p)
106  {
107  p.reset ();
108  }
109 
110  static const wrapped_type&
111  get_ref (const wrapper_type& p)
112  {
113  return *p;
114  }
115 
116  static unrestricted_wrapped_type&
117  set_ref (wrapper_type& p)
118  {
119  if (p.get () == 0)
120  p.reset (new unrestricted_wrapped_type ());
121 
122  return const_cast<unrestricted_wrapped_type&> (*p);
123  }
124  };
125 
126 #ifdef ODB_CXX11
127 
128  // Specialization for C++11 std::unique_ptr.
129  //
130  template <typename T, typename D>
131  class wrapper_traits<std::unique_ptr<T, D>>
132  {
133  public:
134  // T can be const.
135  //
136  typedef T wrapped_type;
137  typedef std::unique_ptr<T, D> wrapper_type;
138 
139  // T can be const.
140  //
141  typedef
142  typename odb::details::meta::remove_const<T>::result
143  unrestricted_wrapped_type;
144 
145  static const bool null_handler = true;
146  static const bool null_default = false;
147 
148  static bool
149  get_null (const wrapper_type& p)
150  {
151  return !p;
152  }
153 
154  static void
155  set_null (wrapper_type& p)
156  {
157  p.reset ();
158  }
159 
160  static const wrapped_type&
161  get_ref (const wrapper_type& p)
162  {
163  return *p;
164  }
165 
166  static unrestricted_wrapped_type&
167  set_ref (wrapper_type& p)
168  {
169  if (!p)
170  p.reset (new unrestricted_wrapped_type ());
171 
172  return const_cast<unrestricted_wrapped_type&> (*p);
173  }
174  };
175 
176  // Specialization for C++11 std::shared_ptr.
177  //
178  template <typename T>
179  class wrapper_traits<std::shared_ptr<T>>
180  {
181  public:
182  typedef T wrapped_type;
183  typedef std::shared_ptr<T> wrapper_type;
184 
185  // T can be const.
186  //
187  typedef
188  typename odb::details::meta::remove_const<T>::result
189  unrestricted_wrapped_type;
190 
191  static const bool null_handler = true;
192  static const bool null_default = false;
193 
194  static bool
195  get_null (const wrapper_type& p)
196  {
197  return !p;
198  }
199 
200  static void
201  set_null (wrapper_type& p)
202  {
203  p.reset ();
204  }
205 
206  static const wrapped_type&
207  get_ref (const wrapper_type& p)
208  {
209  return *p;
210  }
211 
212  static unrestricted_wrapped_type&
213  set_ref (wrapper_type& p)
214  {
215  if (!p)
216  p.reset (new unrestricted_wrapped_type);
217 
218  return const_cast<unrestricted_wrapped_type&> (*p);
219  }
220  };
221 
222 #endif // ODB_CXX11
223 
224  // Specialization for odb::nullable.
225  //
226  template <typename T>
228  {
229  public:
230  // T can be const.
231  //
232  typedef T wrapped_type;
234 
235  // T can be const.
236  //
237  typedef
238  typename odb::details::meta::remove_const<T>::result
240 
241  static const bool null_handler = true;
242  static const bool null_default = true;
243 
244  static bool
245  get_null (const wrapper_type& n)
246  {
247  return n.null ();
248  }
249 
250  static void
251  set_null (wrapper_type& n)
252  {
253  n.reset ();
254  }
255 
256  static const wrapped_type&
257  get_ref (const wrapper_type& n)
258  {
259  return *n;
260  }
261 
262  static unrestricted_wrapped_type&
263  set_ref (wrapper_type& n)
264  {
265  if (n.null ())
266  n = unrestricted_wrapped_type ();
267 
268  return const_cast<unrestricted_wrapped_type&> (*n);
269  }
270  };
271 }
272 
273 #include <odb/post.hxx>
274 
275 #endif // ODB_WRAPPER_TRAITS_HXX
static unrestricted_wrapped_type & set_ref(wrapper_type &p)
Definition: wrapper-traits.hxx:117
bool null() const
Definition: nullable.hxx:139
static const wrapped_type & get_ref(const wrapper_type &p)
Definition: wrapper-traits.hxx:111
static unrestricted_wrapped_type & set_ref(wrapper_type &n)
Definition: wrapper-traits.hxx:263
T wrapped_type
Definition: wrapper-traits.hxx:86
Definition: wrapper-traits.hxx:20
T wrapped_type
Definition: wrapper-traits.hxx:232
static void set_null(wrapper_type &p)
Definition: wrapper-traits.hxx:105
Definition: nullable.hxx:15
nullable< T > wrapper_type
Definition: wrapper-traits.hxx:233
void reset()
Definition: nullable.hxx:188
static bool get_null(const wrapper_type &p)
Definition: wrapper-traits.hxx:99
odb::details::meta::remove_const< T >::result unrestricted_wrapped_type
Definition: wrapper-traits.hxx:239
odb::details::meta::remove_const< T >::result unrestricted_wrapped_type
Definition: wrapper-traits.hxx:93
static void set_null(wrapper_type &n)
Definition: wrapper-traits.hxx:251
static bool get_null(const wrapper_type &n)
Definition: wrapper-traits.hxx:245
static const wrapped_type & get_ref(const wrapper_type &n)
Definition: wrapper-traits.hxx:257
std::auto_ptr< T > wrapper_type
Definition: wrapper-traits.hxx:87