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 bool IsContainedWithin( const KIID_PATH& aParentPath ) const
194 {
195 return size() >= aParentPath.size() && std::equal( aParentPath.begin(), aParentPath.end(), begin() );
196 }
197
198 wxString AsString() const;
199
200 bool operator==( KIID_PATH const& rhs ) const
201 {
202 if( size() != rhs.size() )
203 return false;
204
205 for( size_t i = 0; i < size(); ++i )
206 {
207 if( at( i ) != rhs.at( i ) )
208 return false;
209 }
210
211 return true;
212 }
213
214 bool operator<( KIID_PATH const& rhs ) const
215 {
216 if( size() != rhs.size() )
217 return size() < rhs.size();
218
219 for( size_t i = 0; i < size(); ++i )
220 {
221 if( at( i ) < rhs.at( i ) )
222 return true;
223
224 if( at( i ) != rhs.at( i ) )
225 return false;
226 }
227
228 return false;
229 }
230
231 bool operator>( KIID_PATH const& rhs ) const
232 {
233 if( size() != rhs.size() )
234 return size() > rhs.size();
235
236 for( size_t i = 0; i < size(); ++i )
237 {
238 if( at( i ) > rhs.at( i ) )
239 return true;
240
241 if( at( i ) != rhs.at( i ) )
242 return false;
243 }
244
245 return false;
246 }
247
249 {
250 for( const KIID& kiid : aRhs )
251 emplace_back( kiid );
252
253 return *this;
254 }
255
256 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
257 {
258 aLhs += aRhs;
259 return aLhs;
260 }
261};
262
273
274KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
275
276KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
277
278template <>
279struct KICOMMON_API std::hash<KIID>
280{
281 std::size_t operator()( const KIID& aId ) const { return aId.Hash(); }
282};
283
284
285template <>
286struct std::hash<KIID_PATH>
287{
288 std::size_t operator()( const KIID_PATH& aPath ) const
289 {
290 std::size_t seed = 0;
291
292 for( const KIID& kiid : aPath )
293 seed ^= std::hash<KIID>()( kiid ) + 0x9e3779b9 + ( seed << 6 ) + ( seed >> 2 );
294
295 return seed;
296 }
297};
298
299#endif // KIID_H
bool IsContainedWithin(const KIID_PATH &aParentPath) const
Definition kiid.h:193
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition kiid.h:248
bool operator>(KIID_PATH const &rhs) const
Definition kiid.h:231
bool operator==(KIID_PATH const &rhs) const
Definition kiid.h:200
KIID_PATH()
Definition kiid.h:176
bool operator<(KIID_PATH const &rhs) const
Definition kiid.h:214
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition kiid.h:256
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:281
std::size_t operator()(const KIID_PATH &aPath) const
Definition kiid.h:288