KiCad PCB EDA Suite
iterators.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, you may find one here:
18  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19  * or you may search the http://www.gnu.org website for the version 2 license,
20  * or you may write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #ifndef __ITERATORS_H
25 #define __ITERATORS_H
26 
27 #include <dlist.h>
28 #include <iterator>
29 
30 template <class T>
31 class DLIST_ITERATOR : public std::iterator<std::bidirectional_iterator_tag, T>
32 {
33 private:
34  T m_obj;
35 
37 
38 public:
39  explicit DLIST_ITERATOR<T>( T obj ) :
40  m_obj(obj) {}
41 
43  {
44  m_obj = m_obj->Next(); return *this;
45  }
46 
48  {
49  m_obj = m_obj->Prev(); return *this;
50  }
51 
52  bool operator==( DLIST_ITERATOR<T> other ) const
53  {
54  return m_obj == other.m_obj;
55  }
56 
57  bool operator!=( DLIST_ITERATOR<T> other ) const
58  {
59  return !(*this == other);
60  }
61 
63  {
64  return m_obj;
65  }
66 };
67 
68 // helper object, used to convert a DLIST<T> to an iterator
69 template <class T>
71 {
72 public:
73  explicit DLIST_ITERATOR_WRAPPER<T> ( DLIST<T>& list ) :
74  m_list(list) {}
75 
77  {
78  return DLIST_ITERATOR<T*> ( m_list.GetFirst() );
79  }
80 
82  {
83  return DLIST_ITERATOR<T*> ( nullptr );
84  }
85 
86  unsigned int Size() const
87  {
88  return m_list.GetCount();
89  }
90 
91 private:
93 };
94 
95 #endif
DLIST_ITERATOR< T * > end()
Definition: iterators.h:81
DLIST< T > & m_list
Definition: iterators.h:92
DLIST_ITERATOR< T > & operator++()
Definition: iterators.h:42
Class DLIST is the head of a doubly linked list.
Definition: dlist.h:142
bool operator!=(DLIST_ITERATOR< T > other) const
Definition: iterators.h:57
typename DLIST_ITERATOR< T >::reference reference
Definition: iterators.h:36
unsigned int Size() const
Definition: iterators.h:86
DLIST_ITERATOR< T * > begin()
Definition: iterators.h:76
bool operator==(DLIST_ITERATOR< T > other) const
Definition: iterators.h:52
reference operator*()
Definition: iterators.h:62
DLIST_ITERATOR< T > & operator--()
Definition: iterators.h:47