Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
cache-traits.hxx
Go to the documentation of this file.
1 // file : odb/cache-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_CACHE_TRAITS_HXX
6 #define ODB_CACHE_TRAITS_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <odb/traits.hxx>
11 #include <odb/forward.hxx>
12 #include <odb/session.hxx>
13 #include <odb/pointer-traits.hxx>
15 
16 namespace odb
17 {
18  // pointer_cache_traits
19  //
20  // Caching traits for objects passed by pointer. P should be the canonical
21  // pointer (non-const).
22  //
23  template <typename P, typename S, pointer_kind pk>
25  {
26  typedef P pointer_type;
27  typedef S session_type;
29  typedef typename pointer_traits::element_type object_type;
31  typedef typename session_type::template cache_position<object_type>
33 
34  struct insert_guard
35  {
37  insert_guard (const position_type& pos): pos_ (pos) {}
38  ~insert_guard () {session_type::_cache_erase (pos_);}
39 
40  const position_type&
41  position () const {return pos_;}
42 
43  void
44  release () {pos_ = position_type ();}
45 
46  // Note: doesn't call erase() on the old position (assumes empty).
47  //
48  void
49  reset (const position_type& pos) {pos_ = pos;}
50 
51  private:
52  position_type pos_;
53  };
54 
55  // Cache management.
56  //
57  // We need the insert() overload with explicit id to handle self-
58  // references. In such cases the object is not yet loaded and the
59  // id member does not contain the correct id.
60  //
61  // Qualify the database type to resolve a phony ambiguity in VC 10.
62  //
63  static position_type
64  insert (odb::database& db, const id_type& id, const pointer_type& p)
65  {
66  return session_type::template _cache_insert<object_type> (db, id, p);
67  }
68 
69  static position_type
71  {
72  const id_type& id (
74  pointer_traits::get_ref (p)));
75 
76  return session_type::template _cache_insert<object_type> (db, id, p);
77  }
78 
79  static pointer_type
80  find (odb::database& db, const id_type& id)
81  {
82  return session_type::template _cache_find<object_type> (db, id);
83  }
84 
85  static void
86  erase (const position_type& p)
87  {
88  session_type::template _cache_erase<object_type> (p);
89  }
90 
91  // Notifications.
92  //
93  static void
95  {
96  session_type::template _cache_persist<object_type> (p);
97  }
98 
99  static void
100  load (const position_type& p)
101  {
102  session_type::template _cache_load<object_type> (p);
103  }
104 
105  static void
106  update (odb::database& db, const object_type& obj)
107  {
108  session_type::template _cache_update<object_type> (db, obj);
109  }
110 
111  static void
112  erase (odb::database& db, const id_type& id)
113  {
114  session_type::template _cache_erase<object_type> (db, id);
115  }
116  };
117 
118  // Unique pointers don't work with the object cache.
119  //
120  template <typename P, typename S>
123 
124  template <typename P, typename S>
126  pointer_cache_traits_impl<P, S, pointer_traits<P>::kind> {};
127 
128  // reference_cache_traits
129  //
130  // Caching traits for objects passed by reference. T should be the
131  // canonical object type (non-const). Only if the object pointer
132  // kind is raw do we add the object to the session.
133  //
134  template <typename T, typename S, pointer_kind pk>
136 
137  template <typename T, typename S>
139  {
140  typedef T object_type;
143 
146  typedef typename pointer_traits::insert_guard insert_guard;
147 
148  static position_type
149  insert (odb::database& db, const id_type& id, object_type& obj)
150  {
151  pointer_type p (&obj);
152  return pointer_traits::insert (db, id, p);
153  }
154 
155  static position_type
157  {
158  pointer_type p (&obj);
159  return pointer_traits::insert (db, p);
160  }
161 
162  static void
164  {
165  pointer_traits::persist (p);
166  }
167 
168  static void
169  load (const position_type& p)
170  {
171  pointer_traits::load (p);
172  }
173  };
174 
175  template <typename T, typename S>
178  T, S, pointer_traits<typename object_traits<T>::pointer_type>::kind> {};
179 }
180 
181 #include <odb/post.hxx>
182 
183 #endif // ODB_CACHE_TRAITS_HXX
T object_type
Definition: cache-traits.hxx:140
pointer_traits::position_type position_type
Definition: cache-traits.hxx:145
static void erase(odb::database &db, const id_type &id)
Definition: cache-traits.hxx:112
void reset(const position_type &pos)
Definition: cache-traits.hxx:49
static void update(odb::database &db, const object_type &obj)
Definition: cache-traits.hxx:106
pointer_traits::insert_guard insert_guard
Definition: cache-traits.hxx:146
object_traits< object_type >::id_type id_type
Definition: cache-traits.hxx:30
Definition: cache-traits.hxx:125
Definition: forward.hxx:140
Definition: cache-traits.hxx:135
~insert_guard()
Definition: cache-traits.hxx:38
static void load(const position_type &p)
Definition: cache-traits.hxx:100
S session_type
Definition: cache-traits.hxx:27
static void persist(const position_type &p)
Definition: cache-traits.hxx:163
object_traits< object_type >::id_type id_type
Definition: cache-traits.hxx:142
insert_guard(const position_type &pos)
Definition: cache-traits.hxx:37
Definition: pointer-traits.hxx:28
static void load(const position_type &p)
Definition: cache-traits.hxx:169
Definition: forward.hxx:123
pointer_cache_traits< pointer_type, S > pointer_traits
Definition: cache-traits.hxx:144
static void persist(const position_type &p)
Definition: cache-traits.hxx:94
odb::pointer_traits< pointer_type > pointer_traits
Definition: cache-traits.hxx:28
object_traits< object_type >::pointer_type pointer_type
Definition: cache-traits.hxx:141
static position_type insert(odb::database &db, object_type &obj)
Definition: cache-traits.hxx:156
pointer_traits::element_type object_type
Definition: cache-traits.hxx:29
Definition: database.hxx:38
static position_type insert(odb::database &db, const id_type &id, const pointer_type &p)
Definition: cache-traits.hxx:64
static pointer_type find(odb::database &db, const id_type &id)
Definition: cache-traits.hxx:80
static position_type insert(odb::database &db, const id_type &id, object_type &obj)
Definition: cache-traits.hxx:149
P pointer_type
Definition: cache-traits.hxx:26
Definition: cache-traits.hxx:34
static void erase(const position_type &p)
Definition: cache-traits.hxx:86
Definition: pointer-traits.hxx:21
const position_type & position() const
Definition: cache-traits.hxx:41
Definition: cache-traits.hxx:24
Definition: forward.hxx:137
session_type::template cache_position< object_type > position_type
Definition: cache-traits.hxx:32
static position_type insert(odb::database &db, const pointer_type &p)
Definition: cache-traits.hxx:70
insert_guard()
Definition: cache-traits.hxx:36
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
Definition: pointer-traits.hxx:22
void release()
Definition: cache-traits.hxx:44
Definition: cache-traits.hxx:176