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/cache-traits.hxx | 152 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 odb/cache-traits.hxx (limited to 'odb/cache-traits.hxx') diff --git a/odb/cache-traits.hxx b/odb/cache-traits.hxx new file mode 100644 index 0000000..5d32624 --- /dev/null +++ b/odb/cache-traits.hxx @@ -0,0 +1,152 @@ +// file : odb/cache-traits.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_CACHE_TRAITS_HXX +#define ODB_CACHE_TRAITS_HXX + +#include + +#include +#include +#include + +namespace odb +{ + // Caching traits for objects passed by pointer. + // + template ::kind> + struct pointer_cache_traits + { + typedef P pointer_type; + typedef typename pointer_traits::element_type element_type; + typedef typename object_traits::id_type id_type; + typedef session::object_position position_type; + + struct insert_guard + { + insert_guard (const position_type& pos): pos_ (pos) {} + + ~insert_guard () + { + if (pos_.map_ != 0) + session::current ().erase (pos_); + } + + void + release () {pos_.map_ = 0;} + + private: + position_type pos_; + }; + + static position_type + insert (database& db, const id_type& id, const pointer_type& p) + { + if (session::has_current ()) + return session::current ().insert (db, id, p); + else + return position_type (); + } + + static pointer_type + find (database& db, const id_type& id) + { + if (session::has_current ()) + return session::current ().find (db, id); + else + return pointer_type (); + } + + static void + erase (database& db, const id_type& id) + { + if (session::has_current ()) + session::current ().erase (db, id); + } + }; + + // Unique pointers don't work with the object cache. + // + template + struct pointer_cache_traits + { + typedef P pointer_type; + typedef typename pointer_traits::element_type element_type; + typedef typename object_traits::id_type id_type; + struct position_type {}; + + struct insert_guard + { + insert_guard (const position_type&) {} + void release () {} + }; + + static position_type + insert (database&, const id_type&, const pointer_type&) + { + return position_type (); + } + + static pointer_type + find (database&, const id_type&) { return pointer_type (); } + + static void + erase (database&, const id_type&) {} + }; + + // Caching traits for objects passed by reference. Only if the object + // pointer kind is naked do we add the object to the session. + // + template ::pointer_type>::kind> + struct reference_cache_traits + { + typedef T element_type; + typedef typename object_traits::pointer_type pointer_type; + typedef typename object_traits::id_type id_type; + + struct position_type {}; + + struct insert_guard + { + insert_guard (const position_type&) {} + void release () {} + }; + + static position_type + insert (database&, const id_type&, element_type&) + { + return position_type (); + } + }; + + template + struct reference_cache_traits + { + typedef T element_type; + typedef typename object_traits::pointer_type pointer_type; + typedef typename object_traits::id_type id_type; + + typedef + typename pointer_cache_traits::position_type + position_type; + + typedef + typename pointer_cache_traits::insert_guard + insert_guard; + + static position_type + insert (database& db, const id_type& id, element_type& obj) + { + pointer_type p (&obj); + return pointer_cache_traits::insert (db, id, p); + } + }; +} + +#include + +#endif // ODB_CACHE_TRAITS_HXX -- cgit v1.1