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
43{
44public:
50 explicit REFDES_TRACKER( bool aThreadSafe = false );
51
58 bool Insert( const std::string& aRefDes );
59
66 bool Contains( const std::string& aRefDes ) const;
67
77 int GetNextRefDes( const std::string& aPrefix, int aMinValue = 1 );
78
93 int GetNextRefDesForUnits( const SCH_REFERENCE& aRef,
94 const std::map<int, std::vector<SCH_REFERENCE>>& aRefNumberMap,
95 const std::vector<int>& aRequiredUnits, int aMinValue,
96 const std::function<bool( int )>& aAcceptNumber = {} );
97
105 std::string Serialize() const;
106
113 bool Deserialize( const std::string& aData );
114
118 void Clear();
119
125 size_t Size() const;
126
127 bool GetReuseRefDes() const { return m_reuseRefDes; }
128 void SetReuseRefDes( bool aReuse ) { m_reuseRefDes = aReuse; }
129
130private:
135 {
136 std::set<int> m_usedNumbers;
137 mutable std::map<int, int> m_nextCache;
138 mutable int m_baseNext;
139 mutable bool m_cacheValid;
140
141 PREFIX_DATA() : m_baseNext( 1 ), m_cacheValid( false ) {}
142 };
143
144 mutable std::mutex m_mutex;
146
148 std::unordered_map<std::string, PREFIX_DATA> m_prefixData;
149 std::unordered_set<std::string> m_allRefDes;
150
152
159 bool insertImpl( const std::string& aRefDes );
160
166 void clearImpl();
167
174 bool containsImpl( const std::string& aRefDes ) const;
175
182 std::pair<std::string, int> parseRefDes( const std::string& aRefDes ) const;
183
190 void updateCacheOnInsert( PREFIX_DATA& aData, int aInsertedNumber ) const;
191
199 int findNextAvailable( const PREFIX_DATA& aData, int aMinValue ) const;
200
209 bool areUnitsAvailable( const SCH_REFERENCE& aRef,
210 const std::vector<SCH_REFERENCE>& aRefVector,
211 const std::vector<int>& aRequiredUnits ) const;
212
220 bool insertNumber( const std::string& aPrefix, int aNumber );
221
228 std::string escapeForSerialization( const std::string& aStr ) const;
229
236 std::string unescapeFromSerialization( const std::string& aStr ) const;
237
245 std::vector<std::string> splitString( const std::string& aStr, char aDelimiter ) const;
246
247 void updateBaseNext( PREFIX_DATA& aData ) const;
248};
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.
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.
int GetNextRefDesForUnits(const SCH_REFERENCE &aRef, const std::map< int, std::vector< SCH_REFERENCE > > &aRefNumberMap, const std::vector< int > &aRequiredUnits, int aMinValue, const std::function< bool(int)> &aAcceptNumber={})
Get the next available reference designator number for multi-unit symbols.
std::mutex m_mutex
Mutex for thread safety.
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.
REFDES_TRACKER(bool aThreadSafe=false)
Constructor.
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 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.
void Clear()
Clear all stored reference designators.
A helper to define a symbol's reference designator in a schematic.
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.