KiCad PCB EDA Suite
collector.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) 2007-2008 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
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 
30 #ifndef COLLECTOR_H
31 #define COLLECTOR_H
32 
33 #include <vector>
34 
35 #include <base_struct.h> // SEARCH_RESULT
36 #include <common.h> // GetNewTimeStamp()
37 #include <eda_rect.h>
38 
39 
40 class EDA_ITEM;
41 
42 
55 class COLLECTOR
56 {
57 protected:
60 
63 
65  std::vector<EDA_ITEM*> m_List;
66 
69 
72 
75 
76 public:
78  // Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
79  m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } )
80  {
81  m_ScanTypes = 0;
82  m_TimeAtCollection = 0;
83  }
84 
85  virtual ~COLLECTOR() {}
86 
87  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) = 0;
88 
97  bool IsValidIndex( int aIndex )
98  {
99  return ( (unsigned) aIndex < m_List.size() );
100  }
101 
106  int GetCount() const
107  {
108  return (int) m_List.size();
109  }
110 
115  void Empty()
116  {
117  m_List.clear();
118  }
119 
125  void Append( EDA_ITEM* item )
126  {
127  m_List.push_back( item );
128  }
129 
135  void Remove( int aIndex )
136  {
137  m_List.erase( m_List.begin() + aIndex );
138  }
139 
145  void Remove( const EDA_ITEM* aItem )
146  {
147  for( size_t i = 0; i < m_List.size(); i++ )
148  {
149  if( m_List[i] == aItem )
150  {
151  m_List.erase( m_List.begin() + i);
152  return;
153  }
154  }
155  }
156 
163  EDA_ITEM* operator[]( int aIndex ) const
164  {
165  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
166  return m_List[ aIndex ];
167 
168  return NULL;
169  }
170 
177  EDA_ITEM* const* BasePtr() const
178  {
179  return &m_List[0];
180  }
181 
189  bool HasItem( const EDA_ITEM* aItem ) const
190  {
191  for( size_t i = 0; i < m_List.size(); i++ )
192  {
193  if( m_List[i] == aItem )
194  return true;
195  }
196 
197  return false;
198  }
199 
207  void SetScanTypes( const KICAD_T* scanTypes )
208  {
209  m_ScanTypes = scanTypes;
210  }
211 
212  void SetTimeNow()
213  {
214  m_TimeAtCollection = GetNewTimeStamp();
215  }
216 
218  {
219  return m_TimeAtCollection;
220  }
221 
222  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
223  const wxPoint& GetRefPos() const { return m_RefPos; }
224 
225  void SetBoundingBox( const EDA_RECT& aRefBox ) { m_RefBox = aRefBox; }
226  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
227 
237  bool IsSimilarPointAndTime( const wxPoint& aRefPos )
238  {
239  const int distMax = 2; // adjust these here
240  const timestamp_t timeMax = 3; // seconds
241 
242  int dx = abs( aRefPos.x - m_RefPos.x );
243  int dy = abs( aRefPos.y - m_RefPos.y );
244 
245  if( dx <= distMax && dy <= distMax &&
246  GetNewTimeStamp() - m_TimeAtCollection <= timeMax )
247  return true;
248  else
249  return false;
250  }
257  int CountType( KICAD_T aType )
258  {
259  int cnt = 0;
260  for( size_t i = 0; i < m_List.size(); i++ )
261  {
262  if( m_List[i]->Type() == aType )
263  cnt++;
264  }
265  return cnt;
266  }
267 
291 };
292 
293 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:115
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:106
const wxPoint & GetRefPos() const
Definition: collector.h:223
std::function< SEARCH_RESULT(EDA_ITEM *aItem, void *aTestData) > INSPECTOR_FUNC
Typedef INSPECTOR is used to inspect and possibly collect the (search) results of iterating over a li...
Definition: base_struct.h:77
long timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition: common.h:52
bool IsValidIndex(int aIndex)
Function IsValidIndex tests if aIndex is with the limits of the list of collected items...
Definition: collector.h:97
void SetScanTypes(const KICAD_T *scanTypes)
Function SetScanTypes records the list of KICAD_T types to consider for collection by the Inspect() f...
Definition: collector.h:207
EDA_ITEM * operator[](int aIndex) const
Function operator[int] is used for read only access and returns the object at aIndex.
Definition: collector.h:163
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:257
#define abs(a)
Definition: auxiliary.h:84
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
Definition: collector.h:135
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
void Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:125
timestamp_t GetNewTimeStamp()
Definition: common.cpp:159
INSPECTOR_FUNC m_inspector
a class common bridge into the polymorphic Inspect()
Definition: collector.h:59
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:222
bool IsSimilarPointAndTime(const wxPoint &aRefPos)
Function IsSimilarPointAndTime returns true if the given reference point is "similar" (defined here) ...
Definition: collector.h:237
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)=0
virtual ~COLLECTOR()
Definition: collector.h:85
void SetTimeNow()
Definition: collector.h:212
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:145
size_t i
Definition: json11.cpp:597
Class EDA_RECT handles the component boundary box.
Definition: eda_rect.h:44
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:151
Class COLLECTOR is an abstract class that will find and hold all the objects according to an inspecti...
Definition: collector.h:55
The common library.
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
Definition: collector.h:189
void SetBoundingBox(const EDA_RECT &aRefBox)
Definition: collector.h:225
EDA_RECT m_RefBox
A bounding box to test against, and that was used to make the collection.
Definition: collector.h:71
wxPoint m_RefPos
A point to test against, and that was used to make the collection.
Definition: collector.h:68
COLLECTOR()
Definition: collector.h:77
Basic classes for most KiCad items.
SEARCH_RESULT
Definition: base_struct.h:66
timestamp_t m_TimeAtCollection
The time at which the collection was made.
Definition: collector.h:74
std::vector< EDA_ITEM * > m_List
A place to hold collected objects without taking ownership of their memory.
Definition: collector.h:65
timestamp_t GetTime()
Definition: collector.h:217
const KICAD_T * m_ScanTypes
Which object types to scan.
Definition: collector.h:62
EDA_ITEM *const * BasePtr() const
Function BasePtr returns the address of the first element in the array.
Definition: collector.h:177
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:226