2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

Replacement value_traits to handle comparisons via indirection

[SVN r20856]
This commit is contained in:
Raoul Gough
2003-11-19 12:46:43 +00:00
parent 922a1b9194
commit 712b90dfe0

View File

@@ -16,19 +16,6 @@
#include "int_wrapper.hpp"
#include <boost/shared_ptr.hpp>
#include <boost/type_traits/broken_compiler_spec.hpp>
typedef boost::shared_ptr<int_wrapper> 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<int_wrapper>)
#include <boost/python/suite/indexing/container_suite.hpp>
#include <boost/python/suite/indexing/vector.hpp>
#include <vector>
@@ -36,79 +23,38 @@ BOOST_TT_BROKEN_COMPILER_SPEC (boost::shared_ptr<int_wrapper>)
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/implicit.hpp>
#include <functional>
// 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<int_wrapper>)
bool operator== (int_wrapper_holder const &lhs, int_wrapper_holder const &rhs)
{
return (*lhs) == (*rhs);
}
template <typename Ptr>
struct indirect_value_traits : boost::python::indexing::value_traits<Ptr> {
// Hide the base class versions of the comparisons, using
// indirect versions
struct less : std::binary_function<Ptr, Ptr, bool> {
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<int_wrapper_holder> 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<Ptr, Ptr, bool> {
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<Container1>
, msvc6_vector_shared_algorithms
>
{
typedef indexing::default_algorithms <
indexing::default_sequence_traits<Container1>
, msvc6_vector_shared_algorithms
> base_type;
typedef msvc6_vector_shared_algorithms self_type;
// key_param will be boost::shared_ptr<int_wrapper> 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> int_wrapper_holder;
typedef std::vector<int_wrapper_holder> Container1;
boost::python::implicitly_convertible <int, int_wrapper>();
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<Container1> Suite1;
#else
// For any other compilers that don't have partial specialization
typedef indexing::vector_suite<Container1> Suite1;
#endif
typedef indirect_value_traits<int_wrapper_holder> value_traits_;
typedef indexing::default_sequence_traits<Container1, value_traits_>
container_traits_;
typedef indexing::default_algorithms<container_traits_> algorithms_;
typedef indexing::container_suite<Container1, algorithms_> Suite1;
boost::python::class_<Container1>("Vector_shared")
.def (Suite1())