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