KiCad PCB EDA Suite
class_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 <fctsys.h>
36 #include <base_struct.h> // SEARCH_RESULT
37 #include <common.h> // GetNewTimeStamp()
38 #include <class_eda_rect.h>
39 
40 
41 class EDA_ITEM;
42 
43 
56 class COLLECTOR
57 {
58 protected:
61 
64 
66  std::vector<EDA_ITEM*> m_List;
67 
70 
73 
76 
77 public:
79  // Inspect() is virtual so calling it from a class common inspector preserves polymorphism.
80  m_inspector( [=] ( EDA_ITEM* aItem, void* aTestData ) { return this->Inspect( aItem, aTestData ); } )
81  {
82  m_ScanTypes = 0;
83  m_TimeAtCollection = 0;
84  }
85 
86  virtual ~COLLECTOR() {}
87 
88  virtual SEARCH_RESULT Inspect( EDA_ITEM* aItem, void* aTestData ) = 0;
89 
98  bool IsValidIndex( int aIndex )
99  {
100  return ( (unsigned) aIndex < m_List.size() );
101  }
102 
107  int GetCount() const
108  {
109  return (int) m_List.size();
110  }
111 
116  void Empty()
117  {
118  m_List.clear();
119  }
120 
126  void Append( EDA_ITEM* item )
127  {
128  m_List.push_back( item );
129  }
130 
136  void Remove( int aIndex )
137  {
138  m_List.erase( m_List.begin() + aIndex );
139  }
140 
146  void Remove( const EDA_ITEM* aItem )
147  {
148  for( size_t i = 0; i < m_List.size(); i++ )
149  {
150  if( m_List[i] == aItem )
151  {
152  m_List.erase( m_List.begin() + i);
153  return;
154  }
155  }
156  }
157 
164  EDA_ITEM* operator[]( int aIndex ) const
165  {
166  if( (unsigned)aIndex < (unsigned)GetCount() ) // (unsigned) excludes aIndex<0 also
167  return m_List[ aIndex ];
168 
169  return NULL;
170  }
171 
178  EDA_ITEM* const* BasePtr() const
179  {
180  return &m_List[0];
181  }
182 
190  bool HasItem( const EDA_ITEM* aItem ) const
191  {
192  for( size_t i = 0; i < m_List.size(); i++ )
193  {
194  if( m_List[i] == aItem )
195  return true;
196  }
197 
198  return false;
199  }
200 
208  void SetScanTypes( const KICAD_T* scanTypes )
209  {
210  m_ScanTypes = scanTypes;
211  }
212 
213  void SetTimeNow()
214  {
215  m_TimeAtCollection = GetNewTimeStamp();
216  }
217 
218  time_t GetTime()
219  {
220  return m_TimeAtCollection;
221  }
222 
223  void SetRefPos( const wxPoint& aRefPos ) { m_RefPos = aRefPos; }
224  const wxPoint& GetRefPos() const { return m_RefPos; }
225 
226  void SetBoundingBox( const EDA_RECT& aRefBox ) { m_RefBox = aRefBox; }
227  const EDA_RECT& GetBoundingBox() const { return m_RefBox; }
228 
238  bool IsSimilarPointAndTime( const wxPoint& aRefPos )
239  {
240  const int distMax = 2; // adjust these here
241  const time_t timeMax = 3; // seconds
242 
243  int dx = abs( aRefPos.x - m_RefPos.x );
244  int dy = abs( aRefPos.y - m_RefPos.y );
245 
246  if( dx <= distMax && dy <= distMax &&
247  (int)GetNewTimeStamp() - m_TimeAtCollection <= timeMax )
248  return true;
249  else
250  return false;
251  }
258  int CountType( KICAD_T aType )
259  {
260  int cnt = 0;
261  for( size_t i = 0; i < m_List.size(); i++ )
262  {
263  if( m_List[i]->Type() == aType )
264  cnt++;
265  }
266  return cnt;
267  }
268 
292 };
293 
294 #endif // COLLECTOR_H
void Empty()
Function Empty sets the list to empty.
int GetCount() const
Function GetCount returns the number of objects in the list.
const wxPoint & GetRefPos() const
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:93
bool IsValidIndex(int aIndex)
Function IsValidIndex tests if aIndex is with the limits of the list of collected items...
void SetScanTypes(const KICAD_T *scanTypes)
Function SetScanTypes records the list of KICAD_T types to consider for collection by the Inspect() f...
EDA_ITEM * operator[](int aIndex) const
Function operator[int] is used for read only access and returns the object at aIndex.
int CountType(KICAD_T aType)
Function CountType counts the number of items matching aType.
#define abs(a)
Definition: auxiliary.h:84
void Remove(int aIndex)
Function Remove removes the item at aIndex (first position is 0);.
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.
timestamp_t GetNewTimeStamp()
Definition: common.cpp:166
INSPECTOR_FUNC m_inspector
a class common bridge into the polymorphic Inspect()
void SetRefPos(const wxPoint &aRefPos)
bool IsSimilarPointAndTime(const wxPoint &aRefPos)
Function IsSimilarPointAndTime returns true if the given reference point is "similar" (defined here) ...
virtual SEARCH_RESULT Inspect(EDA_ITEM *aItem, void *aTestData)=0
virtual ~COLLECTOR()
void SetTimeNow()
time_t m_TimeAtCollection
The time at which the collection was made.
void Remove(const EDA_ITEM *aItem)
Function Remove removes the item aItem (if exists in the collector).
Class EDA_RECT handles the component boundary box.
Class EDA_ITEM is a base class for most all the KiCad significant classes, used in schematics and boa...
Definition: base_struct.h:178
Class COLLECTOR is an abstract class that will find and hold all the objects according to an inspecti...
The common library.
bool HasItem(const EDA_ITEM *aItem) const
Function HasItem tests if aItem has already been collected.
void SetBoundingBox(const EDA_RECT &aRefBox)
EDA_RECT m_RefBox
A bounding box to test against, and that was used to make the collection.
wxPoint m_RefPos
A point to test against, and that was used to make the collection.
time_t GetTime()
Basic classes for most KiCad items.
SEARCH_RESULT
Definition: base_struct.h:82
std::vector< EDA_ITEM * > m_List
A place to hold collected objects without taking ownership of their memory.
const KICAD_T * m_ScanTypes
Which object types to scan.
EDA_ITEM *const * BasePtr() const
Function BasePtr returns the address of the first element in the array.
const EDA_RECT & GetBoundingBox() const