diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-21 13:00:33 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2011-09-21 13:00:33 +0200 |
commit | a28444da4ca6adb016f719e032174ccb54e1692e (patch) | |
tree | 75fe8ceae2d20baa3d4b5375a832598efe8b2fce /odb/view-result.hxx | |
parent | 0046a34d5ad5fc1e88b33279605f179646b0ea59 (diff) |
Rework const object handling
Now objects are always loaded as non-const and the object cache in
session treats all objects as non-const.
Diffstat (limited to 'odb/view-result.hxx')
-rw-r--r-- | odb/view-result.hxx | 57 |
1 files changed, 49 insertions, 8 deletions
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<T, class_view>; + friend class result<const T, class_view>; friend class result_iterator<T, class_view>; + friend class result_iterator<const T, class_view>; typedef odb::database database_type; - typedef typename odb::view_traits<T>::pointer_type pointer_type; - typedef odb::pointer_traits<pointer_type> pointer_traits; - - typedef typename odb::view_traits<T>::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_type> view_traits; + typedef typename view_traits::pointer_type pointer_type; + typedef odb::pointer_traits<pointer_type> 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<T>::view_type view_type; - typedef result_impl<T, class_view> result_impl_type; + typedef result_impl<view_type, class_view> 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<typename view_traits<T>::pointer_type> + odb::pointer_traits<typename view_traits<view_type>::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<T, class_view> result_impl_type; + // T can be const T while view_type is always non-const. + // + typedef typename view_traits<T>::view_type view_type; + typedef result_impl<view_type, class_view> result_impl_type; public: result () @@ -262,6 +272,35 @@ namespace odb return *this; } + // Conversion from result<T> to result<const T>. + // + template <typename UT> + result (const result<UT, class_view>& 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<const obj> to result<obj>. + // + : impl_ (r.impl_) + { + } + + template <typename UT> + result& + operator= (const result<UT, class_view>& 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<const obj> to result<obj>. + // + if (impl_ != r.impl_) + impl_ = r.impl_; + + return *this; + } + void swap (result& r) { @@ -320,6 +359,8 @@ namespace odb } private: + friend class result<const T, class_view>; + details::shared_ptr<result_impl_type> impl_; }; } |