From a28444da4ca6adb016f719e032174ccb54e1692e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 21 Sep 2011 13:00:33 +0200 Subject: Rework const object handling Now objects are always loaded as non-const and the object cache in session treats all objects as non-const. --- odb/object-result.hxx | 59 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 9 deletions(-) (limited to 'odb/object-result.hxx') diff --git a/odb/object-result.hxx b/odb/object-result.hxx index fcee125..235b682 100644 --- a/odb/object-result.hxx +++ b/odb/object-result.hxx @@ -28,16 +28,20 @@ namespace odb protected: friend class result; + friend class result; friend class result_iterator; + friend class result_iterator; typedef odb::database database_type; - typedef typename odb::object_traits::pointer_type pointer_type; - typedef odb::pointer_traits pointer_traits; - - typedef typename odb::object_traits::object_type object_type; - typedef typename odb::object_traits::id_type id_type; + // In result_impl, T is always non-const and the same as object_type. + // + typedef T object_type; typedef odb::object_traits object_traits; + typedef typename object_traits::id_type id_type; + + typedef typename object_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; result_impl (database_type& db) : begin_ (true), end_ (false), db_ (db), current_ () @@ -123,12 +127,12 @@ namespace odb typedef std::ptrdiff_t difference_type; typedef std::input_iterator_tag iterator_category; - // T might be const T, but object_type is always T. + // T can be const T while object_type is always non-const. // typedef typename object_traits::object_type object_type; typedef typename object_traits::id_type id_type; - typedef result_impl result_impl_type; + typedef result_impl result_impl_type; public: explicit @@ -192,8 +196,11 @@ namespace odb } private: + // Use unrestricted pointer traits since that's what is returned by + // result_impl. + // typedef - odb::pointer_traits::pointer_type> + odb::pointer_traits::pointer_type> pointer_traits; result_impl_type* res_; @@ -234,7 +241,10 @@ namespace odb typedef std::size_t size_type; typedef std::ptrdiff_t difference_type; - typedef result_impl result_impl_type; + // T can be const T while object_type is always non-const. + // + typedef typename object_traits::object_type object_type; + typedef result_impl result_impl_type; public: result () @@ -267,6 +277,35 @@ namespace odb return *this; } + // Conversion from result to result. + // + template + result (const result& r) + // + // If you get a compiler error pointing to the line below saying + // that the impl_ member is inaccessible, then you are most likely + // trying to perform an illegal result conversion, for example, + // from result to result. + // + : impl_ (r.impl_) + { + } + + template + result& + operator= (const result& r) + { + // If you get a compiler error pointing to the line below saying + // that the impl_ member is inaccessible, then you are most likely + // trying to perform an illegal result conversion, for example, + // from result to result. + // + if (impl_ != r.impl_) + impl_ = r.impl_; + + return *this; + } + void swap (result& r) { @@ -325,6 +364,8 @@ namespace odb } private: + friend class result; + details::shared_ptr impl_; }; } -- cgit v1.1