2003-07-07 14:14:36 +00:00
|
|
|
// (C) Copyright Jeremy Siek 2002. Permission to copy, use, modify,
|
|
|
|
// sell and distribute this software is granted provided this
|
|
|
|
// copyright notice appears in all copies. This software is provided
|
|
|
|
// "as is" without express or implied warranty, and with no claim as
|
|
|
|
// to its suitability for any purpose.
|
|
|
|
|
|
|
|
#ifndef BOOST_ITERATOR_CONCEPTS_HPP
|
|
|
|
#define BOOST_ITERATOR_CONCEPTS_HPP
|
|
|
|
|
|
|
|
// Revision History
|
|
|
|
// 26 Apr 2003 thw
|
|
|
|
// Adapted to new iterator concepts
|
|
|
|
// 22 Nov 2002 Thomas Witt
|
|
|
|
// Added interoperable concept.
|
|
|
|
|
|
|
|
#include <boost/concept_check.hpp>
|
|
|
|
#include <boost/iterator/iterator_categories.hpp>
|
2003-11-22 01:18:37 +00:00
|
|
|
|
|
|
|
// Use boost::detail::iterator_traits to work around some MSVC/Dinkumware problems.
|
|
|
|
#include <boost/detail/iterator.hpp>
|
|
|
|
|
2003-07-07 14:14:36 +00:00
|
|
|
#include <boost/type_traits/is_same.hpp>
|
|
|
|
#include <boost/type_traits/is_integral.hpp>
|
2003-11-23 17:07:04 +00:00
|
|
|
#include <boost/type_traits/is_convertible.hpp>
|
2003-11-22 01:18:37 +00:00
|
|
|
|
2003-07-07 14:14:36 +00:00
|
|
|
#include <boost/mpl/bool.hpp>
|
|
|
|
#include <boost/mpl/if.hpp>
|
|
|
|
#include <boost/mpl/and.hpp>
|
2003-11-22 01:18:37 +00:00
|
|
|
#include <boost/mpl/or.hpp>
|
2003-07-07 14:14:36 +00:00
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
#include <boost/static_assert.hpp>
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
// Use boost/limits to work around missing limits headers on some compilers
|
|
|
|
#include <boost/limits.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace boost_concepts {
|
|
|
|
// Used a different namespace here (instead of "boost") so that the
|
|
|
|
// concept descriptions do not take for granted the names in
|
|
|
|
// namespace boost.
|
|
|
|
|
|
|
|
// We use this in place of STATIC_ASSERT((is_convertible<...>))
|
|
|
|
// because some compilers (CWPro7.x) can't detect convertibility.
|
|
|
|
//
|
|
|
|
// Of course, that just gets us a different error at the moment with
|
|
|
|
// some tests, since new iterator category deduction still depends
|
|
|
|
// on convertibility detection. We might need some specializations
|
|
|
|
// to support this compiler.
|
|
|
|
template <class Target, class Source>
|
|
|
|
struct static_assert_base_and_derived
|
|
|
|
{
|
|
|
|
static_assert_base_and_derived(Target* = (Source*)0) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
// Iterator Access Concepts
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class ReadableIteratorConcept {
|
|
|
|
public:
|
2003-11-28 16:21:15 +00:00
|
|
|
typedef BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::value_type value_type;
|
|
|
|
typedef BOOST_DEDUCED_TYPENAME boost::detail::iterator_traits<Iterator>::reference reference;
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
|
|
|
|
boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
|
|
|
|
boost::function_requires<
|
|
|
|
boost::DefaultConstructibleConcept<Iterator> >();
|
|
|
|
|
|
|
|
reference r = *i; // or perhaps read(x)
|
2003-11-23 17:07:04 +00:00
|
|
|
value_type v = r;
|
|
|
|
value_type v2 = *i;
|
2003-07-07 14:14:36 +00:00
|
|
|
boost::ignore_unused_variable_warning(v);
|
|
|
|
}
|
|
|
|
Iterator i;
|
|
|
|
};
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
template <
|
|
|
|
typename Iterator
|
|
|
|
, typename ValueType = typename boost::detail::iterator_traits<Iterator>::value_type
|
|
|
|
>
|
2003-07-07 14:14:36 +00:00
|
|
|
class WritableIteratorConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
|
2003-07-07 14:14:36 +00:00
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
|
|
|
|
boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
|
|
|
|
boost::function_requires<
|
|
|
|
boost::DefaultConstructibleConcept<Iterator> >();
|
|
|
|
|
|
|
|
*i = v; // a good alternative could be something like write(x, v)
|
|
|
|
}
|
|
|
|
ValueType v;
|
|
|
|
Iterator i;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class SwappableIteratorConcept {
|
|
|
|
public:
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
std::iter_swap(i1, i2);
|
|
|
|
}
|
|
|
|
Iterator i1;
|
|
|
|
Iterator i2;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
2003-11-22 01:18:37 +00:00
|
|
|
class ReadableLvalueIteratorConcept
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::value_type value_type;
|
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::reference reference;
|
2003-07-07 14:14:36 +00:00
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
void constraints()
|
|
|
|
{
|
|
|
|
boost::function_requires< ReadableIteratorConcept<Iterator> >();
|
2003-07-07 14:14:36 +00:00
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef boost::mpl::or_<
|
|
|
|
boost::is_same<reference, value_type&>
|
|
|
|
, boost::is_same<reference, value_type const&>
|
|
|
|
> correct_reference;
|
2003-07-07 14:14:36 +00:00
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(correct_reference::value);
|
2003-07-07 14:14:36 +00:00
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
reference v = *i;
|
|
|
|
boost::ignore_unused_variable_warning(v);
|
2003-07-07 14:14:36 +00:00
|
|
|
}
|
|
|
|
Iterator i;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class WritableLvalueIteratorConcept {
|
|
|
|
public:
|
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::value_type value_type;
|
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::reference reference;
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires<
|
|
|
|
ReadableLvalueIteratorConcept<Iterator> >();
|
|
|
|
boost::function_requires<
|
|
|
|
WritableIteratorConcept<Iterator, value_type> >();
|
|
|
|
boost::function_requires<
|
|
|
|
SwappableIteratorConcept<Iterator> >();
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_STATIC_ASSERT((boost::is_same<reference, value_type&>::value));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
// Iterator Traversal Concepts
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class IncrementableIteratorConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< boost::SGIAssignableConcept<Iterator> >();
|
|
|
|
boost::function_requires<
|
|
|
|
boost::DefaultConstructibleConcept<Iterator> >();
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
(boost::is_convertible<
|
|
|
|
traversal_category
|
|
|
|
, boost::incrementable_traversal_tag
|
|
|
|
>::value
|
|
|
|
));
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
++i;
|
|
|
|
(void)i++;
|
|
|
|
}
|
|
|
|
Iterator i;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class SinglePassIteratorConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::difference_type difference_type;
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< IncrementableIteratorConcept<Iterator> >();
|
|
|
|
boost::function_requires< boost::EqualityComparableConcept<Iterator> >();
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
(boost::is_convertible<
|
|
|
|
traversal_category
|
|
|
|
, boost::single_pass_traversal_tag
|
|
|
|
>::value
|
|
|
|
));
|
2003-07-07 14:14:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class ForwardTraversalConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::difference_type difference_type;
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< SinglePassIteratorConcept<Iterator> >();
|
|
|
|
|
|
|
|
typedef boost::mpl::and_<
|
|
|
|
boost::is_integral<difference_type>,
|
|
|
|
boost::mpl::bool_< std::numeric_limits<difference_type>::is_signed >
|
|
|
|
> difference_type_is_signed_integral;
|
|
|
|
|
|
|
|
BOOST_STATIC_ASSERT(difference_type_is_signed_integral::value);
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
(boost::is_convertible<
|
|
|
|
traversal_category
|
|
|
|
, boost::forward_traversal_tag
|
|
|
|
>::value
|
|
|
|
));
|
2003-07-07 14:14:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class BidirectionalTraversalConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< ForwardTraversalConcept<Iterator> >();
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
(boost::is_convertible<
|
|
|
|
traversal_category
|
|
|
|
, boost::bidirectional_traversal_tag
|
|
|
|
>::value
|
|
|
|
));
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
--i;
|
|
|
|
(void)i--;
|
|
|
|
}
|
|
|
|
Iterator i;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename Iterator>
|
|
|
|
class RandomAccessTraversalConcept {
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::difference_type
|
|
|
|
difference_type;
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
boost::function_requires< BidirectionalTraversalConcept<Iterator> >();
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
(boost::is_convertible<
|
|
|
|
traversal_category
|
|
|
|
, boost::random_access_traversal_tag
|
|
|
|
>::value
|
|
|
|
));
|
2003-07-07 14:14:36 +00:00
|
|
|
|
|
|
|
i += n;
|
|
|
|
i = i + n;
|
|
|
|
i = n + i;
|
|
|
|
i -= n;
|
|
|
|
i = i - n;
|
|
|
|
n = i - j;
|
|
|
|
}
|
|
|
|
difference_type n;
|
|
|
|
Iterator i, j;
|
|
|
|
};
|
|
|
|
|
|
|
|
//===========================================================================
|
|
|
|
// Iterator Interoperability Concept
|
|
|
|
|
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename TraversalTag>
|
|
|
|
struct Operations;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Operations<boost::incrementable_traversal_tag>
|
|
|
|
{
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
|
|
static void constraints(Iterator1 const& i1, Iterator2 const& i2)
|
|
|
|
{
|
|
|
|
// no interoperability constraints
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Operations<boost::single_pass_traversal_tag>
|
|
|
|
{
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
|
|
static void constraints(Iterator1 const& i1, Iterator2 const& i2)
|
|
|
|
{
|
2003-07-14 21:31:44 +00:00
|
|
|
Operations<boost::incrementable_traversal_tag>::constraints(i1, i2);
|
2003-07-07 14:14:36 +00:00
|
|
|
i1 == i2;
|
|
|
|
i1 != i2;
|
|
|
|
|
|
|
|
i2 == i1;
|
|
|
|
i2 != i1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Operations<boost::forward_traversal_tag>
|
|
|
|
{
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
|
|
static void constraints(Iterator1 const& i1, Iterator2 const& i2)
|
|
|
|
{
|
|
|
|
Operations<boost::single_pass_traversal_tag>::constraints(i1, i2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Operations<boost::bidirectional_traversal_tag>
|
|
|
|
{
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
|
|
static void constraints(Iterator1 const& i1, Iterator2 const& i2)
|
|
|
|
{
|
|
|
|
Operations<boost::forward_traversal_tag>::constraints(i1, i2);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct Operations<boost::random_access_traversal_tag>
|
|
|
|
{
|
|
|
|
template <typename Iterator1, typename Iterator2>
|
|
|
|
static void constraints(Iterator1 const& i1, Iterator2 const& i2)
|
|
|
|
{
|
|
|
|
Operations<boost::bidirectional_traversal_tag>::constraints(i1, i2);
|
|
|
|
|
|
|
|
i1 < i2;
|
|
|
|
i1 <= i2;
|
|
|
|
i1 > i2;
|
|
|
|
i1 >= i2;
|
|
|
|
i1 - i2;
|
|
|
|
|
|
|
|
i2 < i1;
|
|
|
|
i2 <= i1;
|
|
|
|
i2 > i1;
|
|
|
|
i2 >= i1;
|
|
|
|
i2 - i1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <typename Iterator, typename ConstIterator>
|
|
|
|
class InteroperableConcept
|
|
|
|
{
|
|
|
|
public:
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<Iterator>::type traversal_category;
|
2003-07-07 14:14:36 +00:00
|
|
|
typedef typename boost::detail::iterator_traits<Iterator>::difference_type
|
|
|
|
difference_type;
|
|
|
|
|
2003-11-22 01:18:37 +00:00
|
|
|
typedef typename boost::iterator_traversal<ConstIterator>::type
|
2003-07-07 14:14:36 +00:00
|
|
|
const_traversal_category;
|
|
|
|
typedef typename boost::detail::iterator_traits<ConstIterator>::difference_type
|
|
|
|
const_difference_type;
|
|
|
|
|
|
|
|
void constraints() {
|
|
|
|
BOOST_STATIC_ASSERT((boost::is_same< difference_type,
|
|
|
|
const_difference_type>::value));
|
|
|
|
BOOST_STATIC_ASSERT((boost::is_same< traversal_category,
|
|
|
|
const_traversal_category>::value));
|
|
|
|
|
|
|
|
// ToDo check what the std really requires
|
|
|
|
|
|
|
|
// detail::Operations<traversal_category>::constraints(i, ci);
|
|
|
|
|
|
|
|
ci = i;
|
|
|
|
|
|
|
|
}
|
|
|
|
Iterator i;
|
|
|
|
ConstIterator ci;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace boost_concepts
|
|
|
|
|
|
|
|
|
|
|
|
#endif // BOOST_ITERATOR_CONCEPTS_HPP
|