KiCad PCB EDA Suite
intrusive_list.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) 2017 CERN
5  * @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 #ifndef INTRUSIVE_LIST_H
26 #define INTRUSIVE_LIST_H
27 
29 template <class T>
31 {
32 public:
34  {
35  ListClear();
36  }
37 
38  void ListClear()
39  {
40  m_prev = nullptr;
41  m_next = nullptr;
42  m_root = (T*) this;
43  m_count = 1;
44  }
45 
47  {
48  if( m_prev )
49  m_prev->m_next = m_next;
50 
51  if( m_next )
52  m_next->m_prev = m_prev;
53 
54  m_root->m_count--;
55 
56  T* rv = nullptr;
57 
58  if( m_prev )
59  rv = m_prev;
60  else if( m_next )
61  rv = m_next;
62 
63  m_root = nullptr;
64  m_prev = nullptr;
65  m_next = nullptr;
66  return rv;
67  }
68 
69  int ListSize() const
70  {
71  return m_root ? m_root->m_count : 0;
72  }
73 
74  void ListInsert( T* item )
75  {
76  if( !m_root )
77  m_root = item;
78 
79  if( m_next )
80  m_next->m_prev = item;
81 
82  item->m_prev = (T*) this;
83  item->m_next = m_next;
84  item->m_root = m_root;
85  m_root->m_count++;
86 
87  m_next = item;
88  }
89 
90  T* ListNext() const { return m_next; };
91  T* ListPrev() const { return m_prev; };
92 
93 private:
94  int m_count;
95  T* m_prev;
96  T* m_next;
97  T* m_root;
98 };
99 
100 #endif /* INTRUSIVE_LIST_H */
void ListInsert(T *item)
int ListSize() const
A lightweight intrusive list container
T * ListNext() const
T * ListPrev() const