KiCad PCB EDA Suite
Loading...
Searching...
No Matches
refdes_tracker.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * KiCad is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * KiCad is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with KiCad. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <unordered_map>
23#include <unordered_set>
24#include <string>
25#include <mutex>
26#include <set>
27#include <sstream>
28#include <map>
29#include <vector>
30#include <functional>
31#include <algorithm>
32
33// Forward declaration
34class SCH_REFERENCE;
35
47template<typename T>
48using UNITS_CHECKER_FUNC = std::function<bool(const T& aTestRef,
49 const std::vector<T>& aExistingRefs,
50 const std::vector<int>& aRequiredUnits)>;
51
59{
60public:
66 explicit REFDES_TRACKER( bool aThreadSafe = false );
67
74 bool Insert( const std::string& aRefDes );
75
82 bool Contains( const std::string& aRefDes ) const;
83
93 int GetNextRefDes( const std::string& aPrefix, int aMinValue = 1 );
94
108 int GetNextRefDesForUnits( const SCH_REFERENCE& aRef,
109 const std::map<int, std::vector<SCH_REFERENCE>>& aRefNumberMap,
110 const std::vector<int>& aRequiredUnits,
111 int aMinValue );
112
122
126 void ClearUnitsChecker();
127
135 std::string Serialize() const;
136
143 bool Deserialize( const std::string& aData );
144
148 void Clear();
149
155 size_t Size() const;
156
157 bool GetReuseRefDes() const { return m_reuseRefDes; }
158 void SetReuseRefDes( bool aReuse ) { m_reuseRefDes = aReuse; }
159
160private:
165 {
166 std::set<int> m_usedNumbers;
167 mutable std::map<int, int> m_nextCache;
168 mutable int m_baseNext;
169 mutable bool m_cacheValid;
170
171 PREFIX_DATA() : m_baseNext( 1 ), m_cacheValid( false ) {}
172 };
173
174 mutable std::mutex m_mutex;
176
178 std::unordered_map<std::string, PREFIX_DATA> m_prefixData;
179 std::unordered_set<std::string> m_allRefDes;
180
182
185
192 bool insertImpl( const std::string& aRefDes );
193
199 void clearImpl();
200
207 bool containsImpl( const std::string& aRefDes ) const;
208
215 std::pair<std::string, int> parseRefDes( const std::string& aRefDes ) const;
216
223 void updateCacheOnInsert( PREFIX_DATA& aData, int aInsertedNumber ) const;
224
232 int findNextAvailable( const PREFIX_DATA& aData, int aMinValue ) const;
233
242 bool areUnitsAvailable( const SCH_REFERENCE& aRef,
243 const std::vector<SCH_REFERENCE>& aRefVector,
244 const std::vector<int>& aRequiredUnits ) const;
245
253 bool insertNumber( const std::string& aPrefix, int aNumber );
254
261 std::string escapeForSerialization( const std::string& aStr ) const;
262
269 std::string unescapeFromSerialization( const std::string& aStr ) const;
270
278 std::vector<std::string> splitString( const std::string& aStr, char aDelimiter ) const;
279
280 void updateBaseNext( PREFIX_DATA& aData ) const;
281};
Class to efficiently track reference designators and provide next available designators.
void updateBaseNext(PREFIX_DATA &aData) const
std::string escapeForSerialization(const std::string &aStr) const
Escape special characters for serialization.
bool insertNumber(const std::string &aPrefix, int aNumber)
Insert a number for a specific prefix, updating internal structures.
void ClearUnitsChecker()
Clear the external units checker, reverting to default behavior.
bool Deserialize(const std::string &aData)
Deserialize tracker data from string representation.
std::vector< std::string > splitString(const std::string &aStr, char aDelimiter) const
Split string by delimiter, handling escaped characters.
int GetNextRefDes(const std::string &aPrefix, int aMinValue=1)
Get the next available reference designator for a given prefix and reserve it.
bool m_reuseRefDes
If true, allows reusing existing reference designators.
std::mutex m_mutex
Mutex for thread safety.
int GetNextRefDesForUnits(const SCH_REFERENCE &aRef, const std::map< int, std::vector< SCH_REFERENCE > > &aRefNumberMap, const std::vector< int > &aRequiredUnits, int aMinValue)
Get the next available reference designator number for multi-unit symbols.
std::unordered_set< std::string > m_allRefDes
void SetReuseRefDes(bool aReuse)
bool GetReuseRefDes() const
bool Insert(const std::string &aRefDes)
Insert a reference designator into the tracker.
void clearImpl()
Clear all internal data structures without locking.
size_t Size() const
Get the total count of stored reference designators.
std::string unescapeFromSerialization(const std::string &aStr) const
Unescape special characters from serialization.
bool insertImpl(const std::string &aRefDes)
Internal implementation of Insert without locking.
int findNextAvailable(const PREFIX_DATA &aData, int aMinValue) const
Find next available number for a prefix starting from a minimum value.
bool areUnitsAvailable(const SCH_REFERENCE &aRef, const std::vector< SCH_REFERENCE > &aRefVector, const std::vector< int > &aRequiredUnits) const
Check if all required units are available for a given reference number.
void SetUnitsChecker(const UNITS_CHECKER_FUNC< SCH_REFERENCE > &aChecker)
Set an external units checker function for SCH_REFERENCE objects.
void updateCacheOnInsert(PREFIX_DATA &aData, int aInsertedNumber) const
Update cached next available values when a number is inserted.
std::string Serialize() const
Serialize the tracker data to a compact string representation.
std::unordered_map< std::string, PREFIX_DATA > m_prefixData
Map from prefix to its tracking data.
bool m_threadSafe
True if thread safety is enabled.
bool Contains(const std::string &aRefDes) const
Check if a reference designator exists in the tracker.
std::pair< std::string, int > parseRefDes(const std::string &aRefDes) const
Parse a reference designator into prefix and numerical suffix.
bool containsImpl(const std::string &aRefDes) const
Check if a reference designator exists in the tracker without locking.
UNITS_CHECKER_FUNC< SCH_REFERENCE > m_externalUnitsChecker
External units checker function (optional)
void Clear()
Clear all stored reference designators.
A helper to define a symbol's reference designator in a schematic.
std::function< bool(const T &aTestRef, const std::vector< T > &aExistingRefs, const std::vector< int > &aRequiredUnits)> UNITS_CHECKER_FUNC
Function type for external units availability checking.
Data structure for tracking used numbers and caching next available values.
std::set< int > m_usedNumbers
Sorted set of used numbers for this prefix.
bool m_cacheValid
True if m_baseNext cache is valid.
int m_baseNext
Next available from 1 (cached)
std::map< int, int > m_nextCache
Cache of next available number for given min values.