OKlibrary  0.2.1.6
std_Iterators.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 15.5.2005 (Swansea)
00002 /* Copyright 2005 - 2007 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00025 #ifndef STDITERATORS_pOpt5185
00026 #define STDITERATORS_pOpt5185
00027 
00028 #include <iterator>
00029 
00030 #include <boost/concept_check.hpp>
00031 #include <boost/mpl/or.hpp>
00032 #include <boost/type_traits.hpp>
00033 
00034 #include <OKlib/Concepts/ConceptsBase.hpp>
00035 #include <OKlib/Concepts/std_Basics.hpp>
00036 #include <OKlib/Concepts/Basics.hpp>
00037 
00038 namespace OKlib {
00039   namespace Concepts {
00040 
00058     template <typename Iterator>
00059     struct InputIterator {
00060       typedef typename std::iterator_traits<Iterator>::value_type value_type;
00061       typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
00062       BOOST_STATIC_ASSERT((::boost::mpl::or_< ::boost::is_same<std::input_iterator_tag, iterator_category>, ::boost::is_base_and_derived<std::input_iterator_tag, iterator_category> >::value));
00063       typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
00064 
00065       typedef typename std::iterator_traits<Iterator>::reference reference;
00066       typedef typename std::iterator_traits<Iterator>::pointer pointer;
00067       void constraints() {
00068         boost::function_requires<ConstructibleCAEq<Iterator> >();
00069         dummy_use_v(static_cast<value_type>(*a)); // pre-condition: a is dereferencable
00070         static_cast<Iterator&>(++r); // pre-condition: r is dereferencable
00071         (void) r++; // pre-condition: r is dereferencable
00072         dummy_use_v(static_cast<value_type>(*r++)); // pre-condition: r is dereferencable
00073       }
00074       const Iterator a;
00075       Iterator& r;
00076       void dummy_use_v(const value_type& v) const {}
00077     };
00078     struct InputIterator_tag : virtual ConstructibleCAEq_tag {};
00079 
00080     class InputIterator_Archetype {
00081       InputIterator_Archetype();
00082       typedef InputIterator_Archetype self;
00083     protected :
00084       struct convertible_to_bool {
00085         operator bool() { return bool(); }
00086       };
00087     public:
00088       typedef std::input_iterator_tag iterator_category;
00089       struct value_type {};
00090       typedef std::ptrdiff_t difference_type;
00091       typedef void pointer; // ToDo: left unspecified in the standard ?
00092       struct reference {
00093         operator value_type() { return value_type(); }
00094       };
00095       self& operator=(const self&) { return *this; }
00096       convertible_to_bool operator==(const self&) const { return convertible_to_bool(); }
00097       convertible_to_bool operator!=(const self&) const { return convertible_to_bool(); }
00098       reference operator*() const { return reference(); }
00099       self& operator++() { return *this; }
00100       struct internal {
00101         value_type operator* () { return value_type(); }
00102       };
00103       internal operator++(int) { return internal(); }
00104     };
00105 
00106     // --------------------------------------------------------------------------------------------------------------------
00107 
00116     template <typename Iterator>
00117     struct MultiPassInputIterator {
00118       void constraints() {
00119         boost::function_requires<InputIterator<Iterator> >();
00120         boost::function_requires<EqualitySubstitutable<Iterator> >();
00121       }
00122     };
00123     struct MultiPassInputIterator_tag : virtual InputIterator_tag, virtual EqualitySubstitutable_tag {};
00124 
00125     struct MultiPassInputIterator_Archetype : InputIterator_Archetype {};
00126 
00127     // --------------------------------------------------------------------------------------------------------------------
00128 
00136     template <typename Iterator>
00137     struct ForwardIterator {
00138       typedef typename std::iterator_traits<Iterator>::value_type value_type;
00139       typedef typename std::iterator_traits<Iterator>::iterator_category iterator_category;
00140       BOOST_STATIC_ASSERT((::boost::mpl::or_< ::boost::is_same<std::input_iterator_tag, iterator_category>, ::boost::is_base_and_derived<std::forward_iterator_tag, iterator_category> >::value));
00141       typedef typename std::iterator_traits<Iterator>::difference_type difference_type;
00142 
00143       typedef typename std::iterator_traits<Iterator>::reference reference;
00144       typedef typename std::iterator_traits<Iterator>::pointer pointer;
00145       void constraints() {
00146         boost::function_requires<MultiPassInputIterator<Iterator> >();
00147         boost::function_requires<DefaultConstructible<Iterator> >();
00148         dummy_use_v(static_cast<value_type&>(*a));
00149         static_cast<Iterator&>(++r);
00150         Iterator i(static_cast<const Iterator&>(r++)); dummy_use_i(i);
00151         static_cast<value_type&>(*r++);
00152       }
00153       const Iterator a;
00154       Iterator& r;
00155       void dummy_use_v(const value_type& v) const {}
00156       void dummy_use_i(const Iterator& i) const {}
00157     };
00158   struct ForwardIterator_tag : virtual MultiPassInputIterator_tag, virtual DefaultConstructible_tag {};
00159 
00160     class ForwardIterator_Archetype {
00161       typedef ForwardIterator_Archetype self;
00162     protected :
00163       struct convertible_to_bool {
00164         operator bool() { return false; }
00165       };
00166     public:
00167       typedef std::input_iterator_tag iterator_category;
00168       struct value_type {};
00169       typedef std::ptrdiff_t difference_type;
00170       typedef value_type* pointer;
00171     public :
00172       struct reference {
00173         operator value_type&() { return v; }
00174       private :
00175         value_type v;
00176       };
00177       self& operator=(const self&) { return *this; }
00178       convertible_to_bool operator==(const self&) const { return convertible_to_bool(); }
00179       convertible_to_bool operator!=(const self&) const { return convertible_to_bool(); }
00180       reference operator*() const { return reference(); }
00181       self& operator++() { return *this; }
00182       struct internal {
00183         value_type& operator*() { return v; }
00184         operator const self&() { return *this; }
00185       private :
00186         value_type v;
00187       };
00188       internal operator++(int) { return internal(); }
00189     };
00190 
00191 
00192   }
00193 
00194 }
00195 
00196 #endif
00197