|
libpsynth 0.2.1
|
00001 00014 /* 00015 * Copyright (C) 2007 Juan Pedro BolĂvar Puente 00016 * 00017 * This file is part of Psychosynth. 00018 * 00019 * Psychosynth is free software: you can redistribute it and/or modify 00020 * it under the terms of the GNU General Public License as published by 00021 * the Free Software Foundation, either version 3 of the License, or 00022 * (at your option) any later version. 00023 * 00024 * Psychosynth is distributed in the hope that it will be useful, 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00027 * GNU General Public License for more details. 00028 * 00029 * You should have received a copy of the GNU General Public License 00030 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00031 * 00032 */ 00033 00034 #ifndef PSYNTH_BASE_ITERATOR_HPP_ 00035 #define PSYNTH_BASE_ITERATOR_HPP_ 00036 00037 #include <map> 00038 #include <iterator> 00039 00040 #include <psynth/base/type_traits.hpp> 00041 #include <psynth/base/pointer.hpp> 00042 00043 namespace psynth 00044 { 00045 namespace base 00046 { 00047 00048 template <typename Iterator> 00049 class ptr_iterator : public Iterator 00050 { 00051 public: 00052 typedef std::iterator_traits<Iterator> base_traits; 00053 00054 typedef typename std::iterator_traits<Iterator>::value_type ptr_value_type; 00055 typedef typename pointee<ptr_value_type>::type value_type; 00056 00057 typedef typename Iterator::iterator_category iterator_category; 00058 typedef typename Iterator::difference_type difference_type; 00059 typedef value_type* pointer; 00060 typedef value_type& reference; 00061 00062 ptr_iterator () = default; 00063 ptr_iterator (const ptr_iterator& it) = default; 00064 00065 ptr_iterator (ptr_iterator&& it) 00066 : Iterator (std::move (it)) 00067 {} 00068 00069 ptr_iterator (const Iterator& it) 00070 : Iterator (it) 00071 {} 00072 00073 ptr_iterator (Iterator&& it) 00074 : Iterator (std::move (it)) 00075 {} 00076 00077 template<class OtherIterator> 00078 ptr_iterator (const OtherIterator& it) 00079 : Iterator (it) 00080 {} 00081 00082 template<class OtherIterator> 00083 ptr_iterator (OtherIterator&& it) 00084 : Iterator (std::move (it)) 00085 {} 00086 00091 value_type& operator*() const 00092 { 00093 return *Iterator::operator* (); 00094 } 00095 00099 value_type* operator->() const 00100 { 00101 return &*Iterator::operator* (); 00102 } 00103 }; 00104 00105 template <typename Iterator> 00106 class ptr_const_iterator : public Iterator 00107 { 00108 public: 00109 typedef typename std::iterator_traits<Iterator>::value_type ptr_value_type; 00110 typedef typename pointee<ptr_value_type>::type value_type; 00111 00112 typedef typename Iterator::iterator_category iterator_category; 00113 typedef typename Iterator::difference_type difference_type; 00114 typedef value_type* pointer; 00115 typedef value_type& reference; 00116 00117 ptr_const_iterator () {} 00118 00119 ptr_const_iterator (const Iterator& it) 00120 : Iterator (it) 00121 {} 00122 00123 template<class OtherIterator> 00124 ptr_const_iterator (const OtherIterator& it) 00125 : Iterator (it) 00126 {} 00127 00128 ptr_const_iterator (const ptr_const_iterator& it) 00129 : Iterator (it) 00130 {} 00131 00136 const value_type& operator*() const 00137 { 00138 return *Iterator::operator* (); 00139 } 00140 00144 const value_type* operator->() const 00145 { 00146 return &*Iterator::operator* (); 00147 } 00148 }; 00149 00155 template <typename Key, typename Data> 00156 class map_iterator : public std::map<Key, Data>::iterator 00157 { 00158 public: 00159 typedef typename std::map<Key, Data>::iterator base_type; 00160 00161 typedef typename base_type::iterator_category iterator_category; 00162 typedef Data value_type; 00163 typedef typename base_type::difference_type difference_type; 00164 typedef Data* pointer; 00165 typedef Data& reference; 00166 00168 map_iterator () 00169 {} 00170 00172 map_iterator (const base_type& i) 00173 : base_type (i) {} 00174 00176 map_iterator (const map_iterator& i) 00177 : base_type ( 00178 static_cast<const base_type&>(i) 00179 ) 00180 {} 00181 00183 map_iterator (base_type&& i) 00184 : base_type (std::move (i)) {} 00185 00187 map_iterator (map_iterator&& i) 00188 : base_type ( 00189 std::move ( 00190 static_cast<const base_type&>(i) 00191 )) 00192 {} 00193 00197 Data& operator*() 00198 { 00199 return std::map<Key, Data>::iterator::operator*().second; 00200 } 00201 00205 Data* operator->() 00206 { 00207 return &std::map<Key, Data>::iterator::operator*().second; 00208 } 00209 00214 const Data& operator*() const 00215 { 00216 return std::map<Key, Data>::iterator::operator*().second; 00217 } 00218 00222 const Data* operator->() const 00223 { 00224 return &std::map<Key, Data>::iterator::operator*().second; 00225 } 00226 00230 map_iterator<Key, Data> operator++ () 00231 { 00232 return std::map<Key, Data>::iterator::operator++(); 00233 } 00234 00238 map_iterator<Key, Data> operator++ (int) 00239 { 00240 return std::map<Key, Data>::iterator::operator++(0); 00241 } 00242 }; 00243 00252 template <typename Key, typename Data> 00253 class map_const_iterator : public std::map<Key, Data>::const_iterator 00254 { 00255 public: 00256 typedef typename std::map<Key, Data>::const_iterator::iterator_category iterator_category; 00257 typedef Data value_type; 00258 typedef typename std::map<Key, Data>::const_iterator::difference_type difference_type; 00259 typedef Data* pointer; 00260 typedef Data& reference; 00261 00263 map_const_iterator (const typename std::map<Key, Data>::const_iterator& i) 00264 : std::map<Key, Data>::const_iterator(i) {} 00265 00267 map_const_iterator (const map_const_iterator<Key, Data>& i) 00268 : std::map<Key, Data>::const_iterator ( 00269 static_cast<const typename std::map<Key, Data>::const_iterator>(i)) 00270 {} 00271 00276 const Data& operator* () 00277 { 00278 return std::map<Key, Data>::const_iterator::operator*().second; 00279 } 00280 00285 const Data& operator* () const 00286 { 00287 return std::map<Key, Data>::const_iterator::operator*().second; 00288 } 00289 }; 00290 00299 template <typename Key, typename Data, class Iter> 00300 class map_iterator_base : public std::map<Key, Data>::iterator 00301 { 00302 public: 00303 typedef typename std::map<Key, Data>::iterator::iterator_category 00304 iterator_category; 00305 typedef Data value_type; 00306 typedef typename std::map<Key, Data>::iterator::difference_type 00307 difference_type; 00308 typedef Data* pointer; 00309 typedef Data& reference; 00310 00312 map_iterator_base () 00313 {} 00314 00316 map_iterator_base (const typename std::map<Key, Data>::iterator& i) 00317 : std::map<Key, Data>::iterator (i) 00318 {} 00319 00321 map_iterator_base (const map_iterator_base<Key, Data, Iter>& i) 00322 : std::map<Key, Data>::iterator ( 00323 static_cast<const typename std::map<Key, Data>::iterator> (i)) 00324 {} 00325 00329 Iter operator++ () 00330 { 00331 return std::map<Key, Data>::iterator::operator++(); 00332 } 00333 00337 Iter operator++ (int) 00338 { 00339 return std::map<Key, Data>::iterator::operator++(0); 00340 } 00341 }; 00342 00343 template <typename Key, typename Data, class Iter> 00344 class map_const_iterator_base : public std::map<Key, Data>::const_iterator 00345 { 00346 public: 00347 typedef typename std::map<Key, Data>::const_iterator::iterator_category 00348 iterator_category; 00349 typedef Data value_type; 00350 typedef typename std::map<Key, Data>::const_iterator::difference_type 00351 difference_type; 00352 typedef Data* pointer; 00353 typedef Data& reference; 00354 00356 map_const_iterator_base () 00357 {} 00358 00360 map_const_iterator_base (const typename std::map<Key, Data>::const_iterator& i) 00361 : std::map<Key, Data>::const_iterator (i) 00362 {} 00363 00365 map_const_iterator_base (const map_const_iterator_base<Key, Data, Iter>& i) 00366 : std::map<Key, Data>::const_iterator ( 00367 static_cast<const typename std::map<Key, Data>::const_iterator> (i)) 00368 {} 00369 00373 Iter operator++ () 00374 { 00375 return std::map<Key, Data>::const_iterator::operator++ (); 00376 } 00377 00381 Iter operator++ (int) 00382 { 00383 return std::map<Key, Data>::const_iterator::operator++ (0); 00384 } 00385 }; 00386 00391 template <typename Key, typename Data> 00392 class map_key_iterator : 00393 public map_iterator_base<Key, Data, map_key_iterator<Key, Data> > 00394 { 00395 public: 00396 map_key_iterator () {} 00397 00398 template <class T> 00399 map_key_iterator (const T& val) 00400 : map_iterator_base<Key, Data, map_key_iterator> (val) 00401 {} 00402 00407 const Key& operator*() const 00408 { 00409 return std::map<Key, Data>::iterator::operator*().first; 00410 } 00411 00415 const Key* operator->() const 00416 { 00417 return &std::map<Key, Data>::iterator::operator*().first; 00418 } 00419 }; 00420 00421 00422 } /* namespace base */ 00423 } /* namespace psynth */ 00424 00425 #endif /* PSYNTH_BASE_ITERATOR_HPP_ */
1.7.4