From 712b90dfe01cf6b6b71a21a03f358e414e839d13 Mon Sep 17 00:00:00 2001 From: Raoul Gough Date: Wed, 19 Nov 2003 12:46:43 +0000 Subject: [PATCH] Replacement value_traits to handle comparisons via indirection [SVN r20856] --- test/test_vector_shared.cpp | 114 ++++++++---------------------------- 1 file changed, 26 insertions(+), 88 deletions(-) diff --git a/test/test_vector_shared.cpp b/test/test_vector_shared.cpp index 5557b21b..20b8b573 100755 --- a/test/test_vector_shared.cpp +++ b/test/test_vector_shared.cpp @@ -16,19 +16,6 @@ #include "int_wrapper.hpp" #include #include - -typedef boost::shared_ptr int_wrapper_holder; - -#if !BOOST_WORKAROUND (BOOST_MSVC, == 1200) -// Declare overloads for correct sort and find of int_wrappers via -// shared pointers -bool operator< (int_wrapper_holder const &, int_wrapper_holder const &); -bool operator== (int_wrapper_holder const &, int_wrapper_holder const &); -bool operator!= (int_wrapper_holder const &, int_wrapper_holder const &); -#endif - -BOOST_TT_BROKEN_COMPILER_SPEC (boost::shared_ptr) - #include #include #include @@ -36,79 +23,38 @@ BOOST_TT_BROKEN_COMPILER_SPEC (boost::shared_ptr) #include #include #include +#include // More messiness from not having a separate int_wrapper.cpp file bool int_wrapper::our_trace_flag = true; unsigned int_wrapper::our_object_counter = 0; -#if !BOOST_WORKAROUND (BOOST_MSVC, == 1200) -bool operator< (int_wrapper_holder const &lhs, int_wrapper_holder const &rhs) -{ - return (*lhs) < (*rhs); -} +BOOST_TT_BROKEN_COMPILER_SPEC (boost::shared_ptr) -bool operator== (int_wrapper_holder const &lhs, int_wrapper_holder const &rhs) -{ - return (*lhs) == (*rhs); -} +template +struct indirect_value_traits : boost::python::indexing::value_traits { + // Hide the base class versions of the comparisons, using + // indirect versions + struct less : std::binary_function { + bool operator() (Ptr const &p1, Ptr const &p2) const { + return *p1 < *p2; + } + }; -bool operator!= (int_wrapper_holder const &lhs, int_wrapper_holder const &rhs) -{ - return (*lhs) != (*rhs); -} -#endif - -namespace indexing = boost::python::indexing; - -typedef std::vector Container1; - -#if BOOST_WORKAROUND (BOOST_MSVC, == 1200) -// Unfortunately, the comparison operator overloads don't work under -// MSVC6, so we have to use a sledgehammer and replace the -// implementations of some of the search functions - -struct bound_compare { - int_wrapper_holder m_lhs; - bound_compare (int_wrapper_holder const &lhs) : m_lhs (lhs) { } - bool operator()(int_wrapper_holder const &rhs) { return (*m_lhs) == (*rhs); } + struct equal_to : std::binary_function { + bool operator() (Ptr const &p1, Ptr const &p2) const { + return *p1 == *p2; + } + }; }; -struct msvc6_vector_shared_algorithms - : public indexing::default_algorithms < - indexing::default_sequence_traits - , msvc6_vector_shared_algorithms - > -{ - typedef indexing::default_algorithms < - indexing::default_sequence_traits - , msvc6_vector_shared_algorithms - > base_type; - - typedef msvc6_vector_shared_algorithms self_type; - - // key_param will be boost::shared_ptr or ref to same - - static bool less_than (key_param lhs, key_param rhs) { - return (*lhs) < (*rhs); - } - - static iterator find (container &c, key_param k) { - return std::find_if (c.begin(), c.end(), bound_compare (k)); - } - - static size_type count (container &c, key_param k) { - return std::count_if (c.begin(), c.end(), bound_compare (k)); - } - - static void sort (container &c) { - std::sort (c.begin(), c.end(), self_type::less_than); - } -}; - -#endif - BOOST_PYTHON_MODULE(test_vector_shared_ext) { + namespace indexing = boost::python::indexing; + + typedef boost::shared_ptr int_wrapper_holder; + typedef std::vector Container1; + boost::python::implicitly_convertible (); boost::python::def ("setTrace", &int_wrapper::setTrace); @@ -120,19 +66,11 @@ BOOST_PYTHON_MODULE(test_vector_shared_ext) .def ("__cmp__", compare) ; -#if BOOST_WORKAROUND (BOOST_MSVC, == 1200) - // MSVC6 version here - typedef indexing::container_suite < - Container1, msvc6_vector_shared_algorithms> Suite1; - -#elif !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) - // Normal version here - typedef indexing::container_suite Suite1; - -#else - // For any other compilers that don't have partial specialization - typedef indexing::vector_suite Suite1; -#endif + typedef indirect_value_traits value_traits_; + typedef indexing::default_sequence_traits + container_traits_; + typedef indexing::default_algorithms algorithms_; + typedef indexing::container_suite Suite1; boost::python::class_("Vector_shared") .def (Suite1())