From 531c792dd4eecd246cc1ccebac812d6888464a78 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 22 Nov 2010 12:03:11 +0200 Subject: 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. --- odb/session.hxx | 159 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 odb/session.hxx (limited to 'odb/session.hxx') 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 +// 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 + +#include +#include + +#include +#include + +#include +#include + +#include + +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 + struct object_pointers + { + typedef typename object_traits::pointer_type pointer_type; + typedef typename object_traits::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 + struct object_map: + object_map_base, + std::map< typename object_traits::id_type, object_pointers > + { + }; + + // Object cache. + // + public: + template + struct object_position + { + typedef typename object_traits::object_type object_type; + typedef object_map 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 + object_position + insert (database_type&, + const typename object_traits::id_type&, + const typename object_traits::pointer_type&); + + template + typename object_traits::pointer_type + find (database_type&, const typename object_traits::id_type&) const; + + template + void + erase (database_type&, const typename object_traits::id_type&); + + template + void + erase (const object_position&); + + protected: + typedef std::map, + details::type_info_comparator> type_map; + + typedef std::map database_map; + + database_map db_map_; + }; +} + +#include +#include + +#include + +#endif // ODB_SESSION_HXX -- cgit v1.1