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 <array>
30#include <cstdint>
31#include <string>
32
33class wxString;
34
43typedef uint32_t timestamp_t;
44
46{
47public:
48 KIID();
49 KIID( int null );
50 KIID( const std::string& aString );
51 KIID( const char* aString );
52 KIID( const wxString& aString );
53 KIID( timestamp_t aTimestamp );
54
55 void Clone( const KIID& aUUID );
56
57 size_t Hash() const;
58
62 static KIID FromName( const std::string& aName );
63 static KIID FromBytes( const std::array<uint8_t, 16>& aBytes );
64
65 bool IsLegacyTimestamp() const;
67
68 wxString AsString() const;
69 wxString AsLegacyTimestampString() const;
70 std::string AsStdString() const;
71 std::array<uint8_t, 16> AsBytes() const;
72
76 static bool SniffTest( const wxString& aCandidate );
77
83 static void CreateNilUuids( bool aNil = true );
84
94 static void SeedGenerator( unsigned int aSeed );
95
102
113 static KIID Combine( const KIID& aFirst, const KIID& aSecond );
114
134 static KIID FromDeterministicString( const wxString& aName );
135
142 void Increment();
143
144 bool operator==( KIID const& rhs ) const
145 {
146 return m_uuid == rhs.m_uuid;
147 }
148
149 bool operator!=( KIID const& rhs ) const
150 {
151 return m_uuid != rhs.m_uuid;
152 }
153
154 bool operator<( KIID const& rhs ) const
155 {
156 return m_uuid < rhs.m_uuid;
157 }
158
159 bool operator>( KIID const& rhs ) const
160 {
161 return m_uuid > rhs.m_uuid;
162 }
163
164private:
165 boost::uuids::uuid m_uuid;
166};
167
168
170
172
173class KICOMMON_API KIID_PATH : public std::vector<KIID>
174{
175public:
177
178 KIID_PATH( const wxString& aString );
179
180 bool MakeRelativeTo( const KIID_PATH& aPath );
181
191 bool EndsWith( const KIID_PATH& aPath ) const;
192
193 wxString AsString() const;
194
195 bool operator==( KIID_PATH const& rhs ) const
196 {
197 if( size() != rhs.size() )
198 return false;
199
200 for( size_t i = 0; i < size(); ++i )
201 {
202 if( at( i ) != rhs.at( i ) )
203 return false;
204 }
205
206 return true;
207 }
208
209 bool operator<( KIID_PATH const& rhs ) const
210 {
211 if( size() != rhs.size() )
212 return size() < rhs.size();
213
214 for( size_t i = 0; i < size(); ++i )
215 {
216 if( at( i ) < rhs.at( i ) )
217 return true;
218
219 if( at( i ) != rhs.at( i ) )
220 return false;
221 }
222
223 return false;
224 }
225
226 bool operator>( KIID_PATH const& rhs ) const
227 {
228 if( size() != rhs.size() )
229 return size() > rhs.size();
230
231 for( size_t i = 0; i < size(); ++i )
232 {
233 if( at( i ) > rhs.at( i ) )
234 return true;
235
236 if( at( i ) != rhs.at( i ) )
237 return false;
238 }
239
240 return false;
241 }
242
244 {
245 for( const KIID& kiid : aRhs )
246 emplace_back( kiid );
247
248 return *this;
249 }
250
251 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
252 {
253 aLhs += aRhs;
254 return aLhs;
255 }
256};
257
268
269KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
270
271KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
272
273template <>
274struct KICOMMON_API std::hash<KIID>
275{
276 std::size_t operator()( const KIID& aId ) const { return aId.Hash(); }
277};
278
279
280template <>
281struct std::hash<KIID_PATH>
282{
283 std::size_t operator()( const KIID_PATH& aPath ) const
284 {
285 std::size_t seed = 0;
286
287 for( const KIID& kiid : aPath )
288 seed ^= std::hash<KIID>()( kiid ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
289
290 return seed;
291 }
292};
293
294#endif // KIID_H
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition kiid.h:243
bool operator>(KIID_PATH const &rhs) const
Definition kiid.h:226
bool operator==(KIID_PATH const &rhs) const
Definition kiid.h:195
KIID_PATH()
Definition kiid.h:176
bool operator<(KIID_PATH const &rhs) const
Definition kiid.h:209
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition kiid.h:251
Definition kiid.h:46
KIID()
Definition kiid.cpp:74
static void SeedGenerator(unsigned int aSeed)
Re-initialize the UUID generator with a given seed (for testing or QA purposes)
Definition kiid.cpp:361
size_t Hash() const
Definition kiid.cpp:231
wxString AsString() const
Definition kiid.cpp:264
std::array< uint8_t, 16 > AsBytes() const
Definition kiid.cpp:276
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:314
boost::uuids::uuid m_uuid
Definition kiid.h:165
void Increment()
Generates a deterministic replacement for a given ID.
Definition kiid.cpp:299
bool operator>(KIID const &rhs) const
Definition kiid.h:159
std::string AsStdString() const
Definition kiid.cpp:270
wxString AsLegacyTimestampString() const
Definition kiid.cpp:284
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:218
static KIID FromBytes(const std::array< uint8_t, 16 > &aBytes)
Definition kiid.cpp:250
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition kiid.cpp:355
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition kiid.cpp:175
static KIID FromDeterministicString(const wxString &aName)
Build a deterministic UUID from an arbitrary name string.
Definition kiid.cpp:325
bool operator!=(KIID const &rhs) const
Definition kiid.h:149
static KIID FromName(const std::string &aName)
Return a KIID derived from a name, the same name always gives the same KIID.
Definition kiid.cpp:237
bool operator==(KIID const &rhs) const
Definition kiid.h:144
void Clone(const KIID &aUUID)
Definition kiid.cpp:258
bool IsLegacyTimestamp() const
Definition kiid.cpp:212
bool operator<(KIID const &rhs) const
Definition kiid.h:154
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition kiid.cpp:290
#define KICOMMON_API
Definition kicommon.h:27
KIID niluuid(0)
KICOMMON_API KIID & NilUuid()
Definition kiid.cpp:67
KICOMMON_API void from_json(const nlohmann::json &aJson, KIID &aKIID)
Definition kiid.cpp:440
KICOMMON_API void to_json(nlohmann::json &aJson, const KIID &aKIID)
Definition kiid.cpp:434
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:43
STL namespace.
std::size_t operator()(const KIID &aId) const
Definition kiid.h:276
std::size_t operator()(const KIID_PATH &aPath) const
Definition kiid.h:283