libstdc++
stl_iterator_base_types.h
Go to the documentation of this file.
00001 // Types used in iterator implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
00004 // 2010, 2011, 2012
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /*
00028  *
00029  * Copyright (c) 1994
00030  * Hewlett-Packard Company
00031  *
00032  * Permission to use, copy, modify, distribute and sell this software
00033  * and its documentation for any purpose is hereby granted without fee,
00034  * provided that the above copyright notice appear in all copies and
00035  * that both that copyright notice and this permission notice appear
00036  * in supporting documentation.  Hewlett-Packard Company makes no
00037  * representations about the suitability of this software for any
00038  * purpose.  It is provided "as is" without express or implied warranty.
00039  *
00040  *
00041  * Copyright (c) 1996-1998
00042  * Silicon Graphics Computer Systems, Inc.
00043  *
00044  * Permission to use, copy, modify, distribute and sell this software
00045  * and its documentation for any purpose is hereby granted without fee,
00046  * provided that the above copyright notice appear in all copies and
00047  * that both that copyright notice and this permission notice appear
00048  * in supporting documentation.  Silicon Graphics makes no
00049  * representations about the suitability of this software for any
00050  * purpose.  It is provided "as is" without express or implied warranty.
00051  */
00052 
00053 /** @file bits/stl_iterator_base_types.h
00054  *  This is an internal header file, included by other library headers.
00055  *  Do not attempt to use it directly. @headername{iterator}
00056  *
00057  *  This file contains all of the general iterator-related utility types,
00058  *  such as iterator_traits and struct iterator.
00059  */
00060 
00061 #ifndef _STL_ITERATOR_BASE_TYPES_H
00062 #define _STL_ITERATOR_BASE_TYPES_H 1
00063 
00064 #pragma GCC system_header
00065 
00066 #include <bits/c++config.h>
00067 
00068 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00069 # include <type_traits>  // For _GLIBCXX_HAS_NESTED_TYPE, is_convertible
00070 #endif
00071 
00072 namespace std _GLIBCXX_VISIBILITY(default)
00073 {
00074 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00075 
00076   /**
00077    *  @defgroup iterators Iterators
00078    *  Abstractions for uniform iterating through various underlying types.
00079   */
00080   //@{ 
00081 
00082   /**
00083    *  @defgroup iterator_tags Iterator Tags
00084    *  These are empty types, used to distinguish different iterators.  The
00085    *  distinction is not made by what they contain, but simply by what they
00086    *  are.  Different underlying algorithms can then be used based on the
00087    *  different operations supported by different iterator types.
00088   */
00089   //@{ 
00090   ///  Marking input iterators.
00091   struct input_iterator_tag { };
00092 
00093   ///  Marking output iterators.
00094   struct output_iterator_tag { };
00095 
00096   /// Forward iterators support a superset of input iterator operations.
00097   struct forward_iterator_tag : public input_iterator_tag { };
00098 
00099   /// Bidirectional iterators support a superset of forward iterator
00100   /// operations.
00101   struct bidirectional_iterator_tag : public forward_iterator_tag { };
00102 
00103   /// Random-access iterators support a superset of bidirectional
00104   /// iterator operations.
00105   struct random_access_iterator_tag : public bidirectional_iterator_tag { };
00106   //@}
00107 
00108   /**
00109    *  @brief  Common %iterator class.
00110    *
00111    *  This class does nothing but define nested typedefs.  %Iterator classes
00112    *  can inherit from this class to save some work.  The typedefs are then
00113    *  used in specializations and overloading.
00114    *
00115    *  In particular, there are no default implementations of requirements
00116    *  such as @c operator++ and the like.  (How could there be?)
00117   */
00118   template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
00119            typename _Pointer = _Tp*, typename _Reference = _Tp&>
00120     struct iterator
00121     {
00122       /// One of the @link iterator_tags tag types@endlink.
00123       typedef _Category  iterator_category;
00124       /// The type "pointed to" by the iterator.
00125       typedef _Tp        value_type;
00126       /// Distance between iterators is represented as this type.
00127       typedef _Distance  difference_type;
00128       /// This type represents a pointer-to-value_type.
00129       typedef _Pointer   pointer;
00130       /// This type represents a reference-to-value_type.
00131       typedef _Reference reference;
00132     };
00133 
00134   /**
00135    *  @brief  Traits class for iterators.
00136    *
00137    *  This class does nothing but define nested typedefs.  The general
00138    *  version simply @a forwards the nested typedefs from the Iterator
00139    *  argument.  Specialized versions for pointers and pointers-to-const
00140    *  provide tighter, more correct semantics.
00141   */
00142 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00143 
00144 _GLIBCXX_HAS_NESTED_TYPE(iterator_category)
00145 
00146   template<typename _Iterator,
00147        bool = __has_iterator_category<_Iterator>::value>
00148     struct __iterator_traits { };
00149 
00150   template<typename _Iterator>
00151     struct __iterator_traits<_Iterator, true>
00152     {
00153       typedef typename _Iterator::iterator_category iterator_category;
00154       typedef typename _Iterator::value_type        value_type;
00155       typedef typename _Iterator::difference_type   difference_type;
00156       typedef typename _Iterator::pointer           pointer;
00157       typedef typename _Iterator::reference         reference;
00158     };
00159 
00160   template<typename _Iterator>
00161     struct iterator_traits
00162     : public __iterator_traits<_Iterator> { };
00163 #else
00164   template<typename _Iterator>
00165     struct iterator_traits
00166     {
00167       typedef typename _Iterator::iterator_category iterator_category;
00168       typedef typename _Iterator::value_type        value_type;
00169       typedef typename _Iterator::difference_type   difference_type;
00170       typedef typename _Iterator::pointer           pointer;
00171       typedef typename _Iterator::reference         reference;
00172     };
00173 #endif
00174 
00175   /// Partial specialization for pointer types.
00176   template<typename _Tp>
00177     struct iterator_traits<_Tp*>
00178     {
00179       typedef random_access_iterator_tag iterator_category;
00180       typedef _Tp                         value_type;
00181       typedef ptrdiff_t                   difference_type;
00182       typedef _Tp*                        pointer;
00183       typedef _Tp&                        reference;
00184     };
00185 
00186   /// Partial specialization for const pointer types.
00187   template<typename _Tp>
00188     struct iterator_traits<const _Tp*>
00189     {
00190       typedef random_access_iterator_tag iterator_category;
00191       typedef _Tp                         value_type;
00192       typedef ptrdiff_t                   difference_type;
00193       typedef const _Tp*                  pointer;
00194       typedef const _Tp&                  reference;
00195     };
00196 
00197   /**
00198    *  This function is not a part of the C++ standard but is syntactic
00199    *  sugar for internal library use only.
00200   */
00201   template<typename _Iter>
00202     inline typename iterator_traits<_Iter>::iterator_category
00203     __iterator_category(const _Iter&)
00204     { return typename iterator_traits<_Iter>::iterator_category(); }
00205 
00206   //@}
00207 
00208   // If _Iterator has a base returns it otherwise _Iterator is returned
00209   // untouched
00210   template<typename _Iterator, bool _HasBase>
00211     struct _Iter_base
00212     {
00213       typedef _Iterator iterator_type;
00214       static iterator_type _S_base(_Iterator __it)
00215       { return __it; }
00216     };
00217 
00218   template<typename _Iterator>
00219     struct _Iter_base<_Iterator, true>
00220     {
00221       typedef typename _Iterator::iterator_type iterator_type;
00222       static iterator_type _S_base(_Iterator __it)
00223       { return __it.base(); }
00224     };
00225 
00226 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00227   template<typename _InIter>
00228     using _RequireInputIter = typename
00229       enable_if<is_convertible<typename
00230         iterator_traits<_InIter>::iterator_category,
00231                    input_iterator_tag>::value>::type;
00232 #endif
00233 
00234 _GLIBCXX_END_NAMESPACE_VERSION
00235 } // namespace
00236 
00237 #endif /* _STL_ITERATOR_BASE_TYPES_H */
00238