From 7430b7c4a6cecf10b759608dd2a816c02ed17754 Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Thu, 20 Nov 2003 18:20:33 +0000 Subject: [PATCH] Custom less and equal_to for element_proxy specialization of value_traits [SVN r20882] --- .../python/suite/indexing/container_proxy.hpp | 13 ++-- .../suite/indexing/element_proxy_traits.hpp | 59 +++++++++++++++---- 2 files changed, 55 insertions(+), 17 deletions(-) diff --git a/include/boost/python/suite/indexing/container_proxy.hpp b/include/boost/python/suite/indexing/container_proxy.hpp index 96c60f44..31b7af6b 100755 --- a/include/boost/python/suite/indexing/container_proxy.hpp +++ b/include/boost/python/suite/indexing/container_proxy.hpp @@ -769,20 +769,21 @@ namespace boost { namespace python { namespace indexing { typedef typename BOOST_PYTHON_INDEXING_CALL_TRAITS ::param_type key_param; -#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) - typedef value_traits value_traits_; -#else +#if defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) + // value_traits for the reference type (i.e. our element_proxy + // instance) supplies a custom visitor_helper. Compilers without + // partial specialization need help here. + typedef element_proxy_traits value_traits_; - // Forward visitor_helper to eleemnt_proxy_traits + // Hide base class visitor_helper, which would call the + // unspecialized value_traits version template static void visitor_helper (PythonClass &pyClass, Policy const &policy) { value_traits_::visitor_helper (pyClass, policy); } #endif - // Get value_traits for the reference type (i.e. element_proxy) - // to get the custom visitor_helper }; #if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) diff --git a/include/boost/python/suite/indexing/element_proxy_traits.hpp b/include/boost/python/suite/indexing/element_proxy_traits.hpp index c0aee1d4..4c934a08 100755 --- a/include/boost/python/suite/indexing/element_proxy_traits.hpp +++ b/include/boost/python/suite/indexing/element_proxy_traits.hpp @@ -1,4 +1,3 @@ -// // Header file element_proxy_traits.hpp // // Copyright (c) 2003 Raoul M. Gough @@ -7,6 +6,12 @@ // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy // at http://www.boost.org/LICENSE_1_0.txt) // +// This is a separate header so that element_proxy.hpp is not +// dependant on register_ptr_to_python.hpp. This avoids a problem with +// two-phase name lookup, where register_ptr_to_python must be +// included *after* the element_proxy overload of boost::get_pointer +// is defined. +// // History // ======= // 2003/10/23 rmg File creation @@ -23,28 +28,60 @@ #include namespace boost { namespace python { namespace indexing { -#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) - template - struct value_traits > - : public value_traits < - BOOST_DEDUCED_TYPENAME ContainerProxy::raw_value_type> -#else template struct element_proxy_traits : public value_traits < BOOST_DEDUCED_TYPENAME ContainerProxy::raw_value_type> -#endif { + typedef element_proxy element_proxy_; + typedef typename ContainerProxy::raw_value_type raw_value_type; + typedef value_traits base_type; + + // Wrap the base class versions of the comparisons using + // indirection + struct less + : std::binary_function + { + typename base_type::less m_base_compare; + + bool operator() ( + element_proxy_ const &p1, element_proxy_ const &p2) const + { + return m_base_compare (*p1, *p2); + } + }; + + struct equal_to + : std::binary_function + { + // First param is raw_value_type to interface smoothly with the + // bind1st used in default_algorithms::find + + typename base_type::equal_to m_base_compare; + + bool operator() ( + raw_value_type const &v, element_proxy_ const &p) const + { + return m_base_compare (v, *p); + } + }; + template static void visitor_helper (PythonClass &, Policy const &) { - typedef element_proxy element_proxy_; - typedef typename ContainerProxy::raw_value_type raw_value_type; - boost::python::register_ptr_to_python(); boost::python::implicitly_convertible(); } }; + +#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) + // value_traits partial specialization for element_proxy instances + template + struct value_traits > + : element_proxy_traits + { + }; +#endif } } } #endif // BOOST_PYTHON_INDEXING_ELEMENT_PROXY_TRAITS_HPP