// (C) Copyright Thomas Witt 2003. 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. // See http://www.boost.org for most recent version including documentation. #include #include #include #include #include #include #include #include #include #include #include #include #include "static_assert_same.hpp" struct my_iterator_tag : public std::random_access_iterator_tag { }; using boost::dummyT; struct mult_functor { typedef int result_type; typedef int argument_type; // Functors used with transform_iterator must be // DefaultConstructible, as the transform_iterator must be // DefaultConstructible to satisfy the requirements for // TrivialIterator. mult_functor() { } mult_functor(int aa) : a(aa) { } int operator()(int b) const { return a * b; } int a; }; template struct select1st_ : public std::unary_function { const typename Pair::first_type& operator()(const Pair& x) const { return x.first; } typename Pair::first_type& operator()(Pair& x) const { return x.first; } }; struct one_or_four { bool operator()(dummyT x) const { return x.foo() == 1 || x.foo() == 4; } }; typedef std::deque storage; typedef std::deque pointer_deque; typedef std::set iterator_set; template struct foo; void blah(int) { } struct my_gen { typedef int result_type; my_gen() : n(0) { } int operator()() { return ++n; } int n; }; template struct ptr_iterator : boost::iterator_adaptor< ptr_iterator , V* , V , std::random_access_iterator_tag #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) , V& #endif > { private: typedef boost::iterator_adaptor< ptr_iterator , V* , V , std::random_access_iterator_tag #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x551)) , V& #endif > super_t; public: ptr_iterator() { } ptr_iterator(V* d) : super_t(d) { } template ptr_iterator( const ptr_iterator& x , typename boost::enable_if_convertible::type* = 0 ) : super_t(x.base()) {} }; // Non-functional iterator for category modification checking template struct modify_category : boost::iterator_adaptor< modify_category , Iter , boost::use_default , Category > {}; template struct fwd_iterator : boost::iterator_adaptor< fwd_iterator , boost::forward_iterator_archetype > { private: typedef boost::iterator_adaptor< fwd_iterator , boost::forward_iterator_archetype > super_t; public: fwd_iterator() { } fwd_iterator(boost::forward_iterator_archetype d) : super_t(d) { } }; template struct in_iterator : boost::iterator_adaptor< in_iterator , boost::input_iterator_archetype > { private: typedef boost::iterator_adaptor< in_iterator , boost::input_iterator_archetype > super_t; public: in_iterator() { } in_iterator(boost::input_iterator_archetype d) : super_t(d) { } }; template struct constant_iterator : boost::iterator_adaptor< constant_iterator , Iter , typename std::iterator_traits::value_type const > { typedef boost::iterator_adaptor< constant_iterator , Iter , typename std::iterator_traits::value_type const > base_t; constant_iterator() {} constant_iterator(Iter it) : base_t(it) {} }; int main() { dummyT array[] = { dummyT(0), dummyT(1), dummyT(2), dummyT(3), dummyT(4), dummyT(5) }; const int N = sizeof(array)/sizeof(dummyT); // sanity check, if this doesn't pass the test is buggy boost::random_access_iterator_test(array, N, array); // Test the iterator_adaptor { ptr_iterator i(array); boost::random_access_iterator_test(i, N, array); ptr_iterator j(array); boost::random_access_iterator_test(j, N, array); boost::const_nonconst_iterator_test(i, ++j); } int test; // Test the iterator_traits { // Test computation of defaults typedef ptr_iterator Iter1; // don't use std::iterator_traits here to avoid VC++ problems test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; #if !BOOST_WORKAROUND(__MWERKS__, <= 0x2407) BOOST_STATIC_ASSERT((boost::is_convertible::value)); #endif } { // Test computation of default when the Value is const typedef ptr_iterator Iter1; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; } { // Test constant iterator idiom typedef ptr_iterator BaseIter; typedef constant_iterator Iter; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; test = static_assert_same::value; // Test category modification typedef modify_category ReadableIter; test = static_assert_same::value; typedef modify_category IncrementableIter; test = static_assert_same::value; test = static_assert_same::value; } // Test the iterator_adaptor { ptr_iterator i(array); boost::random_access_iterator_test(i, N, array); ptr_iterator j(array); boost::random_access_iterator_test(j, N, array); boost::const_nonconst_iterator_test(i, ++j); } // check operator-> with a forward iterator { boost::forward_iterator_archetype forward_iter; typedef fwd_iterator adaptor_type; adaptor_type i(forward_iter); int zero = 0; if (zero) // don't do this, just make sure it compiles assert((*i).m_x == i->foo()); } // check operator-> with an input iterator { boost::input_iterator_archetype input_iter; typedef in_iterator adaptor_type; adaptor_type i(input_iter); int zero = 0; if (zero) // don't do this, just make sure it compiles assert((*i).m_x == i->foo()); } std::cout << "test successful " << std::endl; (void)test; return 0; }