Common ODB Runtime Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
session.hxx
Go to the documentation of this file.
1 // file : odb/session.hxx
2 // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC
3 // license : GNU GPL v2; see accompanying LICENSE file
4 
5 #ifndef ODB_SESSION_HXX
6 #define ODB_SESSION_HXX
7 
8 #include <odb/pre.hxx>
9 
10 #include <map>
11 #include <typeinfo>
12 
13 #include <odb/traits.hxx>
14 #include <odb/forward.hxx>
15 
16 #include <odb/details/shared-ptr.hxx>
17 #include <odb/details/type-info.hxx>
18 
19 #include <odb/details/export.hxx>
20 
21 namespace odb
22 {
23  class LIBODB_EXPORT session
24  {
25  public:
27 
28  // If the make_current argument is true, then set the current thread's
29  // session to this session. If another session is already in effect,
30  // throw the already_in_session exception.
31  //
32  session (bool make_current = true);
33 
34  // Reset the current thread's session if it is this session.
35  //
36  ~session ();
37 
38  // Current session.
39  //
40  public:
41  // Return true if there is a session in effect in the current
42  // thread.
43  //
44  static bool
45  has_current () {return current_pointer () != 0;}
46 
47  // Get current thread's session. Throw if no session is in effect.
48  //
49  static session&
50  current ();
51 
52  // Set current thread's session.
53  //
54  static void
55  current (session& s) {current_pointer (&s);}
56 
57  // Revert to the no session in effect state for the current thread.
58  //
59  static void
60  reset_current () {current_pointer (0);}
61 
62  // Pointer versions.
63  //
64  static session*
65  current_pointer ();
66 
67  static void
68  current_pointer (session*);
69 
70  // Copying or assignment of sessions is not supported.
71  //
72  private:
73  session (const session&);
74  session& operator= (const session&);
75 
76  public:
77  struct LIBODB_EXPORT object_map_base: details::shared_base
78  {
79  virtual
80  ~object_map_base ();
81  };
82 
83  template <typename T>
85  std::map<typename object_traits<T>::id_type,
86  typename object_traits<T>::pointer_type>
87  {
88  };
89 
90  // Object cache.
91  //
92  public:
93  // Position in the cache of the inserted element.
94  //
95  template <typename T>
97 
98  template <typename T>
100  cache_insert (database_type&,
101  const typename object_traits<T>::id_type&,
102  const typename object_traits<T>::pointer_type&);
103 
104  template <typename T>
106  cache_find (database_type&,
107  const typename object_traits<T>::id_type&) const;
108 
109  template <typename T>
110  void
111  cache_erase (const cache_position<T>&);
112 
113  template <typename T>
114  void
115  cache_erase (database_type&, const typename object_traits<T>::id_type&);
116 
117  // Low-level object cache access (iteration, etc).
118  //
119  public:
120  typedef std::map<const std::type_info*,
121  details::shared_ptr<object_map_base>,
122  details::type_info_comparator> type_map;
123 
124  typedef std::map<database_type*, type_map> database_map;
125 
126  database_map&
127  map () {return db_map_;}
128 
129  const database_map&
130  map () const {return db_map_;}
131 
132  // Static cache API as expected by the rest of ODB.
133  //
134  public:
135  // Position in the cache of the inserted element. The requirements
136  // for this class template are: default and copy-constructible as
137  // well as copy-assignable. The default constructor creates an
138  // empty/NULL position.
139  //
140  template <typename T>
141  struct cache_position
142  {
144  typedef typename map::iterator iterator;
145 
146  cache_position (): map_ (0) {}
147  cache_position (map& m, const iterator& p): map_ (&m), pos_ (p) {}
148 
150  operator= (const cache_position& p)
151  {
152  // It might not be ok to use an uninitialized iterator on the rhs.
153  //
154  if (p.map_ != 0)
155  pos_ = p.pos_;
156  map_ = p.map_;
157  return *this;
158  }
159 
162  };
163 
164  // The following cache management functions are all static to
165  // allow for a custom notion of a current session. The erase()
166  // function is called to remove the object if the operation
167  // that caused it to be inserted (e.g., load) failed.
168  //
169  template <typename T>
170  static cache_position<T>
171  _cache_insert (database_type&,
172  const typename object_traits<T>::id_type&,
173  const typename object_traits<T>::pointer_type&);
174 
175  template <typename T>
176  static typename object_traits<T>::pointer_type
177  _cache_find (database_type&, const typename object_traits<T>::id_type&);
178 
179  template <typename T>
180  static void
181  _cache_erase (const cache_position<T>&);
182 
183  // Notifications. These are called after per-object callbacks for
184  // post_{persist, load, update, erase} events.
185  //
186  template <typename T>
187  static void
189 
190  template <typename T>
191  static void
193 
194  template <typename T>
195  static void
196  _cache_update (database_type&, const T&) {}
197 
198  template <typename T>
199  static void
200  _cache_erase (database_type&, const typename object_traits<T>::id_type&);
201 
202  protected:
204  };
205 }
206 
207 #include <odb/session.ixx>
208 #include <odb/session.txx>
209 
210 #include <odb/post.hxx>
211 
212 #endif // ODB_SESSION_HXX
static void _cache_persist(const cache_position< T > &)
Definition: session.hxx:188
std::map< database_type *, type_map > database_map
Definition: session.hxx:124
static void _cache_update(database_type &, const T &)
Definition: session.hxx:196
const database_map & map() const
Definition: session.hxx:130
iterator pos_
Definition: session.hxx:161
static void current(session &s)
Definition: session.hxx:55
odb::database database_type
Definition: session.hxx:26
Definition: forward.hxx:123
Definition: session.hxx:96
object_map< T > map
Definition: session.hxx:143
static bool has_current()
Definition: session.hxx:45
Definition: session.hxx:23
std::map< const std::type_info *, details::shared_ptr< object_map_base >, details::type_info_comparator > type_map
Definition: session.hxx:122
Definition: database.hxx:38
cache_position()
Definition: session.hxx:146
map * map_
Definition: session.hxx:160
database_map db_map_
Definition: session.hxx:203
Definition: session.hxx:84
database_map & map()
Definition: session.hxx:127
Definition: session.hxx:77
static void _cache_load(const cache_position< T > &)
Definition: session.hxx:192
map::iterator iterator
Definition: session.hxx:144
cache_position(map &m, const iterator &p)
Definition: session.hxx:147
access::object_traits< T >::pointer_type pointer_type
Definition: traits.hxx:116
static void reset_current()
Definition: session.hxx:60