Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
polymorphic-map.hxx
Go to the documentation of this file.
1 // file : odb/polymorphic-map.hxx
2 // copyright : Copyright (c) 2005-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_POLYMORPHIC_MAP_HXX
6 #define ODB_POLYMORPHIC_MAP_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <map>
11 #include <utility> // std::move
12 #include <cstddef> // std::size_t
13 #include <cassert>
14 #include <typeinfo>
15 
16 #include <odb/callback.hxx>
17 
18 #include <odb/details/config.hxx> // ODB_CXX11
19 #include <odb/details/type-info.hxx>
20 
21 #include <odb/polymorphic-info.hxx>
22 
23 namespace odb
24 {
25  template <typename R>
26  struct polymorphic_map
27  {
28  typedef R root_type;
31 
33 
34  const info_type&
35  find (const std::type_info& t) const;
36 
37  const info_type&
38  find (const discriminator_type& d) const;
39 
40  public:
41  typedef
42  std::map<const std::type_info*,
43  const info_type*,
44  odb::details::type_info_comparator> // VC bug.
46 
48  {
49  bool
51  const discriminator_type* y) const
52  {
53  return *x < *y;
54  }
55  };
56 
57  typedef
58  std::map<const discriminator_type*,
59  const info_type*,
60  discriminator_comparator>
62 
63  public:
64  std::size_t ref_count_;
67  };
68 
69  template <typename R, database_id DB>
71  {
72  typedef R root_type;
75 
76  static void
77  insert (const info_type&);
78 
79  static void
80  erase (const info_type&);
81  };
82 
83  template <typename T>
86  {
87  typedef object_traits<T> derived_traits;
89 
90  typedef typename derived_traits::pointer_type derived_pointer_type;
91  typedef typename root_traits::pointer_type root_pointer_type;
92 
93  derived_pointer_type p (
95 
96  // Implicit downcast.
97  //
98 #ifdef ODB_CXX11
99  root_pointer_type r (std::move (p));
100 #else
101  root_pointer_type r (p);
102 #endif
103  return r;
104  }
105 
106  template <typename T, database_id DB, typename R>
108  {
109  static void
110  call (database& db, T& obj, std::size_t d)
111  {
113  }
114  };
115 
116  template <typename R, database_id DB>
117  struct dispatch_load<R, DB, R>
118  {
119  static void
120  call (database&, R&, std::size_t)
121  {
122  assert (false);
123  }
124  };
125 
126  template <typename T, database_id DB, bool auto_id>
128  {
129  static void
130  call (database& db, const T& obj)
131  {
132  // Top-level call, no dynamic type checking.
133  //
134  object_traits_impl<T, DB>::persist (db, obj, true, false);
135  }
136  };
137 
138  template <typename T, database_id DB>
139  struct dispatch_persist<T, DB, true>
140  {
141  static void
142  call (database& db, const T& obj)
143  {
144  // Top-level call, no dynamic type checking.
145  //
147  db, const_cast<T&> (obj), true, false);
148  }
149  };
150 
151  template <typename T, database_id DB>
152  bool
154  typename polymorphic_concrete_info<
155  typename object_traits<T>::root_type>::call_type c,
156  database& db,
157  const typename object_traits<T>::root_type* pobj,
158  const void* arg)
159  {
160  typedef object_traits_impl<T, DB> derived_traits;
161  typedef typename derived_traits::root_type root_type;
162  typedef object_traits_impl<root_type, DB> root_traits;
163  typedef typename root_traits::id_type id_type;
164  typedef polymorphic_concrete_info<root_type> info_type;
165 
166  bool r (false);
167 
168  switch (c)
169  {
170  case info_type::call_callback:
171  {
172  derived_traits::callback (
173  db,
174  *const_cast<T*> (static_cast<const T*> (pobj)),
175  *static_cast<const callback_event*> (arg));
176  break;
177  }
178  case info_type::call_persist:
179  {
181  db,
182  *static_cast<const T*> (pobj));
183  break;
184  }
185  case info_type::call_update:
186  {
187  derived_traits::update (
188  db,
189  *static_cast<const T*> (pobj),
190  true, // Top-level call.
191  false); // No dynamic type checking.
192  break;
193  }
194  case info_type::call_find:
195  {
196  r = derived_traits::find (
197  db,
198  *static_cast<const id_type*> (arg),
199  *const_cast<T*> (static_cast<const T*> (pobj)),
200  false); // No dynamic type checking.
201  break;
202  }
203  case info_type::call_reload:
204  {
205  r = derived_traits::reload (
206  db,
207  *const_cast<T*> (static_cast<const T*> (pobj)),
208  false); // No dynamic type checking.
209  break;
210  }
211  case info_type::call_load:
212  {
214  db,
215  *const_cast<T*> (static_cast<const T*> (pobj)),
216  *static_cast<const std::size_t*> (arg));
217  break;
218  }
219  case info_type::call_erase:
220  {
221  if (pobj != 0)
222  derived_traits::erase (
223  db,
224  *static_cast<const T*> (pobj),
225  true, // Top-level call.
226  false); // No dynamic type checking.
227  else
228  derived_traits::erase (
229  db,
230  *static_cast<const id_type*> (arg),
231  true, // Top-level call.
232  false); // No dynamic type checking.
233  break;
234  }
235  }
236 
237  return r;
238  }
239 
240  template <typename T, database_id DB, typename ST>
241  void
243  typename object_traits<T>::root_type& obj,
244  bool top)
245  {
246  typedef object_traits_impl<T, DB> derived_traits;
247  typedef typename derived_traits::statements_type statements_type;
248  typedef typename statements_type::connection_type connection_type;
249 
250  connection_type& c (static_cast<connection_type&> (conn));
251  statements_type& sts (c.statement_cache ().template find_object<T> ());
252 
253  ST::load (sts.extra_statement_cache (), static_cast<T&> (obj), top);
254  }
255 
256  template <typename T, database_id DB, typename ST>
257  void
259  const typename object_traits<T>::root_type& obj)
260  {
261  typedef object_traits_impl<T, DB> derived_traits;
262  typedef typename derived_traits::statements_type statements_type;
263  typedef typename statements_type::connection_type connection_type;
264 
265  connection_type& c (static_cast<connection_type&> (conn));
266  statements_type& sts (c.statement_cache ().template find_object<T> ());
267 
268  ST::update (sts.extra_statement_cache (), static_cast<const T&> (obj));
269  }
270 }
271 
272 #include <odb/polymorphic-map.ixx>
273 #include <odb/polymorphic-map.txx>
274 
275 #include <odb/post.hxx>
276 
277 #endif // ODB_POLYMORPHIC_MAP_HXX
std::map< const discriminator_type *, const info_type *, discriminator_comparator > discriminator_map
Definition: polymorphic-map.hxx:61
Definition: polymorphic-info.hxx:103
object_traits< typename object_traits< T >::root_type >::pointer_type create_impl()
Definition: polymorphic-map.hxx:85
static void call(database &db, const T &obj)
Definition: polymorphic-map.hxx:142
static void insert(const info_type &)
info_type::discriminator_type discriminator_type
Definition: polymorphic-map.hxx:30
Definition: polymorphic-map.hxx:107
polymorphic_concrete_info< root_type > info_type
Definition: polymorphic-map.hxx:74
Definition: polymorphic-map.hxx:47
static void call(database &db, const T &obj)
Definition: polymorphic-map.hxx:130
Definition: forward.hxx:123
root_traits::discriminator_type discriminator_type
Definition: polymorphic-info.hxx:111
Definition: forward.hxx:101
Definition: forward.hxx:126
std::size_t ref_count_
Definition: polymorphic-map.hxx:64
type_map type_map_
Definition: polymorphic-map.hxx:65
bool operator()(const discriminator_type *x, const discriminator_type *y) const
Definition: polymorphic-map.hxx:50
static void call(database &db, T &obj, std::size_t d)
Definition: polymorphic-map.hxx:110
Definition: polymorphic-map.hxx:70
Definition: polymorphic-map.hxx:127
static void erase(const info_type &)
std::map< const std::type_info *, const info_type *, odb::details::type_info_comparator > type_map
Definition: polymorphic-map.hxx:45
Definition: database.hxx:38
const info_type & find(const std::type_info &t) const
Definition: connection.hxx:33
discriminator_map discriminator_map_
Definition: polymorphic-map.hxx:66
polymorphic_concrete_info< root_type > info_type
Definition: polymorphic-map.hxx:29
void section_update_impl(odb::connection &conn, const typename object_traits< T >::root_type &obj)
Definition: polymorphic-map.hxx:258
bool dispatch_impl(typename polymorphic_concrete_info< typename object_traits< T >::root_type >::call_type c, database &db, const typename object_traits< T >::root_type *pobj, const void *arg)
Definition: polymorphic-map.hxx:153
R root_type
Definition: polymorphic-map.hxx:72
static void call(database &, R &, std::size_t)
Definition: polymorphic-map.hxx:120
R root_type
Definition: polymorphic-map.hxx:28
polymorphic_map()
Definition: polymorphic-map.hxx:32
object_traits_impl< root_type, DB > root_traits
Definition: polymorphic-map.hxx:73
void section_load_impl(odb::connection &conn, typename object_traits< T >::root_type &obj, bool top)
Definition: polymorphic-map.hxx:242