OKlibrary  0.2.1.6
std_Container.hpp
Go to the documentation of this file.
00001 // Oliver Kullmann, 3.7.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 
00019 #ifndef STDCONTAINER_776YhBq
00020 #define STDCONTAINER_776YhBq
00021 
00022 #include <iterator>
00023 
00024 #include <boost/concept_check.hpp>
00025 #include <boost/type_traits.hpp>
00026 #include <boost/static_assert.hpp>
00027 
00028 #include <OKlib/Concepts/ConceptsBase.hpp>
00029 #include <OKlib/Concepts/std_Basics.hpp>
00030 #include <OKlib/Concepts/std_Iterators.hpp>
00031 #include <OKlib/Concepts/Basics.hpp>
00032 #include <OKlib/traits/TypeTraits.hpp>
00033 
00034 namespace OKlib {
00035   namespace Concepts {
00036 
00052     template <typename C>
00053     struct Container {
00054       typedef typename C::value_type value_type;
00055       typedef typename C::reference reference;
00056       typedef typename C::const_reference const_reference;
00057       typedef typename C::iterator iterator;
00058       typedef typename C::const_iterator const_iterator;
00059       typedef typename C::difference_type difference_type;
00060       typedef typename C::size_type size_type;
00061 
00062       typedef typename std::iterator_traits<iterator>::value_type iterator_value_type;
00063       typedef typename std::iterator_traits<const_iterator>::value_type const_iterator_value_type;
00064       typedef typename std::iterator_traits<iterator>::difference_type iterator_difference_type;
00065       typedef typename std::iterator_traits<const_iterator>::difference_type const_iterator_difference_type;
00066 
00067       void constraints() {
00068         boost::function_requires<OKlib::Concepts::FullyConstructible<C> >();
00069         boost::function_requires<OKlib::Concepts::LinearOrder<C> >();
00070         boost::function_requires<OKlib::Concepts::Assignable<value_type> >();
00071         boost::function_requires<OKlib::Concepts::CopyConstructible<value_type> >();
00072         boost::function_requires<OKlib::Concepts::Destructible<value_type> >();
00073         boost::function_requires<OKlib::Concepts::InputIterator<iterator> >();
00074         boost::function_requires<OKlib::Concepts::InputIterator<const_iterator> >();
00075 
00076         // unclear the meaning of reference ("lvalue of value_type") and const_reference ("const lvalue of value_type")
00077         BOOST_STATIC_ASSERT((boost::is_convertible<reference, value_type&>::value));
00078         BOOST_STATIC_ASSERT((boost::is_convertible<value_type&, reference>::value));
00079         BOOST_STATIC_ASSERT((boost::is_convertible<const_reference, const value_type&>::value));
00080         BOOST_STATIC_ASSERT((boost::is_convertible<const value_type&, const_reference>::value));
00081 
00082         BOOST_STATIC_ASSERT((boost::is_convertible<iterator, const_iterator>::value));
00083 
00084         // unclear the meaning of "iterator type pointing to value_type" resp. "iterator type pointing to const value_type
00085         BOOST_STATIC_ASSERT((boost::is_convertible<iterator_value_type, value_type>::value));
00086         BOOST_STATIC_ASSERT((boost::is_convertible<value_type, iterator_value_type>::value));
00087         BOOST_STATIC_ASSERT((boost::is_convertible<const_iterator_value_type, const value_type>::value));
00088         BOOST_STATIC_ASSERT((boost::is_convertible<value_type, const_iterator_value_type>::value));
00089 
00090         BOOST_STATIC_ASSERT((boost::is_same<difference_type, iterator_difference_type>::value));
00091         BOOST_STATIC_ASSERT((boost::is_same<difference_type, const_iterator_difference_type>::value));
00092         BOOST_STATIC_ASSERT(OKlib::traits::is_signed_integral<difference_type>::value);
00093 
00094         BOOST_STATIC_ASSERT(OKlib::traits::is_unsigned_integral<size_type>::value);
00095 
00096         iterator i(static_cast<iterator>(x.begin()));
00097         i = static_cast<iterator>(x.end());
00098         const_iterator ic(static_cast<const_iterator>(a.begin()));
00099         ic = static_cast<const_iterator>(a.end());
00100 
00101         static_cast<void>(x.swap(y));
00102 
00103         size_type s;
00104         s = static_cast<size_type>(x.size());
00105         s = static_cast<size_type>(a.size());
00106         s = static_cast<size_type>(x.max_size());
00107         s = static_cast<size_type>(a.max_size());
00108 
00109         bool b;
00110         b = static_cast<bool>(x.empty());
00111         b = static_cast<bool>(a.empty());
00112       }
00113       C x, y;
00114       const C a;
00115     };
00116 
00117     struct Container_tag : virtual FullyConstructible_tag, virtual LinearOrder_tag, virtual Assignable_tag {};
00118 
00119     class Container_Archetype {
00120     public :
00121 
00122       class value_type {
00123         value_type();
00124       };
00125     private :
00126       static value_type v;
00127 
00128     public :
00129 
00130       // ---------------------------------
00131 
00132       class reference {
00133         reference();
00134         reference& operator=(const reference&);
00135       public :
00136         reference(value_type&) {}
00137         operator value_type&() { return v; }
00138       };
00139 
00140       // ---------------------------------
00141 
00142       class const_reference {
00143         const_reference();
00144         const_reference& operator=(const const_reference&);
00145       public :
00146         const_reference(const value_type&) {}
00147         operator const value_type&() { return v; }
00148       };
00149 
00150       // ---------------------------------
00151 
00152       class iterator {
00153         iterator();
00154         typedef iterator self;
00155         struct convertible_to_bool {
00156           operator bool() { return bool(); }
00157         };
00158       public:
00159         typedef std::input_iterator_tag iterator_category;
00160         struct value_type {
00161           value_type(const Container_Archetype::value_type&) {}
00162           operator Container_Archetype::value_type() { return v; }
00163         };
00164       private :
00165         static value_type vi;
00166       public :
00167         typedef std::ptrdiff_t difference_type;
00168         typedef void pointer; // ToDo: left unspecified in the standard ?
00169         struct reference {
00170           operator value_type() { return vi; }
00171         };
00172         self& operator=(const self&) { return *this; }
00173         convertible_to_bool operator==(const self&) const { return convertible_to_bool(); }
00174         convertible_to_bool operator!=(const self&) const { return convertible_to_bool(); }
00175         reference operator*() const { return reference(); }
00176         self& operator++() { return *this; }
00177         struct internal {
00178           value_type operator* () { return vi; }
00179         };
00180         internal operator++(int) { return internal(); }
00181       };
00182 
00183       // ---------------------------------
00184 
00185       class const_iterator {
00186         const_iterator();
00187         typedef const_iterator self;
00188         struct convertible_to_bool {
00189           operator bool() { return bool(); }
00190         };
00191       public:
00192         const_iterator(const iterator&) {}
00193         typedef std::input_iterator_tag iterator_category;
00194         struct value_type {
00195           value_type(const Container_Archetype::value_type&) {}
00196           operator const Container_Archetype::value_type() { return v; }
00197         };
00198       private :
00199         static value_type vi;
00200       public :
00201         typedef std::ptrdiff_t difference_type;
00202         typedef void pointer; // ToDo: left unspecified in the standard ?
00203         struct reference {
00204           operator value_type() { return vi; }
00205         };
00206 
00207         self& operator=(const self&) { return *this; }
00208         convertible_to_bool operator==(const self&) const { return convertible_to_bool(); }
00209         convertible_to_bool operator!=(const self&) const { return convertible_to_bool(); }
00210         reference operator*() const { return reference(); }
00211         self& operator++() { return *this; }
00212         struct internal {
00213           value_type operator* () { return vi; }
00214         };
00215         internal operator++(int) { return internal(); }
00216       };
00217 
00218       // ---------------------------------
00219 
00220       typedef std::ptrdiff_t difference_type;
00221       typedef unsigned int size_type;
00222 
00223     private :
00224       static iterator i;
00225       static const_iterator ci;
00226     public :
00227       iterator begin() { return i; }
00228       iterator end() { return i; }
00229       const_iterator begin() const { return ci; }
00230       const_iterator end() const { return ci; }
00231       void swap(Container_Archetype&) {}
00232       size_type size() const { return size_type(); }
00233       size_type max_size() const { return size_type(); }
00234 
00235     private :
00236       struct convertible_to_bool {
00237         operator bool() { return bool(); }
00238       };
00239 
00240     public :
00241 
00242       convertible_to_bool empty() const { return convertible_to_bool(); }
00243 
00244       convertible_to_bool operator ==(const Container_Archetype&) const { return convertible_to_bool(); }
00245       convertible_to_bool operator !=(const Container_Archetype&) const { return convertible_to_bool(); }
00246       convertible_to_bool operator <(const Container_Archetype&) const { return convertible_to_bool(); }
00247       convertible_to_bool operator >(const Container_Archetype&) const { return convertible_to_bool(); }
00248       convertible_to_bool operator <=(const Container_Archetype&) const { return convertible_to_bool(); }
00249       convertible_to_bool operator >=(const Container_Archetype&) const { return convertible_to_bool(); }
00250 
00251     };
00252 
00253     // ----------------------------------------------------------------------------------------------------------------------------------
00254 
00255   }
00256 
00257 }
00258 
00259 #endif