2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-28 07:22:31 +00:00

Allow client replacement of value_traits, including less and equal_to members

[SVN r20855]
This commit is contained in:
Raoul Gough
2003-11-19 12:45:29 +00:00
parent 3cadfa529e
commit 922a1b9194
5 changed files with 68 additions and 35 deletions

View File

@@ -28,27 +28,11 @@
#include <boost/mpl/if.hpp>
#include <boost/limits.hpp>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include <string>
namespace boost { namespace python { namespace indexing {
namespace detail {
struct no_override { };
template<typename Base, typename Override>
struct maybe_override
{
// Probably need to disable this if there is no partial
// specialization support, because Override is almost certain to
// be an incomplete type. If that is right, the workaround
// version would just have to do "typedef Base type;"
typedef typename mpl::if_
<is_same <Override, no_override>, Base, Override>
::type type;
};
}
template<typename ContainerTraits, typename Ovr = detail::no_override>
class default_algorithms
{
@@ -233,7 +217,13 @@ namespace boost { namespace python { namespace indexing {
default_algorithms<ContainerTraits, Ovr>::find (
container &c, key_param key)
{
return std::find (most_derived::begin(c), most_derived::end(c), key);
typedef typename container_traits::value_traits_ value_traits_;
typedef typename value_traits_::equal_to comparison;
return std::find_if (
most_derived::begin(c)
, most_derived::end(c)
, std::bind1st (comparison(), key));
}
/////////////////////////////////////////////////////////////////////////
@@ -268,7 +258,13 @@ namespace boost { namespace python { namespace indexing {
default_algorithms<ContainerTraits, Ovr>::count (
container &c, key_param key)
{
return std::count (most_derived::begin(c), most_derived::end(c), key);
typedef typename container_traits::value_traits_ value_traits_;
typedef typename value_traits_::equal_to comparison;
return std::count_if (
most_derived::begin(c)
, most_derived::end(c)
, std::bind1st (comparison(), key));
}
/////////////////////////////////////////////////////////////////////////
@@ -391,7 +387,9 @@ namespace boost { namespace python { namespace indexing {
template<typename ContainerTraits, typename Ovr>
void default_algorithms<ContainerTraits, Ovr>::sort (container &c)
{
std::sort (most_derived::begin(c), most_derived::end(c));
typedef typename container_traits::value_traits_ value_traits_;
typedef typename value_traits_::less comparison;
std::sort (most_derived::begin(c), most_derived::end(c), comparison());
}
/////////////////////////////////////////////////////////////////////////

View File

@@ -42,13 +42,13 @@ namespace boost { namespace python { namespace indexing {
typedef ::boost::detail::iterator_traits<Iterator> std_traits;
public:
typedef Iterator iterator;
typedef BOOST_DEDUCED_TYPENAME std_traits::value_type value_type;
typedef BOOST_DEDUCED_TYPENAME std_traits::reference reference;
typedef BOOST_DEDUCED_TYPENAME std_traits::difference_type difference_type;
typedef Iterator iterator;
typedef typename std_traits::value_type value_type;
typedef typename std_traits::reference reference;
typedef typename std_traits::difference_type difference_type;
BOOST_STATIC_CONSTANT (bool, has_copyable_iter = false);
BOOST_STATIC_CONSTANT (bool, is_reorderable = false);
BOOST_STATIC_CONSTANT (bool, has_copyable_iter = false);
BOOST_STATIC_CONSTANT (bool, is_reorderable = false);
BOOST_STATIC_CONSTANT (
bool, has_mutable_ref = is_mutable_ref<reference>::value);
@@ -121,11 +121,11 @@ namespace boost { namespace python { namespace indexing {
template<typename Iterator>
class deduced_traits {
typedef BOOST_DEDUCED_TYPENAME ::boost::BOOST_ITERATOR_CATEGORY<
Iterator>::type category;
typedef typename ::boost::BOOST_ITERATOR_CATEGORY<Iterator>::type
category;
typedef BOOST_DEDUCED_TYPENAME ::boost::detail::std_category <
category>::type max_category;
typedef typename ::boost::detail::std_category<category>::type
max_category;
public:
typedef typename traits_by_category <max_category>

View File

@@ -24,6 +24,11 @@
#include <boost/python/suite/indexing/algo_selector.hpp>
#include <list>
#if BOOST_WORKAROUND (BOOST_MSVC, == 1200)
# include <boost/static_assert.hpp>
# include <boost/type_traits.hpp>
#endif
namespace boost { namespace python { namespace indexing {
/////////////////////////////////////////////////////////////////////////
// Algorithms implementation for std::list instances
@@ -93,7 +98,20 @@ namespace boost { namespace python { namespace indexing {
template<typename ContainerTraits, typename Ovr>
void list_algorithms<ContainerTraits, Ovr>::sort (container &c)
{
c.sort();
typedef typename container_traits::value_traits_ value_traits_;
typedef typename value_traits_::less comparison;
#if BOOST_WORKAROUND (BOOST_MSVC, == 1200)
// MSVC6 doesn't have a templated sort member in list, so we just
// use the parameterless version. This gives the correct behaviour
// provided that value_traits_::less is std::less<value_type>. It
// would be possible to support std::greater<T> (the only other
// overload of list::sort in MSVC6) with some additional work.
BOOST_STATIC_ASSERT (
(::boost::is_same<comparison, std::less<value_type> >::value));
c.sort ();
#else
c.sort (comparison());
#endif
}
} } }

View File

@@ -86,6 +86,24 @@ namespace boost { namespace python { namespace indexing {
typedef __int64 type;
};
#endif
namespace detail {
struct no_override { };
template<typename Base, typename Override>
struct maybe_override
{
// Probably need to disable this if there is no partial
// specialization support, because Override is almost certain to
// be an incomplete type. If that is right, the workaround
// version would just have to do "typedef Base type;"
typedef typename mpl::if_
<is_same <Override, no_override>, Base, Override>
::type type;
};
}
} } }
#endif // BOOST_PYTHON_INDEXING_SUITE_UTILS_HPP

View File

@@ -22,17 +22,16 @@
#define BOOST_PYTHON_INDEXING_VALUE_TRAITS_HPP
#include <boost/config.hpp>
#include <functional>
namespace boost { namespace python { namespace indexing {
template<typename T>
struct value_traits {
BOOST_STATIC_CONSTANT (bool, equality_comparable = true);
// Meaning from C++98 standard section 20.1.1
typedef std::equal_to<T> equal_to;
BOOST_STATIC_CONSTANT (bool, lessthan_comparable = true);
// static bool const has_less = true;
// etc...
typedef std::less<T> less;
// Default, do-nothing, version of visitor_helper
template<typename PythonClass, typename Policy>