diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2012-04-23 16:48:00 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2012-04-23 16:48:00 +0200 |
commit | 1896d36996ab48ed7271e855d7e32b4e61f64896 (patch) | |
tree | 6d085eb0349d23bc26fe101b2451235939f17d40 /odb/polymorphic-object-result.txx | |
parent | c2be6303f97e470960ee77805128c46d3ea5c102 (diff) |
Polymorphic inheritance support
Diffstat (limited to 'odb/polymorphic-object-result.txx')
-rw-r--r-- | odb/polymorphic-object-result.txx | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/odb/polymorphic-object-result.txx b/odb/polymorphic-object-result.txx new file mode 100644 index 0000000..28331c6 --- /dev/null +++ b/odb/polymorphic-object-result.txx @@ -0,0 +1,76 @@ +// file : odb/polymorphic-object-result.txx +// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#include <odb/cache-traits.hxx> +#include <odb/exceptions.hxx> + +namespace odb +{ + // + // polymorphic_object_result_impl + // + + template <typename T> + polymorphic_object_result_impl<T>:: + ~polymorphic_object_result_impl () + { + } + + template <typename T> + void polymorphic_object_result_impl<T>:: + load () + { + typedef typename root_traits::pointer_type root_pointer_type; + typedef typename root_traits::pointer_traits root_pointer_traits; + + // First check the session. + // + const id_type& id (load_id ()); + + root_pointer_type rp ( + object_traits::pointer_cache_traits::find (database (), id)); + + if (!root_pointer_traits::null_ptr (rp)) + { + // Check if the types match. + // + pointer_type p ( + root_pointer_traits::template dynamic_pointer_cast< + object_type> (rp)); + + if (!pointer_traits::null_ptr (p)) + current (p, false); // Pointer from cache should not be guarded. + else + // We have an object in session that has a different type + // compared to the one in the database. + // + throw object_not_persistent (); // @@ type_mismatch? + } + else + // load() is responsible for creating the object of a correct + // dynamic type and for object cache insertion. + // + load (0, false); + } + + // + // object_result_iterator + // + + template <typename T, typename ID> + void object_result_iterator<T, ID, true>:: + load (object_type& obj) + { + if (res_->end ()) + return; + + typedef odb::object_traits<object_type> object_traits; + + typename object_traits::reference_cache_traits::insert_guard ig ( + object_traits::reference_cache_traits::insert ( + res_->database (), res_->load_id (), obj)); + res_->load (&obj, false); + ig.release (); + } +} |