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