KiCad PCB EDA Suite
pns_itemset.h
Go to the documentation of this file.
1 /*
2  * KiRouter - a push-and-(sometimes-)shove PCB router
3  *
4  * Copyright (C) 2013-2014 CERN
5  * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
6  * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
7  *
8  * This program is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation, either version 3 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #ifndef __PNS_ITEMSET_H
23 #define __PNS_ITEMSET_H
24 
25 #include <vector>
26 #include <core/kicad_algo.h>
27 #include "pns_item.h"
28 
29 namespace PNS {
30 
37 class LINE;
38 
39 class ITEM_SET
40 {
41 public:
42  struct ENTRY
43  {
44  ENTRY( ITEM* aItem, bool aOwned = false ) :
45  item( aItem ),
46  owned( aOwned )
47  {}
48 
49  ENTRY( const ENTRY& aOther )
50  {
51  owned = aOther.owned;
52 
53  if( aOther.owned )
54  item = aOther.item->Clone();
55  else
56  item = aOther.item;
57  }
58 
60  {
61  if( owned )
62  delete item;
63  }
64 
65  bool operator==( const ENTRY& b ) const
66  {
67  return item == b.item;
68  }
69 
70  bool operator<( const ENTRY& b ) const
71  {
72  return item < b.item;
73  }
74 
75  ENTRY& operator=( const ENTRY& aOther )
76  {
77  owned = aOther.owned;
78 
79  if( aOther.owned )
80  item = aOther.item->Clone();
81  else
82  item = aOther.item;
83 
84  return *this;
85  }
86 
87  operator ITEM* () const
88  {
89  return item;
90  }
91 
93  bool owned;
94  };
95 
96  typedef std::vector<ENTRY> ENTRIES;
97 
98  ITEM_SET( ITEM* aInitialItem = NULL, bool aBecomeOwner = false )
99  {
100  if( aInitialItem )
101  m_items.push_back( ENTRY( aInitialItem, aBecomeOwner ) );
102  }
103 
104  ITEM_SET( const ITEM_SET& aOther )
105  {
106  m_items = aOther.m_items;
107  }
108 
109  ~ITEM_SET();
110 
111  ITEM_SET& operator=( const ITEM_SET& aOther )
112  {
113  m_items = aOther.m_items;
114  return *this;
115  }
116 
117  int Count( int aKindMask = -1 ) const
118  {
119  int n = 0;
120 
121  if( aKindMask == -1 || aKindMask == ITEM::ANY_T )
122  return m_items.size();
123 
124  for( ITEM* item : m_items )
125  {
126  if( item->Kind() & aKindMask )
127  n++;
128  }
129 
130  return n;
131  }
132 
133  bool Empty() const
134  {
135  return m_items.empty();
136  }
137 
138  ENTRIES& Items() { return m_items; }
139  const ENTRIES& CItems() const { return m_items; }
140 
141  ITEM_SET& FilterLayers( int aStart, int aEnd = -1, bool aInvert = false );
142  ITEM_SET& FilterKinds( int aKindMask, bool aInvert = false );
143  ITEM_SET& FilterNet( int aNet, bool aInvert = false );
144  ITEM_SET& FilterMarker( int aMarker, bool aInvert = false );
145 
146  ITEM_SET& ExcludeLayers( int aStart, int aEnd = -1 )
147  {
148  return FilterLayers( aStart, aEnd, true );
149  }
150 
151  ITEM_SET& ExcludeKinds( int aKindMask )
152  {
153  return FilterKinds( aKindMask, true );
154  }
155 
156  ITEM_SET& ExcludeNet( int aNet )
157  {
158  return FilterNet( aNet, true );
159  }
160 
161  ITEM_SET& ExcludeItem( const ITEM* aItem );
162 
163  int Size() const
164  {
165  return m_items.size();
166  }
167 
168  void Add( const LINE& aLine );
169  void Prepend( const LINE& aLine );
170 
171  ITEM* operator[] ( int index ) const
172  {
173  return m_items[index].item;
174  }
175 
176  void Add( ITEM* aItem, bool aBecomeOwner = false )
177  {
178  m_items.push_back( ENTRY( aItem, aBecomeOwner ) );
179  }
180 
181  void Prepend( ITEM* aItem, bool aBecomeOwner = false )
182  {
183  m_items.insert( m_items.begin(), ENTRY( aItem, aBecomeOwner ) );
184  }
185 
186  void Clear()
187  {
188  m_items.clear();
189  }
190 
191  bool Contains( ITEM* aItem ) const
192  {
193  const ENTRY ent( aItem );
194  return alg::contains( m_items, ent );
195  }
196 
197  void Erase( ITEM* aItem )
198  {
199  ENTRY ent( aItem );
200  ENTRIES::iterator f = std::find( m_items.begin(), m_items.end(), ent );
201 
202  if( f != m_items.end() )
203  m_items.erase( f );
204  }
205 
206  template<class T>
207  T* FindByKind( ITEM::PnsKind kind, int index = 0 )
208  {
209  int n = 0;
210 
211  for( const ITEM* item : m_items )
212  {
213  if( item->OfKind( kind ) )
214  {
215  if( index == n )
216  return static_cast<T*>( item );
217  else
218  n++;
219  }
220  }
221 
222  return NULL;
223  }
224 
225 private:
227 };
228 
229 }
230 
231 #endif
Base class for PNS router board items.
Definition: pns_item.h:55
virtual ITEM * Clone() const =0
Return a deep copy of the item.
int Count(int aKindMask=-1) const
Definition: pns_itemset.h:117
void Prepend(ITEM *aItem, bool aBecomeOwner=false)
Definition: pns_itemset.h:181
ITEM_SET & ExcludeNet(int aNet)
Definition: pns_itemset.h:156
ITEM_SET & ExcludeLayers(int aStart, int aEnd=-1)
Definition: pns_itemset.h:146
bool Contains(ITEM *aItem) const
Definition: pns_itemset.h:191
bool Empty() const
Definition: pns_itemset.h:133
bool operator==(const ENTRY &b) const
Definition: pns_itemset.h:65
void Prepend(const LINE &aLine)
Definition: pns_itemset.cpp:39
ENTRIES & Items()
Definition: pns_itemset.h:138
ITEM_SET & FilterKinds(int aKindMask, bool aInvert=false)
Definition: pns_itemset.cpp:68
std::vector< ENTRY > ENTRIES
Definition: pns_itemset.h:96
ITEM_SET & ExcludeKinds(int aKindMask)
Definition: pns_itemset.h:151
int Size() const
Definition: pns_itemset.h:163
Represents a track on a PCB, connecting two non-trivial joints (that is, vias, pads,...
Definition: pns_line.h:60
void Add(ITEM *aItem, bool aBecomeOwner=false)
Definition: pns_itemset.h:176
ITEM_SET & ExcludeItem(const ITEM *aItem)
bool operator<(const ENTRY &b) const
Definition: pns_itemset.h:70
void Erase(ITEM *aItem)
Definition: pns_itemset.h:197
void Add(const LINE &aLine)
Definition: pns_itemset.cpp:32
ITEM_SET & FilterMarker(int aMarker, bool aInvert=false)
Definition: pns_itemset.cpp:84
T * FindByKind(ITEM::PnsKind kind, int index=0)
Definition: pns_itemset.h:207
ENTRIES m_items
Definition: pns_itemset.h:226
#define NULL
ENTRY & operator=(const ENTRY &aOther)
Definition: pns_itemset.h:75
ITEM_SET & operator=(const ITEM_SET &aOther)
Definition: pns_itemset.h:111
ITEM_SET & FilterNet(int aNet, bool aInvert=false)
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
Definition: kicad_algo.h:81
ITEM_SET(ITEM *aInitialItem=NULL, bool aBecomeOwner=false)
Definition: pns_itemset.h:98
ITEM_SET & FilterLayers(int aStart, int aEnd=-1, bool aInvert=false)
Definition: pns_itemset.cpp:46
ITEM * operator[](int index) const
Definition: pns_itemset.h:171
ITEM_SET(const ITEM_SET &aOther)
Definition: pns_itemset.h:104
const ENTRIES & CItems() const
Definition: pns_itemset.h:139
ENTRY(ITEM *aItem, bool aOwned=false)
Definition: pns_itemset.h:44
Push and Shove diff pair dimensions (gap) settings dialog.
ENTRY(const ENTRY &aOther)
Definition: pns_itemset.h:49