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/view-result.hxx | 57 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) (limited to 'odb/view-result.hxx') diff --git a/odb/view-result.hxx b/odb/view-result.hxx index 088969a..ef9ffd8 100644 --- a/odb/view-result.hxx +++ b/odb/view-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::view_traits::pointer_type pointer_type; - typedef odb::pointer_traits pointer_traits; - - typedef typename odb::view_traits::view_type view_type; + // In result_impl, T is always non-const and the same as view_type. + // + typedef T view_type; typedef odb::view_traits view_traits; + typedef typename view_traits::pointer_type pointer_type; + typedef odb::pointer_traits pointer_traits; + result_impl (database_type& db) : begin_ (true), end_ (false), db_ (db), current_ () { @@ -119,11 +123,11 @@ namespace odb typedef std::ptrdiff_t difference_type; typedef std::input_iterator_tag iterator_category; - // Const views are not supported, so this should be the same as T. + // T can be const T while view_type is always non-const. // typedef typename view_traits::view_type view_type; - typedef result_impl result_impl_type; + typedef result_impl result_impl_type; public: explicit @@ -187,8 +191,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_; @@ -229,7 +236,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 view_type is always non-const. + // + typedef typename view_traits::view_type view_type; + typedef result_impl result_impl_type; public: result () @@ -262,6 +272,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) { @@ -320,6 +359,8 @@ namespace odb } private: + friend class result; + details::shared_ptr impl_; }; } -- cgit v1.1