Stefan Scholz / ETL
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers fixed_iterator.h Source File

fixed_iterator.h

Go to the documentation of this file.
00001 ///\file
00002 
00003 /******************************************************************************
00004 The MIT License(MIT)
00005 
00006 Embedded Template Library.
00007 https://github.com/ETLCPP/etl
00008 http://www.etlcpp.com
00009 
00010 Copyright(c) 2015 jwellbelove
00011 
00012 Permission is hereby granted, free of charge, to any person obtaining a copy
00013 of this software and associated documentation files(the "Software"), to deal
00014 in the Software without restriction, including without limitation the rights
00015 to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
00016 copies of the Software, and to permit persons to whom the Software is
00017 furnished to do so, subject to the following conditions :
00018 
00019 The above copyright notice and this permission notice shall be included in all
00020 copies or substantial portions of the Software.
00021 
00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00023 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00024 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
00025 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00026 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00028 SOFTWARE.
00029 ******************************************************************************/
00030 
00031 #ifndef __ETL_FIXED_ITERATOR__
00032 #define __ETL_FIXED_ITERATOR__
00033 
00034 #include <iterator>
00035 
00036 #include "platform.h "
00037 
00038 ///\defgroup iterator Iterator types
00039 
00040 namespace etl
00041 {
00042   /// A fixed iterator class.
00043   /// This iterator can be given an iterator value, which will not be allowed to be incremented or decremented.
00044   /// This can be useful when using STL algorithms to interact with fixed memory locations such as registers.
00045   ///\ingroup iterator
00046   template <typename TIterator>
00047   class fixed_iterator : std::iterator<typename std::iterator_traits<TIterator>::iterator_category, typename std::iterator_traits<TIterator>::value_type>
00048   {
00049   public:
00050 
00051     //***************************************************************************
00052     /// Default constructor.
00053     //***************************************************************************
00054     fixed_iterator()
00055       : it(TIterator())
00056     {
00057     }
00058 
00059     //***************************************************************************
00060     /// Construct from iterator.
00061     //***************************************************************************
00062     fixed_iterator(TIterator it_)
00063       : it(it_)
00064     {
00065     }
00066 
00067     //***************************************************************************
00068     /// Increment (Does nothing).
00069     //***************************************************************************
00070     fixed_iterator& operator ++()
00071     {
00072       return *this;
00073     }
00074 
00075     //***************************************************************************
00076     /// Increment (Does nothing).
00077     //***************************************************************************
00078     fixed_iterator operator ++(int)
00079     {
00080       return *this;
00081     }
00082 
00083     //***************************************************************************
00084     /// Decrement (Does nothing).
00085     //***************************************************************************
00086     fixed_iterator& operator --()
00087     {
00088       return *this;
00089     }
00090 
00091     //***************************************************************************
00092     /// Decrement (Does nothing).
00093     //***************************************************************************
00094     fixed_iterator operator --(int)
00095     {
00096       return *this;
00097     }
00098 
00099     //***************************************************************************
00100     /// Dereference operator.
00101     //***************************************************************************
00102     typename std::iterator_traits<TIterator>::value_type operator *()
00103     {
00104       return *it;
00105     }
00106 
00107     //***************************************************************************
00108     /// Dereference operator.
00109     //***************************************************************************
00110     const typename std::iterator_traits<TIterator>::value_type operator *() const
00111     {
00112       return *it;
00113     }
00114 
00115     //***************************************************************************
00116     /// -> operator.
00117     //***************************************************************************
00118     TIterator operator ->()
00119     {
00120       return it;
00121     }
00122 
00123     //***************************************************************************
00124     /// -> operator.
00125     //***************************************************************************
00126     const TIterator operator ->() const
00127     {
00128       return it;
00129     }
00130 
00131     //***************************************************************************
00132     /// Conversion operator.
00133     //***************************************************************************
00134     operator TIterator() const
00135     {
00136       return it;
00137     }
00138 
00139     //***************************************************************************
00140     /// += operator.
00141     //***************************************************************************
00142     fixed_iterator& operator +=(typename std::iterator_traits<TIterator>::difference_type /*offset*/)
00143     {
00144       return *this;
00145     }
00146 
00147     //***************************************************************************
00148     /// -= operator.
00149     //***************************************************************************
00150     fixed_iterator& operator -=(typename std::iterator_traits<TIterator>::difference_type /*offset*/)
00151     {
00152       return *this;
00153     }
00154 
00155     //***************************************************************************
00156     /// Assignment from iterator.
00157     //***************************************************************************
00158     fixed_iterator& operator =(TIterator new_it)
00159     {
00160       it = new_it;
00161       return *this;
00162     }
00163 
00164     //***************************************************************************
00165     /// Assignment from fixed_iterator.
00166     //***************************************************************************
00167     fixed_iterator& operator =(fixed_iterator other)
00168     {
00169       it = other.it;
00170       return *this;
00171     }
00172 
00173   private:
00174 
00175     TIterator it; ///< The underlying iterator.
00176   };
00177 }
00178 
00179 //*****************************************************************************
00180 /// + difference operator.
00181 //*****************************************************************************
00182 template <typename TIterator>
00183 etl::fixed_iterator<TIterator>& operator +(etl::fixed_iterator<TIterator>& lhs,
00184                                            typename std::iterator_traits<TIterator>::difference_type /*rhs*/)
00185 {
00186   return lhs;
00187 }
00188 
00189 //*****************************************************************************
00190 /// - difference operator.
00191 //*****************************************************************************
00192 template <typename TIterator>
00193 etl::fixed_iterator<TIterator>& operator -(etl::fixed_iterator<TIterator>& lhs,
00194                                            typename std::iterator_traits<TIterator>::difference_type /*rhs*/)
00195 {
00196   return lhs;
00197 }
00198 
00199 //*****************************************************************************
00200 /// - fixed_iterator operator.
00201 //*****************************************************************************
00202 template <typename TIterator>
00203 typename std::iterator_traits<TIterator>::difference_type operator -(etl::fixed_iterator<TIterator>& lhs,
00204                                                                      etl::fixed_iterator<TIterator>& rhs)
00205 {
00206   return TIterator(lhs) - TIterator(rhs);
00207 }
00208 
00209 //*****************************************************************************
00210 /// Equality operator. fixed_iterator == fixed_iterator.
00211 //*****************************************************************************
00212 template <typename TIterator>
00213 bool operator ==(const etl::fixed_iterator<TIterator>& lhs,
00214                  const etl::fixed_iterator<TIterator>& rhs)
00215 {
00216   return TIterator(lhs) == TIterator(rhs);
00217 }
00218 
00219 //*****************************************************************************
00220 /// Equality operator. fixed_iterator == iterator.
00221 //*****************************************************************************
00222 template <typename TIterator>
00223 bool operator ==(const etl::fixed_iterator<TIterator>& lhs,
00224                  TIterator rhs)
00225 {
00226   return TIterator(lhs) == rhs;
00227 }
00228 
00229 //*****************************************************************************
00230 /// Equality operator. iterator == fixed_iterator.
00231 //*****************************************************************************
00232 template <typename TIterator>
00233 bool operator ==(TIterator lhs,
00234                  const etl::fixed_iterator<TIterator>& rhs)
00235 {
00236   return lhs == TIterator(rhs);
00237 }
00238 
00239 
00240 //*****************************************************************************
00241 /// Inequality operator. fixed_iterator == fixed_iterator.
00242 //*****************************************************************************
00243 template <typename TIterator>
00244 bool operator !=(const etl::fixed_iterator<TIterator>& lhs,
00245                  const etl::fixed_iterator<TIterator>& rhs)
00246 {
00247   return !(lhs == rhs);
00248 }
00249 
00250 //*****************************************************************************
00251 /// Inequality operator. fixed_iterator == iterator.
00252 //*****************************************************************************
00253 template <typename TIterator>
00254 bool operator !=(const etl::fixed_iterator<TIterator>& lhs,
00255                  TIterator rhs)
00256 {
00257   return !(lhs == rhs);
00258 }
00259 
00260 //*****************************************************************************
00261 /// Inequality operator. iterator == fixed_iterator.
00262 //*****************************************************************************
00263 template <typename TIterator>
00264 bool operator !=(TIterator& lhs,
00265                  const etl::fixed_iterator<TIterator>& rhs)
00266 {
00267   return !(lhs == rhs);
00268 }
00269 
00270 #endif
00271