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 
89  using ITER = std::vector<EDA_ITEM*>::iterator;
90  using CITER = std::vector<EDA_ITEM*>::const_iterator;
91 
92  ITER begin() { return m_List.begin(); }
93  ITER end() { return m_List.end(); }
94  CITER begin() const { return m_List.cbegin(); }
95  CITER end() const { return m_List.cend(); }
96 
105  bool IsValidIndex( int aIndex )
106  {
107  return ( (unsigned) aIndex < m_List.size() );
108  }
109 
114  int GetCount() const
115  {
116  return (int) m_List.size();
117  }
118 
123  void Empty()
124  {
125  m_List.clear();
126  }
127 
133  void Append( EDA_ITEM* item )
134  {
135  m_List.push_back( item );
136  }
137 
143  void Remove( int aIndex )
144  {
145  m_List.erase( m_List.begin() + aIndex );
146  }
147 
153  void Remove( const EDA_ITEM* aItem )
154  {
155  for( size_t i = 0; i < m_List.size(); i++ )
156  {
157  if( m_List[i] == aItem )
158  {
159  m_List.erase( m_List.begin() + i);
160  return;
161  }
162  }
163  }
164 
171  EDA_ITEM* operator[]( int aIndex ) const
172  {
173  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
174  return m_List[ aIndex ];
175 
176  return NULL;
177  }
178 
185  EDA_ITEM* const* BasePtr() const
186  {
187  return &m_List[0];
188  }
189 
197  bool HasItem( const EDA_ITEM* aItem ) const
198  {
199  for( size_t i = 0; i < m_List.size(); i++ )
200  {
201  if( m_List[i] == aItem )
202  return true;
203  }
204 
205  return false;
206  }
207 
215  void SetScanTypes( const KICAD_T* scanTypes )
216  {
217  m_ScanTypes = scanTypes;
218  }
219 
220  void SetTimeNow()
221  {
222  m_TimeAtCollection = GetNewTimeStamp();
223  }
224 
226  {
227  return m_TimeAtCollection;
228  }
229 
230  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
231  const wxPoint& GetRefPos() const { return m_RefPos; }
232 
233  void SetBoundingBox( const EDA_RECT& aRefBox ) { m_RefBox = aRefBox; }
234  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
235 
245  bool IsSimilarPointAndTime( const wxPoint& aRefPos )
246  {
247  const int distMax = 2; // adjust these here
248  const timestamp_t timeMax = 3; // seconds
249 
250  int dx = abs( aRefPos.x - m_RefPos.x );
251  int dy = abs( aRefPos.y - m_RefPos.y );
252 
253  if( dx <= distMax && dy <= distMax &&
254  GetNewTimeStamp() - m_TimeAtCollection <= timeMax )
255  return true;
256  else
257  return false;
258  }
265  int CountType( KICAD_T aType )
266  {
267  int cnt = 0;
268  for( size_t i = 0; i < m_List.size(); i++ )
269  {
270  if( m_List[i]->Type() == aType )
271  cnt++;
272  }
273  return cnt;
274  }
275 
299 };
300 
301 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
Definition: collector.h:123
int GetCount() const
Function GetCount returns the number of objects in the list.
Definition: collector.h:114
const wxPoint & GetRefPos() const
Definition: collector.h:231
std::vector< EDA_ITEM * >::iterator ITER
Definition: collector.h:89
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:105
CITER begin() const
Definition: collector.h:94
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:215
ITER begin()
Definition: collector.h:92
EDA_ITEM * operator[](int aIndex) const
Function operator[int] is used for read only access and returns the object at aIndex.
Definition: collector.h:171
ITER end()
Definition: collector.h:93
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
Definition: collector.h:265
#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:143
KICAD_T
Enum KICAD_T is the set of class identification values, stored in EDA_ITEM::m_StructType.
Definition: typeinfo.h:78
CITER end() const
Definition: collector.h:95
void Append(EDA_ITEM *item)
Function Append adds an item to the end of the list.
Definition: collector.h:133
timestamp_t GetNewTimeStamp()
Definition: common.cpp:212
INSPECTOR_FUNC m_inspector
a class common bridge into the polymorphic Inspect()
Definition: collector.h:59
void SetRefPos(const wxPoint &aRefPos)
Definition: collector.h:230
bool IsSimilarPointAndTime(const wxPoint &aRefPos)
Function IsSimilarPointAndTime returns true if the given reference point is "similar" (defined here) ...
Definition: collector.h:245
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)=0
std::vector< EDA_ITEM * >::const_iterator CITER
Definition: collector.h:90
virtual ~COLLECTOR()
Definition: collector.h:85
void SetTimeNow()
Definition: collector.h:220
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Definition: collector.h:153
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:154
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:197
void SetBoundingBox(const EDA_RECT &aRefBox)
Definition: collector.h:233
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:225
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:185
const EDA_RECT & GetBoundingBox() const
Definition: collector.h:234