| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | //  (C) Copyright David Abrahams 2002. Permission to copy, use, modify,
 | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  | //  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.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  See http://www.boost.org for most recent version including documentation.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | //  Revision History
 | 
					
						
							| 
									
										
										
										
											2001-03-04 16:08:20 +00:00
										 |  |  | //  04 Mar 2001 Patches for Intel C++ (Dave Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-02-19 22:36:22 +00:00
										 |  |  | //  19 Feb 2001 Take advantage of improved iterator_traits to do more tests
 | 
					
						
							|  |  |  | //              on MSVC. Reordered some #ifdefs for coherency.
 | 
					
						
							|  |  |  | //              (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | //  13 Feb 2001 Test new VC6 workarounds (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-02-11 19:50:14 +00:00
										 |  |  | //  11 Feb 2001 Final fixes for Borland (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-02-11 16:05:01 +00:00
										 |  |  | //  11 Feb 2001 Some fixes for Borland get it closer on that compiler
 | 
					
						
							|  |  |  | //              (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | //  07 Feb 2001 More comprehensive testing; factored out static tests for
 | 
					
						
							|  |  |  | //              better reuse (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-01-22 05:03:48 +00:00
										 |  |  | //  21 Jan 2001 Quick fix to my_iterator, which wasn't returning a
 | 
					
						
							|  |  |  | //              reference type from operator* (David Abrahams)
 | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  | //  19 Jan 2001 Initial version with iterator operators (David Abrahams)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <boost/detail/iterator.hpp>
 | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | #include <boost/type_traits/is_same.hpp>
 | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  | #include <boost/operators.hpp>
 | 
					
						
							|  |  |  | #include <boost/static_assert.hpp>
 | 
					
						
							|  |  |  | #include <iterator>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							|  |  |  | #include <list>
 | 
					
						
							|  |  |  | #include <cassert>
 | 
					
						
							|  |  |  | #include <iostream>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | // A UDT for which we can specialize std::iterator_traits<element*> on
 | 
					
						
							|  |  |  | // compilers which don't support partial specialization. There's no
 | 
					
						
							|  |  |  | // other reasonable way to test pointers on those compilers.
 | 
					
						
							|  |  |  | struct element {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | // An iterator for which we can get traits.
 | 
					
						
							|  |  |  | struct my_iterator1 | 
					
						
							|  |  |  |     : boost::forward_iterator_helper<my_iterator1, char, long, const char*, const char&> | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  |     my_iterator1(const char* p) : m_p(p) {} | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  |     bool operator==(const my_iterator1& rhs) const | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  |         { return this->m_p == rhs.m_p; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  |     my_iterator1& operator++() { ++this->m_p; return *this; } | 
					
						
							| 
									
										
										
										
											2001-01-22 05:03:48 +00:00
										 |  |  |     const char& operator*() { return *m_p; } | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  |  private: | 
					
						
							|  |  |  |     const char* m_p; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | // Used to prove that we don't require std::iterator<> in the hierarchy under
 | 
					
						
							|  |  |  | // MSVC6, and that we can compute all the traits for a standard-conforming UDT
 | 
					
						
							|  |  |  | // iterator.
 | 
					
						
							|  |  |  | struct my_iterator2 | 
					
						
							|  |  |  |     : boost::equality_comparable<my_iterator2 | 
					
						
							|  |  |  |     , boost::incrementable<my_iterator2 | 
					
						
							|  |  |  |     , boost::dereferenceable<my_iterator2,const char*> > > | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     typedef char value_type; | 
					
						
							|  |  |  |     typedef long difference_type; | 
					
						
							|  |  |  |     typedef const char* pointer; | 
					
						
							|  |  |  |     typedef const char& reference; | 
					
						
							|  |  |  |     typedef std::forward_iterator_tag iterator_category; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     my_iterator2(const char* p) : m_p(p) {} | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     bool operator==(const my_iterator2& rhs) const | 
					
						
							|  |  |  |         { return this->m_p == rhs.m_p; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     my_iterator2& operator++() { ++this->m_p; return *this; } | 
					
						
							|  |  |  |     const char& operator*() { return *m_p; } | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |     const char* m_p; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Used to prove that we're not overly confused by the existence of
 | 
					
						
							|  |  |  | // std::iterator<> in the hierarchy under MSVC6 - we should find that
 | 
					
						
							|  |  |  | // boost::detail::iterator_traits<my_iterator3>::difference_type is int.
 | 
					
						
							|  |  |  | struct my_iterator3 : my_iterator1 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     typedef int difference_type; | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  |     my_iterator3(const char* p) | 
					
						
							|  |  |  |         : my_iterator1(p) {} | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Assertion tools.  Used instead of BOOST_STATIC_ASSERT because that
 | 
					
						
							|  |  |  | // doesn't give us a nice stack backtrace
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | template <bool = false> struct assertion; | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  | template <> struct assertion<true> | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     typedef char type; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class T, class U> | 
					
						
							|  |  |  | struct assert_same | 
					
						
							|  |  |  |     : assertion<(::boost::is_same<T,U>::value)> | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Iterator tests
 | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | template <class Iterator, | 
					
						
							|  |  |  |     class value_type, class difference_type, class pointer, class reference, class category> | 
					
						
							|  |  |  | struct non_portable_tests | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-05-04 11:03:42 +00:00
										 |  |  |     typedef typename boost::detail::iterator_traits<Iterator>::pointer test_pt; | 
					
						
							|  |  |  |     typedef typename boost::detail::iterator_traits<Iterator>::reference test_rt; | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  |     typedef typename assert_same<test_pt, pointer>::type a1; | 
					
						
							|  |  |  |     typedef typename assert_same<test_rt, reference>::type a2; | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class Iterator, | 
					
						
							|  |  |  |     class value_type, class difference_type, class pointer, class reference, class category> | 
					
						
							|  |  |  | struct portable_tests | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  |     typedef typename boost::detail::iterator_traits<Iterator>::difference_type test_dt; | 
					
						
							|  |  |  |     typedef typename boost::detail::iterator_traits<Iterator>::iterator_category test_cat; | 
					
						
							|  |  |  |     typedef typename assert_same<test_dt, difference_type>::type a1; | 
					
						
							|  |  |  |     typedef typename assert_same<test_cat, category>::type a2; | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Test iterator_traits
 | 
					
						
							|  |  |  | template <class Iterator, | 
					
						
							|  |  |  |     class value_type, class difference_type, class pointer, class reference, class category> | 
					
						
							|  |  |  | struct input_iterator_test | 
					
						
							|  |  |  |     : portable_tests<Iterator,value_type,difference_type,pointer,reference,category> | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  |     typedef typename boost::detail::iterator_traits<Iterator>::value_type test_vt; | 
					
						
							|  |  |  |     typedef typename assert_same<test_vt, value_type>::type a1; | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class Iterator, | 
					
						
							|  |  |  |     class value_type, class difference_type, class pointer, class reference, class category> | 
					
						
							|  |  |  | struct non_pointer_test | 
					
						
							|  |  |  |     : input_iterator_test<Iterator,value_type,difference_type,pointer,reference,category> | 
					
						
							|  |  |  |       , non_portable_tests<Iterator,value_type,difference_type,pointer,reference,category> | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template <class Iterator, | 
					
						
							|  |  |  |     class value_type, class difference_type, class pointer, class reference, class category> | 
					
						
							|  |  |  | struct maybe_pointer_test | 
					
						
							|  |  |  |     : portable_tests<Iterator,value_type,difference_type,pointer,reference,category> | 
					
						
							|  |  |  | #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
 | 
					
						
							|  |  |  |       , non_portable_tests<Iterator,value_type,difference_type,pointer,reference,category> | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | input_iterator_test<std::istream_iterator<int>, int, std::ptrdiff_t, int*, int&, std::input_iterator_tag> | 
					
						
							|  |  |  |         istream_iterator_test; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-11 16:05:01 +00:00
										 |  |  | #if defined(__BORLANDC__) && !defined(__SGI_STL_PORT)
 | 
					
						
							|  |  |  | typedef ::std::char_traits<char>::off_type distance; | 
					
						
							|  |  |  | non_pointer_test<std::ostream_iterator<int>,int, | 
					
						
							|  |  |  |     distance,int*,int&,std::output_iterator_tag> ostream_iterator_test; | 
					
						
							| 
									
										
										
										
											2001-03-04 16:08:20 +00:00
										 |  |  | #elif defined(BOOST_MSVC_STD_ITERATOR)
 | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | non_pointer_test<std::ostream_iterator<int>, | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  |     int, void, int*, int&, std::output_iterator_tag> | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  |         ostream_iterator_test; | 
					
						
							| 
									
										
										
										
											2001-02-11 16:05:01 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | non_pointer_test<std::ostream_iterator<int>, | 
					
						
							| 
									
										
										
										
											2001-02-19 22:36:22 +00:00
										 |  |  |     void, void, void, void, std::output_iterator_tag> | 
					
						
							| 
									
										
										
										
											2001-02-11 16:05:01 +00:00
										 |  |  |         ostream_iterator_test; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-22 16:52:36 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef __KCC
 | 
					
						
							|  |  |  |   typedef long std_list_diff_type; | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  |   typedef std::ptrdiff_t std_list_diff_type; | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | non_pointer_test<std::list<int>::iterator, int, std_list_diff_type, int*, int&, std::bidirectional_iterator_tag> | 
					
						
							|  |  |  |         list_iterator_test; | 
					
						
							| 
									
										
										
										
											2001-01-22 16:52:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-07 16:38:41 +00:00
										 |  |  | maybe_pointer_test<std::vector<int>::iterator, int, std::ptrdiff_t, int*, int&, std::random_access_iterator_tag> | 
					
						
							|  |  |  |         vector_iterator_test; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | maybe_pointer_test<int*, int, std::ptrdiff_t, int*, int&, std::random_access_iterator_tag> | 
					
						
							|  |  |  |         int_pointer_test; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | non_pointer_test<my_iterator1, char, long, const char*, const char&, std::forward_iterator_tag> | 
					
						
							|  |  |  |        my_iterator1_test; | 
					
						
							|  |  |  |                      | 
					
						
							|  |  |  | non_pointer_test<my_iterator2, char, long, const char*, const char&, std::forward_iterator_tag> | 
					
						
							|  |  |  |        my_iterator2_test; | 
					
						
							| 
									
										
										
										
											2002-11-08 17:08:17 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  | non_pointer_test<my_iterator3, char, int, const char*, const char&, std::forward_iterator_tag> | 
					
						
							|  |  |  |        my_iterator3_test; | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  | int main() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     char chars[100]; | 
					
						
							|  |  |  |     int ints[100]; | 
					
						
							| 
									
										
										
										
											2002-11-08 06:57:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (int length = 3; length < 100; length += length / 3) | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         std::list<int> l(length); | 
					
						
							|  |  |  |         assert(boost::detail::distance(l.begin(), l.end()) == length); | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         std::vector<int> v(length); | 
					
						
							|  |  |  |         assert(boost::detail::distance(v.begin(), v.end()) == length); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         assert(boost::detail::distance(&ints[0], ints + length) == length); | 
					
						
							| 
									
										
										
										
											2001-02-13 23:32:19 +00:00
										 |  |  |         assert(boost::detail::distance(my_iterator1(chars), my_iterator1(chars + length)) == length); | 
					
						
							|  |  |  |         assert(boost::detail::distance(my_iterator2(chars), my_iterator2(chars + length)) == length); | 
					
						
							|  |  |  |         assert(boost::detail::distance(my_iterator3(chars), my_iterator3(chars + length)) == length); | 
					
						
							| 
									
										
										
										
											2001-01-21 06:02:08 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } |