From 658a2f07b47ed80bd3ca35edd7380493c326daa3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 27 Oct 2011 15:16:49 +0200 Subject: Add support for persistent classes without object ids New pragma id (object). New test: common/no-id. --- odb/cache-traits.hxx | 127 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 117 insertions(+), 10 deletions(-) (limited to 'odb/cache-traits.hxx') diff --git a/odb/cache-traits.hxx b/odb/cache-traits.hxx index e4b2495..067812b 100644 --- a/odb/cache-traits.hxx +++ b/odb/cache-traits.hxx @@ -17,8 +17,19 @@ namespace odb { // Caching traits for objects passed by pointer. // - template ::kind> - struct pointer_cache_traits + template + struct pointer_cache_traits_impl; + + template + struct pointer_cache_traits: pointer_cache_traits_impl< + P, + typename object_traits::element_type>::id_type, + pointer_traits

::kind> + { + }; + + template + struct pointer_cache_traits_impl { typedef P pointer_type; typedef odb::pointer_traits pointer_traits; @@ -45,20 +56,36 @@ namespace odb position_type pos_; }; + // We need the insert() overload with explicit id to handle self- + // references. In such cases the object is not yet loaded and the + // id member does not contain the correct id. + // // 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 ()) + { // Cast away constness if any. // return session::current ().insert ( db, id, pointer_traits::cast (p)); + } else return position_type (); } + static position_type + insert (odb::database& db, const pointer_type& p) + { + const id_type& id ( + object_traits::id ( + pointer_traits::get_ref (p))); + + return insert (db, id, p); + } + static pointer_type find (odb::database& db, const id_type& id) { @@ -83,10 +110,23 @@ namespace odb } }; + template + struct pointer_cache_traits_impl + { + typedef P pointer_type; + struct position_type {}; + + static position_type + insert (odb::database&, const pointer_type&) + { + return position_type (); + } + }; + // Unique pointers don't work with the object cache. // - template - struct pointer_cache_traits + template + struct pointer_cache_traits_impl { typedef P pointer_type; typedef typename pointer_traits::element_type element_type; @@ -110,6 +150,12 @@ namespace odb return position_type (); } + static position_type + insert (odb::database&, const pointer_type&) + { + return position_type (); + } + static pointer_type find (odb::database&, const id_type&) { return pointer_type (); } @@ -120,13 +166,35 @@ namespace odb erase (const position_type&) {} }; + template + struct pointer_cache_traits_impl + { + typedef P pointer_type; + struct position_type {}; + + static position_type + insert (odb::database&, const pointer_type&) + { + return 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 + template + struct reference_cache_traits_impl; + + template + struct reference_cache_traits: reference_cache_traits_impl< + T, + typename object_traits::id_type, + pointer_traits::pointer_type>::kind> + { + }; + + template + struct reference_cache_traits_impl { typedef T element_type; typedef typename object_traits::pointer_type pointer_type; @@ -152,10 +220,29 @@ namespace odb { return position_type (); } + + static position_type + insert (odb::database&, element_type&) + { + return position_type (); + } }; - template - struct reference_cache_traits + template + struct reference_cache_traits_impl + { + typedef T element_type; + struct position_type {}; + + static position_type + insert (odb::database&, element_type&) + { + return position_type (); + } + }; + + template + struct reference_cache_traits_impl { typedef T element_type; typedef typename object_traits::pointer_type pointer_type; @@ -175,6 +262,26 @@ namespace odb pointer_type p (&obj); return pointer_cache_traits::insert (db, id, p); } + + static position_type + insert (odb::database& db, element_type& obj) + { + pointer_type p (&obj); + return pointer_cache_traits::insert (db, p); + } + }; + + template + struct reference_cache_traits_impl + { + typedef T element_type; + struct position_type {}; + + static position_type + insert (odb::database&, element_type&) + { + return position_type (); + } }; } -- cgit v1.1