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