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-2015 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 __void_t, 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   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00142   // 2408. SFINAE-friendly common_type/iterator_traits is missing in C++14
00143   template<typename _Iterator, typename = __void_t<>>
00144     struct __iterator_traits { };
00145 
00146   template<typename _Iterator>
00147     struct __iterator_traits<_Iterator,
00148                              __void_t<typename _Iterator::iterator_category,
00149                                       typename _Iterator::value_type,
00150                                       typename _Iterator::difference_type,
00151                                       typename _Iterator::pointer,
00152                                       typename _Iterator::reference>>
00153     {
00154       typedef typename _Iterator::iterator_category iterator_category;
00155       typedef typename _Iterator::value_type        value_type;
00156       typedef typename _Iterator::difference_type   difference_type;
00157       typedef typename _Iterator::pointer           pointer;
00158       typedef typename _Iterator::reference         reference;
00159     };
00160 
00161   template<typename _Iterator>
00162     struct iterator_traits
00163     : public __iterator_traits<_Iterator> { };
00164 #else
00165   template<typename _Iterator>
00166     struct iterator_traits
00167     {
00168       typedef typename _Iterator::iterator_category iterator_category;
00169       typedef typename _Iterator::value_type        value_type;
00170       typedef typename _Iterator::difference_type   difference_type;
00171       typedef typename _Iterator::pointer           pointer;
00172       typedef typename _Iterator::reference         reference;
00173     };
00174 #endif
00175 
00176   /// Partial specialization for pointer types.
00177   template<typename _Tp>
00178     struct iterator_traits<_Tp*>
00179     {
00180       typedef random_access_iterator_tag iterator_category;
00181       typedef _Tp                         value_type;
00182       typedef ptrdiff_t                   difference_type;
00183       typedef _Tp*                        pointer;
00184       typedef _Tp&                        reference;
00185     };
00186 
00187   /// Partial specialization for const pointer types.
00188   template<typename _Tp>
00189     struct iterator_traits<const _Tp*>
00190     {
00191       typedef random_access_iterator_tag iterator_category;
00192       typedef _Tp                         value_type;
00193       typedef ptrdiff_t                   difference_type;
00194       typedef const _Tp*                  pointer;
00195       typedef const _Tp&                  reference;
00196     };
00197 
00198   /**
00199    *  This function is not a part of the C++ standard but is syntactic
00200    *  sugar for internal library use only.
00201   */
00202   template<typename _Iter>
00203     inline typename iterator_traits<_Iter>::iterator_category
00204     __iterator_category(const _Iter&)
00205     { return typename iterator_traits<_Iter>::iterator_category(); }
00206 
00207   //@}
00208 
00209   // If _Iterator has a base returns it otherwise _Iterator is returned
00210   // untouched
00211   template<typename _Iterator, bool _HasBase>
00212     struct _Iter_base
00213     {
00214       typedef _Iterator iterator_type;
00215       static iterator_type _S_base(_Iterator __it)
00216       { return __it; }
00217     };
00218 
00219   template<typename _Iterator>
00220     struct _Iter_base<_Iterator, true>
00221     {
00222       typedef typename _Iterator::iterator_type iterator_type;
00223       static iterator_type _S_base(_Iterator __it)
00224       { return __it.base(); }
00225     };
00226 
00227 #if __cplusplus >= 201103L
00228   template<typename _InIter>
00229     using _RequireInputIter = typename
00230       enable_if<is_convertible<typename
00231                 iterator_traits<_InIter>::iterator_category,
00232                                input_iterator_tag>::value>::type;
00233 #endif
00234 
00235 _GLIBCXX_END_NAMESPACE_VERSION
00236 } // namespace
00237 
00238 #endif /* _STL_ITERATOR_BASE_TYPES_H */
00239