aboutsummaryrefslogtreecommitdiff
path: root/odb/session.hxx
blob: 657a869d65df6ce62905573359d1a427f45abe0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// file      : odb/session.hxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_SESSION_HXX
#define ODB_SESSION_HXX

#include <odb/pre.hxx>

#include <map>
#include <typeinfo>

#include <odb/traits.hxx>
#include <odb/forward.hxx>

#include <odb/details/shared-ptr.hxx>
#include <odb/details/type-info.hxx>

#include <odb/details/export.hxx>

namespace odb
{
  class LIBODB_EXPORT session
  {
  public:
    typedef odb::database database_type;

    // If the make_current argument is true, then set the current thread's
    // session to this session. If another session is already in effect,
    // throw the already_in_session exception.
    //
    session (bool make_current = true);

    // Reset the current thread's session if it is this session.
    //
    ~session ();

    // Current session.
    //
  public:
    // Return true if there is a session in effect in the current
    // thread.
    //
    static bool
    has_current () {return current_pointer () != 0;}

    // Get current thread's session. Throw if no session is in effect.
    //
    static session&
    current ();

    // Set current thread's session.
    //
    static void
    current (session& s) {current_pointer (&s);}

    // Revert to the no session in effect state for the current thread.
    //
    static void
    reset_current () {current_pointer (0);}

    // Pointer versions.
    //
    static session*
    current_pointer ();

    static void
    current_pointer (session*);

    // Copying or assignment of sessions is not supported.
    //
  private:
    session (const session&);
    session& operator= (const session&);

  public:
    struct LIBODB_EXPORT object_map_base: details::shared_base
    {
      virtual
      ~object_map_base ();
    };

    template <typename T>
    struct object_map: object_map_base,
                       std::map<typename object_traits<T>::id_type,
                                typename object_traits<T>::pointer_type>
    {
    };

    // Object cache.
    //
  public:
    // Position in the cache of the inserted element.
    //
    template <typename T>
    struct cache_position;

    template <typename T>
    cache_position<T>
    cache_insert (database_type&,
                  const typename object_traits<T>::id_type&,
                  const typename object_traits<T>::pointer_type&);

    template <typename T>
    typename object_traits<T>::pointer_type
    cache_find (database_type&,
                const typename object_traits<T>::id_type&) const;

    template <typename T>
    void
    cache_erase (const cache_position<T>&);

    template <typename T>
    void
    cache_erase (database_type&, const typename object_traits<T>::id_type&);

    // Low-level object cache access (iteration, etc).
    //
  public:
    typedef std::map<const std::type_info*,
                     details::shared_ptr<object_map_base>,
                     details::type_info_comparator> type_map;

    typedef std::map<database_type*, type_map> database_map;

    database_map&
    map () {return db_map_;}

    const database_map&
    map () const {return db_map_;}

    // Static cache API as expected by the rest of ODB.
    //
  public:
    // Position in the cache of the inserted element. The requirements
    // for this class template are: default and copy-constructible as
    // well as copy-assignable. The default constructor creates an
    // empty/NULL position.
    //
    template <typename T>
    struct cache_position
    {
      typedef object_map<T> map;
      typedef typename map::iterator iterator;

      cache_position (): map_ (0) {}
      cache_position (map& m, const iterator& p): map_ (&m), pos_ (p) {}

      map* map_;
      iterator pos_;
    };

    // The following cache management functions are all static to
    // allow for a custom notion of a current session. The erase()
    // function is called to remove the object if the operation
    // that caused it to be inserted (e.g., load) failed.
    //
    template <typename T>
    static cache_position<T>
    _cache_insert (database_type&,
                   const typename object_traits<T>::id_type&,
                   const typename object_traits<T>::pointer_type&);

    template <typename T>
    static typename object_traits<T>::pointer_type
    _cache_find (database_type&, const typename object_traits<T>::id_type&);

    template <typename T>
    static void
    _cache_erase (const cache_position<T>&);

    // Notifications. These are called after per-object callbacks for
    // post_{persist, load, update, erase} events.
    //
    template <typename T>
    static void
    _cache_persist (const cache_position<T>&) {}

    template <typename T>
    static void
    _cache_load (const cache_position<T>&) {}

    template <typename T>
    static void
    _cache_update (database_type&, const T&) {}

    template <typename T>
    static void
    _cache_erase (database_type&, const typename object_traits<T>::id_type&);

  protected:
    database_map db_map_;
  };
}

#include <odb/session.ixx>
#include <odb/session.txx>

#include <odb/post.hxx>

#endif // ODB_SESSION_HXX