2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-27 19:12:16 +00:00

Add a shared_ptr specialization of value_traits

[SVN r20881]
This commit is contained in:
Raoul Gough
2003-11-20 18:16:06 +00:00
parent 1d295d7f11
commit d5c9831da8
2 changed files with 51 additions and 19 deletions

View File

@@ -22,11 +22,19 @@
#define BOOST_PYTHON_INDEXING_VALUE_TRAITS_HPP
#include <boost/config.hpp>
#include <boost/shared_ptr.hpp>
#include <functional>
namespace boost { namespace python { namespace indexing {
// The value_traits template is used by default by all
// ContainerTraits templates. It can be overridden by specialization
// or by supplying the optional ValueTraits parameter to a container
// traits template.
template<typename T> struct value_traits;
// Implementation for default use
template<typename T>
struct value_traits {
struct default_value_traits {
BOOST_STATIC_CONSTANT (bool, equality_comparable = true);
typedef std::equal_to<T> equal_to;
@@ -37,6 +45,40 @@ namespace boost { namespace python { namespace indexing {
template<typename PythonClass, typename Policy>
static void visitor_helper (PythonClass &, Policy const &) { }
};
// Implementation using pointer indirection
template <typename Ptr>
struct indirect_value_traits : default_value_traits<Ptr> {
// Hide the base class versions of the comparisons, using these
// indirect versions
struct less : std::binary_function<Ptr, Ptr, bool> {
bool operator() (Ptr const &p1, Ptr const &p2) const {
return *p1 < *p2;
}
};
struct equal_to : std::binary_function<Ptr, Ptr, bool> {
bool operator() (Ptr const &p1, Ptr const &p2) const {
return *p1 == *p2;
}
};
};
// Default implementation selection
template<typename T>
struct value_traits
: default_value_traits<T>
{
};
#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
// Partial specialization for instances of boost::shared_ptr
template<typename T>
struct value_traits< ::boost::shared_ptr<T> >
: indirect_value_traits< ::boost::shared_ptr<T> >
{
};
#endif
} } }
#endif // BOOST_PYTHON_INDEXING_VALUE_TRAITS_HPP

View File

@@ -31,23 +31,6 @@ unsigned int_wrapper::our_object_counter = 0;
BOOST_TT_BROKEN_COMPILER_SPEC (boost::shared_ptr<int_wrapper>)
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;
}
};
struct equal_to : std::binary_function<Ptr, Ptr, bool> {
bool operator() (Ptr const &p1, Ptr const &p2) const {
return *p1 == *p2;
}
};
};
BOOST_PYTHON_MODULE(test_vector_shared_ext)
{
namespace indexing = boost::python::indexing;
@@ -66,11 +49,18 @@ BOOST_PYTHON_MODULE(test_vector_shared_ext)
.def ("__cmp__", compare)
;
typedef indirect_value_traits<int_wrapper_holder> value_traits_;
#if !defined (BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
// Partial specialization of value_traits takes care of things
// automatically
typedef indexing::container_suite<Container1> Suite1;
#else
// Otherwise do it the hard way
typedef indexing::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, 0, algorithms_> Suite1;
#endif
boost::python::class_<Container1>("Vector_shared")
.def (Suite1())