// file : odb/cache-traits.hxx // author : Boris Kolpackov // copyright : Copyright (c) 2009-2011 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 #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 () {erase (pos_);} position_type position () const {return pos_;} void release () {pos_.map_ = 0;} private: position_type pos_; }; // Qualify the database type to resolve a phony ambiguity in VC 10. // static position_type insert (odb::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 (odb::database& db, const id_type& id) { if (session::has_current ()) return session::current ().find (db, id); else return pointer_type (); } static void erase (odb::database& db, const id_type& id) { if (session::has_current ()) session::current ().erase (db, id); } static void erase (const position_type& p) { if (p.map_ != 0) session::current ().erase (p); } }; // 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&) {} position_type position () const {return position_type ();} void release () {} }; static position_type insert (odb::database&, const id_type&, const pointer_type&) { return position_type (); } static pointer_type find (odb::database&, const id_type&) { return pointer_type (); } static void erase (odb::database&, const id_type&) {} static void erase (const position_type&) {} }; // Caching traits for objects passed by reference. Only if the object // pointer kind is raw 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; typedef typename pointer_cache_traits::position_type position_type; struct insert_guard { insert_guard (const position_type&) {} position_type position () const {return position_type ();} void release () {} }; static position_type insert (odb::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 (odb::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