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
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 2, 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 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 /*
00032  *
00033  * Copyright (c) 1994
00034  * Hewlett-Packard Company
00035  *
00036  * Permission to use, copy, modify, distribute and sell this software
00037  * and its documentation for any purpose is hereby granted without fee,
00038  * provided that the above copyright notice appear in all copies and
00039  * that both that copyright notice and this permission notice appear
00040  * in supporting documentation.  Hewlett-Packard Company makes no
00041  * representations about the suitability of this software for any
00042  * purpose.  It is provided "as is" without express or implied warranty.
00043  *
00044  *
00045  * Copyright (c) 1996-1998
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.  Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.  It is provided "as is" without express or implied warranty.
00055  */
00056 
00057 /** @file stl_iterator_base_types.h
00058  *  This is an internal header file, included by other library headers.
00059  *  You should not attempt to use it directly.
00060  *
00061  *  This file contains all of the general iterator-related utility types,
00062  *  such as iterator_traits and struct iterator.
00063  */
00064 
00065 #ifndef _STL_ITERATOR_BASE_TYPES_H
00066 #define _STL_ITERATOR_BASE_TYPES_H 1
00067 
00068 #pragma GCC system_header
00069 
00070 #include <bits/c++config.h>
00071 #include <cstddef>
00072 
00073 _GLIBCXX_BEGIN_NAMESPACE(std)
00074 
00075   //@{
00076   /**
00077    *  @defgroup iterator_tags Iterator Tags
00078    *  These are empty types, used to distinguish different iterators.  The
00079    *  distinction is not made by what they contain, but simply by what they
00080    *  are.  Different underlying algorithms can then be used based on the
00081    *  different operations supported by different iterator types.
00082   */
00083   ///  Marking input iterators.
00084   struct input_iterator_tag {};
00085   ///  Marking output iterators.
00086   struct output_iterator_tag {};
00087   /// Forward iterators support a superset of input iterator operations.
00088   struct forward_iterator_tag : public input_iterator_tag {};
00089   /// Bidirectional iterators support a superset of forward iterator
00090   /// operations.
00091   struct bidirectional_iterator_tag : public forward_iterator_tag {};
00092   /// Random-access iterators support a superset of bidirectional iterator
00093   /// operations.
00094   struct random_access_iterator_tag : public bidirectional_iterator_tag {};
00095   //@}
00096 
00097 
00098   /**
00099    *  @brief  Common %iterator class.
00100    *
00101    *  This class does nothing but define nested typedefs.  %Iterator classes
00102    *  can inherit from this class to save some work.  The typedefs are then
00103    *  used in specializations and overloading.
00104    *
00105    *  In particular, there are no default implementations of requirements
00106    *  such as @c operator++ and the like.  (How could there be?)
00107   */
00108   template<typename _Category, typename _Tp, typename _Distance = ptrdiff_t,
00109            typename _Pointer = _Tp*, typename _Reference = _Tp&>
00110     struct iterator
00111     {
00112       /// One of the @link iterator_tags tag types@endlink.
00113       typedef _Category  iterator_category;
00114       /// The type "pointed to" by the iterator.
00115       typedef _Tp        value_type;
00116       /// Distance between iterators is represented as this type.
00117       typedef _Distance  difference_type;
00118       /// This type represents a pointer-to-value_type.
00119       typedef _Pointer   pointer;
00120       /// This type represents a reference-to-value_type.
00121       typedef _Reference reference;
00122     };
00123 
00124   /**
00125    *  This class does nothing but define nested typedefs.  The general
00126    *  version simply "forwards" the nested typedefs from the Iterator
00127    *  argument.  Specialized versions for pointers and pointers-to-const
00128    *  provide tighter, more correct semantics.
00129   */
00130   template<typename _Iterator>
00131     struct iterator_traits
00132     {
00133       typedef typename _Iterator::iterator_category iterator_category;
00134       typedef typename _Iterator::value_type        value_type;
00135       typedef typename _Iterator::difference_type   difference_type;
00136       typedef typename _Iterator::pointer           pointer;
00137       typedef typename _Iterator::reference         reference;
00138     };
00139 
00140   template<typename _Tp>
00141     struct iterator_traits<_Tp*>
00142     {
00143       typedef random_access_iterator_tag iterator_category;
00144       typedef _Tp                         value_type;
00145       typedef ptrdiff_t                   difference_type;
00146       typedef _Tp*                        pointer;
00147       typedef _Tp&                        reference;
00148     };
00149 
00150   template<typename _Tp>
00151     struct iterator_traits<const _Tp*>
00152     {
00153       typedef random_access_iterator_tag iterator_category;
00154       typedef _Tp                         value_type;
00155       typedef ptrdiff_t                   difference_type;
00156       typedef const _Tp*                  pointer;
00157       typedef const _Tp&                  reference;
00158     };
00159 
00160   /**
00161    *  This function is not a part of the C++ standard but is syntactic
00162    *  sugar for internal library use only.
00163   */
00164   template<typename _Iter>
00165     inline typename iterator_traits<_Iter>::iterator_category
00166     __iterator_category(const _Iter&)
00167     { return typename iterator_traits<_Iter>::iterator_category(); }
00168 
00169 _GLIBCXX_END_NAMESPACE
00170 
00171 #endif /* _STL_ITERATOR_BASE_TYPES_H */
00172 

Generated on Wed Mar 26 00:43:14 2008 for libstdc++ by  doxygen 1.5.1