aboutsummaryrefslogtreecommitdiff
path: root/odb/cache-traits.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/cache-traits.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/cache-traits.hxx')
-rw-r--r--odb/cache-traits.hxx152
1 files changed, 152 insertions, 0 deletions
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 <boris@codesynthesis.com>
+// 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 <odb/pre.hxx>
+
+#include <odb/traits.hxx>
+#include <odb/session.hxx>
+#include <odb/pointer-traits.hxx>
+
+namespace odb
+{
+ // Caching traits for objects passed by pointer.
+ //
+ template <typename P, pointer_kind = pointer_traits<P>::kind>
+ struct pointer_cache_traits
+ {
+ typedef P pointer_type;
+ typedef typename pointer_traits<pointer_type>::element_type element_type;
+ typedef typename object_traits<element_type>::id_type id_type;
+ typedef session::object_position<element_type> position_type;
+
+ struct insert_guard
+ {
+ insert_guard (const position_type& pos): pos_ (pos) {}
+
+ ~insert_guard ()
+ {
+ if (pos_.map_ != 0)
+ session::current ().erase<element_type> (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<element_type> (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<element_type> (db, id);
+ else
+ return pointer_type ();
+ }
+
+ static void
+ erase (database& db, const id_type& id)
+ {
+ if (session::has_current ())
+ session::current ().erase<element_type> (db, id);
+ }
+ };
+
+ // Unique pointers don't work with the object cache.
+ //
+ template <typename P>
+ struct pointer_cache_traits<P, pk_unique>
+ {
+ typedef P pointer_type;
+ typedef typename pointer_traits<pointer_type>::element_type element_type;
+ typedef typename object_traits<element_type>::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 <typename T,
+ pointer_kind =
+ pointer_traits<typename object_traits<T>::pointer_type>::kind>
+ struct reference_cache_traits
+ {
+ typedef T element_type;
+ typedef typename object_traits<element_type>::pointer_type pointer_type;
+ typedef typename object_traits<element_type>::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 <typename T>
+ struct reference_cache_traits<T, pk_naked>
+ {
+ typedef T element_type;
+ typedef typename object_traits<element_type>::pointer_type pointer_type;
+ typedef typename object_traits<element_type>::id_type id_type;
+
+ typedef
+ typename pointer_cache_traits<pointer_type>::position_type
+ position_type;
+
+ typedef
+ typename pointer_cache_traits<pointer_type>::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<pointer_type>::insert (db, id, p);
+ }
+ };
+}
+
+#include <odb/post.hxx>
+
+#endif // ODB_CACHE_TRAITS_HXX