aboutsummaryrefslogtreecommitdiff
path: root/odb/session.hxx
blob: 393d22f541d509faeb748f44df7f3ae3e3558034 (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
// file      : odb/session.hxx
// copyright : Copyright (c) 2009-2011 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;

    // Set the current thread's session to this session. If another
    // session is already in effect, throw the already_in_session
    // exception.
    //
    session ();

    // 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 ();

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

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

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

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

  protected:
    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:
    template <typename T>
    struct object_position
    {
      typedef T object_type;
      typedef object_map<object_type> map;
      typedef typename map::iterator iterator;

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

      map* map_;
      iterator pos_;
    };

    template <typename T>
    object_position<T>
    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
    find (database_type&, const typename object_traits<T>::id_type&) const;

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

    template <typename T>
    void
    erase (const object_position<T>&);

  protected:
    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 db_map_;
  };
}

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

#include <odb/post.hxx>

#endif // ODB_SESSION_HXX