Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
traits.hxx
Go to the documentation of this file.
1 // file : odb/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_TRAITS_HXX
6 #define ODB_TRAITS_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <odb/forward.hxx>
11 #include <odb/pointer-traits.hxx>
12 
13 namespace odb
14 {
15  template <typename T, typename P>
16  class access::object_factory
17  {
18  public:
19  typedef T object_type;
20  typedef P pointer_type;
21 
22  static P
23  create ()
24  {
25  // By default use pointer-specific construction.
26  //
28  }
29  };
30 
31  template <typename T, typename P>
33  {
34  public:
35  typedef T view_type;
36  typedef P pointer_type;
37 
38  static P
39  create ()
40  {
41  // By default use pointer-specific construction.
42  //
44  }
45  };
46 
47  template <typename T, typename P>
49  {
50  public:
51  typedef T value_type;
52  typedef P pointer_type;
53 
54  static P
55  create ()
56  {
57  void* v (pointer_traits<P>::allocate (sizeof (T)));
58  mem_guard g (v);
59  P p (new (v) T);
60  g.release ();
61  return p;
62  }
63 
64  private:
65  struct mem_guard
66  {
67  mem_guard (void* p): p_ (p) {}
68  ~mem_guard () {if (p_) pointer_traits<P>::free (p_);}
69  void release () {p_ = 0;}
70  void* p_;
71  };
72  };
73 
74  //
75  // class_traits
76  //
77  enum class_kind
78  {
82  };
83 
84  template <typename T>
85  struct class_traits
86  {
87  static const class_kind kind = class_other;
88  };
89 
90  template <typename T>
91  struct class_traits<const T>
92  {
94  };
95 
96  //
97  // object_traits
98  //
99 
100  template <typename T>
101  //
102  // If a C++ compiler issues an error pointing to this struct and
103  // saying that it is incomplete, then you are most likely trying to
104  // perform a database operation on a C++ type that is not a persistent
105  // object. Or you forgot to include the corresponding -odb.hxx file.
106  //
107  struct object_traits:
109  access::object_factory<T, typename access::object_traits<T>::pointer_type>
110  {
111  typedef
114 
117  typedef typename pointer_traits::const_pointer_type const_pointer_type;
118  };
119 
120  // Specialization for const objects. It only defines the id, object,
121  // pointer, and const_pointer types with pointer and const_pointer
122  // being the same. The idea is to only use this specialization in the
123  // interfaces, with the implementations detecting this situation and
124  // using the non-const object_traits version.
125  //
126  template <typename T>
127  struct object_traits<const T>
128  {
129  private:
130  typedef
133 
134  public:
137  typedef typename pointer_traits::const_pointer_type const_pointer_type;
139 
140  static const bool polymorphic = access::object_traits<T>::polymorphic;
141  };
142 
143  // Specializations for pointer types to allow the C++ compiler to
144  // instantiate persist(), etc., signatures in class database. The
145  // overloads that use these specializations would never actually
146  // be selected by the compiler.
147  //
148  template <typename T>
149  struct object_traits<T*>
150  {
151  struct id_type {};
152  };
153 
154  template <typename T>
155  struct object_traits<T* const>
156  {
157  struct id_type {};
158  };
159 
160  template <typename T, template <typename> class P>
161  struct object_traits<P<T> >
162  {
163  struct id_type {};
164  };
165 
166  template <typename T, typename A1, template <typename, typename> class P>
167  struct object_traits<P<T, A1> >
168  {
169  struct id_type {};
170  };
171 
172  template <typename T, template <typename> class P>
173  struct object_traits<const P<T> >
174  {
175  struct id_type {};
176  };
177 
178  template <typename T, typename A1, template <typename, typename> class P>
179  struct object_traits<const P<T, A1> >
180  {
181  struct id_type {};
182  };
183 
184  // Specialization for section to allow instantiation of all the load()
185  // signature.
186  //
187  template <>
188  struct object_traits<section> {};
189 
190  template <typename T, database_id DB>
191  //
192  // If a C++ compiler issues an error pointing to this struct and
193  // saying that it is incomplete, then you are most likely trying to
194  // perform a database operation on a C++ type that is not a persistent
195  // object. Or you forgot to include the corresponding -odb.hxx file.
196  //
197  struct object_traits_impl:
199  access::object_factory<T, typename access::object_traits<T>::pointer_type>
200  {
201  typedef
204 
207  typedef typename pointer_traits::const_pointer_type const_pointer_type;
208  };
209 
210  //
211  // view_traits
212  //
213 
214  template <typename T>
215  //
216  // If a C++ compiler issues an error pointing to this struct and
217  // saying that it is incomplete, then you are most likely trying to
218  // perform a database operation on a C++ type that is not a view
219  // Or you forgot to include the corresponding -odb.hxx file.
220  //
221  struct view_traits:
223  access::view_factory<T, typename access::view_traits<T>::pointer_type>
224  {
225  typedef
228 
231  };
232 
233  // Specialization for const views. It only defines the view, pointer,
234  // and const_pointer types with pointer and const_pointer being the
235  // same. Similar to objects, the idea is to only use this specialization
236  // in the interfaces, with the implementations detecting this situation
237  // and using the non-const view_traits version.
238  //
239  template <typename T>
240  struct view_traits<const T>
241  {
242  private:
243  typedef
246 
247  public:
249  typedef typename pointer_traits::const_pointer_type const_pointer_type;
251  };
252 
253  template <typename T, database_id DB>
254  //
255  // If a C++ compiler issues an error pointing to this struct and
256  // saying that it is incomplete, then you are most likely trying to
257  // perform a database operation on a C++ type that is not a view
258  // Or you forgot to include the corresponding -odb.hxx file.
259  //
260  struct view_traits_impl:
262  access::view_factory<T, typename access::view_traits<T>::pointer_type>
263  {
264  typedef
267 
270  };
271 
272  //
273  // composite_value_traits
274  //
275 
276  template <typename T, database_id DB>
278  {
279  };
280 
281  //
282  // Get root image from a polymorphic image chain.
283  //
284 
285  template <typename T, std::size_t d>
287  {
288  typedef root_image_impl<typename T::base_traits, d - 1> base_type;
290 
291  static image_type&
292  get (typename T::image_type& i) {return base_type::get (*i.base);}
293  };
294 
295  template <typename T>
296  struct root_image_impl<T, 1>
297  {
298  typedef typename T::image_type image_type;
299 
300  static image_type&
301  get (image_type& i) {return i;}
302  };
303 
304  template <typename T, bool p>
305  struct root_image
306  {
309 
310  static image_type&
311  get (typename T::image_type& i) {return impl_type::get (i);}
312  };
313 
314  template <typename T>
315  struct root_image<T, false>
316  {
317  typedef typename T::image_type image_type;
318 
319  static image_type&
320  get (image_type& i) {return i;}
321  };
322 }
323 
324 #include <odb/post.hxx>
325 
326 #endif // ODB_TRAITS_HXX
T value_type
Definition: traits.hxx:51
pointer_traits::const_pointer_type const_pointer_type
Definition: traits.hxx:249
Definition: traits.hxx:286
P pointer_type
Definition: traits.hxx:36
static P create()
Definition: traits.hxx:23
Definition: traits.hxx:305
odb::pointer_traits< typename access::object_traits< T >::pointer_type > pointer_traits
Definition: traits.hxx:113
pointer_traits::const_pointer_type const_pointer_type
Definition: traits.hxx:207
pointer_traits::const_pointer_type const_pointer_type
Definition: traits.hxx:117
odb::pointer_traits< typename access::view_traits< T >::pointer_type > pointer_traits
Definition: traits.hxx:227
const_pointer_type pointer_type
Definition: traits.hxx:250
root_image_impl< T, T::depth > impl_type
Definition: traits.hxx:307
access::view_traits< T >::pointer_type pointer_type
Definition: traits.hxx:269
Definition: traits.hxx:80
access::view_traits< T >::pointer_type pointer_type
Definition: traits.hxx:230
Definition: pointer-traits.hxx:28
access::object_traits< T >::object_type object_type
Definition: traits.hxx:205
odb::pointer_traits< typename access::object_traits< T >::pointer_type > pointer_traits
Definition: traits.hxx:203
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:206
access::view_traits< T >::view_type view_type
Definition: traits.hxx:229
class_kind
Definition: traits.hxx:77
T::image_type image_type
Definition: traits.hxx:298
static P create()
Definition: traits.hxx:39
Definition: forward.hxx:123
Definition: forward.hxx:101
Definition: forward.hxx:132
Definition: forward.hxx:126
access::object_traits< T >::object_type object_type
Definition: traits.hxx:115
T view_type
Definition: traits.hxx:35
Definition: forward.hxx:107
P pointer_type
Definition: traits.hxx:20
T object_type
Definition: traits.hxx:19
Definition: traits.hxx:79
Definition: forward.hxx:104
Definition: forward.hxx:116
const_pointer_type pointer_type
Definition: traits.hxx:138
static const class_kind kind
Definition: traits.hxx:87
Definition: traits.hxx:81
T::image_type image_type
Definition: traits.hxx:317
Definition: forward.hxx:129
Definition: forward.hxx:98
base_type::image_type image_type
Definition: traits.hxx:289
Definition: section.hxx:15
odb::pointer_traits< typename access::view_traits< T >::pointer_type > pointer_traits
Definition: traits.hxx:266
static P create()
Definition: traits.hxx:55
Definition: forward.hxx:113
static image_type & get(typename T::image_type &i)
Definition: traits.hxx:292
pointer_traits::const_pointer_type const_pointer_type
Definition: traits.hxx:137
access::view_traits< T >::view_type view_type
Definition: traits.hxx:268
P pointer_type
Definition: traits.hxx:52
access::object_traits< T >::object_type object_type
Definition: traits.hxx:136
access::object_traits< T >::id_type id_type
Definition: traits.hxx:135
root_image_impl< typename T::base_traits, d-1 > base_type
Definition: traits.hxx:288
impl_type::image_type image_type
Definition: traits.hxx:308
Definition: traits.hxx:277
access::view_traits< T >::view_type view_type
Definition: traits.hxx:248
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
Definition: forward.hxx:110
Definition: traits.hxx:85
Definition: forward.hxx:95