aboutsummaryrefslogtreecommitdiff
path: root/odb/session.hxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2010-11-22 12:03:11 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2010-11-22 12:03:11 +0200
commit531c792dd4eecd246cc1ccebac812d6888464a78 (patch)
tree9bed050c98a2c407c68e808ae1f1d296a65c5fee /odb/session.hxx
parent1cddfd09a7007f77fc243f178b1ca88ea4d0f4f6 (diff)
Add session, database operations on pointers and const objects
Currently, session is just an object cache. The persist, update, and erase database operations are overloaded to also work on object pointers. All the database operations and the query facility now support const objects. New session-related exceptions: not_in_session, already_in_session, const_object.
Diffstat (limited to 'odb/session.hxx')
-rw-r--r--odb/session.hxx159
1 files changed, 159 insertions, 0 deletions
diff --git a/odb/session.hxx b/odb/session.hxx
new file mode 100644
index 0000000..033934a
--- /dev/null
+++ b/odb/session.hxx
@@ -0,0 +1,159 @@
+// file : odb/session.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2009-2010 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:
+ template <typename T>
+ struct object_pointers
+ {
+ typedef typename object_traits<T>::pointer_type pointer_type;
+ typedef typename object_traits<T>::const_pointer_type const_pointer_type;
+
+ object_pointers ();
+
+ void
+ set (const pointer_type&);
+
+ void
+ set (const const_pointer_type&);
+
+ void
+ get (pointer_type& p) const;
+
+ void
+ get (const_pointer_type& cp) const;
+
+ private:
+ pointer_type p_;
+ const_pointer_type cp_;
+ };
+
+ 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, object_pointers<T> >
+ {
+ };
+
+ // Object cache.
+ //
+ public:
+ template <typename T>
+ struct object_position
+ {
+ typedef typename object_traits<T>::object_type 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