KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kiid.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) 2020 Ian McInerney <[email protected]>
5 * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
6 * Copyright The KiCad Developers, see AUTHORS.TXT for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#ifndef KIID_H
23#define KIID_H
24
25#include <kicommon.h>
26#include <boost/uuid/uuid.hpp>
27#include <nlohmann/json_fwd.hpp>
28
29#include <string>
30
31class wxString;
32
41typedef uint32_t timestamp_t;
42
44{
45public:
46 KIID();
47 KIID( int null );
48 KIID( const std::string& aString );
49 KIID( const char* aString );
50 KIID( const wxString& aString );
51 KIID( timestamp_t aTimestamp );
52
53 void Clone( const KIID& aUUID );
54
55 size_t Hash() const;
56
57 bool IsLegacyTimestamp() const;
59
60 wxString AsString() const;
61 wxString AsLegacyTimestampString() const;
62 std::string AsStdString() const;
63
67 static bool SniffTest( const wxString& aCandidate );
68
74 static void CreateNilUuids( bool aNil = true );
75
85 static void SeedGenerator( unsigned int aSeed );
86
93
104 static KIID Combine( const KIID& aFirst, const KIID& aSecond );
105
125 static KIID FromDeterministicString( const wxString& aName );
126
133 void Increment();
134
135 bool operator==( KIID const& rhs ) const
136 {
137 return m_uuid == rhs.m_uuid;
138 }
139
140 bool operator!=( KIID const& rhs ) const
141 {
142 return m_uuid != rhs.m_uuid;
143 }
144
145 bool operator<( KIID const& rhs ) const
146 {
147 return m_uuid < rhs.m_uuid;
148 }
149
150 bool operator>( KIID const& rhs ) const
151 {
152 return m_uuid > rhs.m_uuid;
153 }
154
155private:
156 boost::uuids::uuid m_uuid;
157};
158
159
161
163
164class KICOMMON_API KIID_PATH : public std::vector<KIID>
165{
166public:
168 {
169 }
170
171 KIID_PATH( const wxString& aString );
172
173 bool MakeRelativeTo( const KIID_PATH& aPath );
174
184 bool EndsWith( const KIID_PATH& aPath ) const;
185
186 wxString AsString() const;
187
188 bool operator==( KIID_PATH const& rhs ) const
189 {
190 if( size() != rhs.size() )
191 return false;
192
193 for( size_t i = 0; i < size(); ++i )
194 {
195 if( at( i ) != rhs.at( i ) )
196 return false;
197 }
198
199 return true;
200 }
201
202 bool operator<( KIID_PATH const& rhs ) const
203 {
204 if( size() != rhs.size() )
205 return size() < rhs.size();
206
207 for( size_t i = 0; i < size(); ++i )
208 {
209 if( at( i ) < rhs.at( i ) )
210 return true;
211
212 if( at( i ) != rhs.at( i ) )
213 return false;
214 }
215
216 return false;
217 }
218
219 bool operator>( KIID_PATH const& rhs ) const
220 {
221 if( size() != rhs.size() )
222 return size() > rhs.size();
223
224 for( size_t i = 0; i < size(); ++i )
225 {
226 if( at( i ) > rhs.at( i ) )
227 return true;
228
229 if( at( i ) != rhs.at( i ) )
230 return false;
231 }
232
233 return false;
234 }
235
237 {
238 for( const KIID& kiid : aRhs )
239 emplace_back( kiid );
240
241 return *this;
242 }
243
244 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
245 {
246 aLhs += aRhs;
247 return aLhs;
248 }
249};
250
255{
256public:
258 {
259 KIID::CreateNilUuids( true );
260 };
261
263 {
264 KIID::CreateNilUuids( false );
265 }
266};
267
268KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
269
270KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
271
272template<> struct KICOMMON_API std::hash<KIID>
273{
274 std::size_t operator()( const KIID& aId ) const
275 {
276 return aId.Hash();
277 }
278};
279
280#endif // KIID_H
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition kiid.h:236
bool operator>(KIID_PATH const &rhs) const
Definition kiid.h:219
bool operator==(KIID_PATH const &rhs) const
Definition kiid.h:188
KIID_PATH()
Definition kiid.h:167
bool operator<(KIID_PATH const &rhs) const
Definition kiid.h:202
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition kiid.h:244
Definition kiid.h:44
KIID()
Definition kiid.cpp:73
static void SeedGenerator(unsigned int aSeed)
Re-initialize the UUID generator with a given seed (for testing or QA purposes)
Definition kiid.cpp:331
size_t Hash() const
Definition kiid.cpp:230
wxString AsString() const
Definition kiid.cpp:242
static KIID Combine(const KIID &aFirst, const KIID &aSecond)
Creates a deterministic KIID from two input KIIDs by XORing their underlying UUIDs.
Definition kiid.cpp:284
boost::uuids::uuid m_uuid
Definition kiid.h:156
void Increment()
Generates a deterministic replacement for a given ID.
Definition kiid.cpp:269
bool operator>(KIID const &rhs) const
Definition kiid.h:150
std::string AsStdString() const
Definition kiid.cpp:248
wxString AsLegacyTimestampString() const
Definition kiid.cpp:254
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:217
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition kiid.cpp:325
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition kiid.cpp:174
static KIID FromDeterministicString(const wxString &aName)
Build a deterministic UUID from an arbitrary name string.
Definition kiid.cpp:295
bool operator!=(KIID const &rhs) const
Definition kiid.h:140
bool operator==(KIID const &rhs) const
Definition kiid.h:135
void Clone(const KIID &aUUID)
Definition kiid.cpp:236
bool IsLegacyTimestamp() const
Definition kiid.cpp:211
bool operator<(KIID const &rhs) const
Definition kiid.h:145
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition kiid.cpp:260
#define KICOMMON_API
Definition kicommon.h:27
KIID niluuid(0)
KICOMMON_API KIID & NilUuid()
Definition kiid.cpp:66
KICOMMON_API void from_json(const nlohmann::json &aJson, KIID &aKIID)
Definition kiid.cpp:410
KICOMMON_API void to_json(nlohmann::json &aJson, const KIID &aKIID)
Definition kiid.cpp:404
uint32_t timestamp_t
timestamp_t is our type to represent unique IDs for all kinds of elements; historically simply the ti...
Definition kiid.h:41
STL namespace.
std::size_t operator()(const KIID &aId) const
Definition kiid.h:274