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, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
26#ifndef KIID_H
27#define KIID_H
28
29#include <kicommon.h>
30#include <boost/uuid/uuid.hpp>
31#include <nlohmann/json_fwd.hpp>
32
33#include <string>
34
35class wxString;
36
45typedef uint32_t timestamp_t;
46
48{
49public:
50 KIID();
51 KIID( int null );
52 KIID( const std::string& aString );
53 KIID( const char* aString );
54 KIID( const wxString& aString );
55 KIID( timestamp_t aTimestamp );
56
57 void Clone( const KIID& aUUID );
58
59 size_t Hash() const;
60
61 bool IsLegacyTimestamp() const;
63
64 wxString AsString() const;
65 wxString AsLegacyTimestampString() const;
66 std::string AsStdString() const;
67
71 static bool SniffTest( const wxString& aCandidate );
72
78 static void CreateNilUuids( bool aNil = true );
79
89 static void SeedGenerator( unsigned int aSeed );
90
97
108 static KIID Combine( const KIID& aFirst, const KIID& aSecond );
109
116 void Increment();
117
118 bool operator==( KIID const& rhs ) const
119 {
120 return m_uuid == rhs.m_uuid;
121 }
122
123 bool operator!=( KIID const& rhs ) const
124 {
125 return m_uuid != rhs.m_uuid;
126 }
127
128 bool operator<( KIID const& rhs ) const
129 {
130 return m_uuid < rhs.m_uuid;
131 }
132
133 bool operator>( KIID const& rhs ) const
134 {
135 return m_uuid > rhs.m_uuid;
136 }
137
138private:
139 boost::uuids::uuid m_uuid;
140};
141
142
144
146
147class KICOMMON_API KIID_PATH : public std::vector<KIID>
148{
149public:
151 {
152 }
153
154 KIID_PATH( const wxString& aString );
155
156 bool MakeRelativeTo( const KIID_PATH& aPath );
157
167 bool EndsWith( const KIID_PATH& aPath ) const;
168
169 wxString AsString() const;
170
171 bool operator==( KIID_PATH const& rhs ) const
172 {
173 if( size() != rhs.size() )
174 return false;
175
176 for( size_t i = 0; i < size(); ++i )
177 {
178 if( at( i ) != rhs.at( i ) )
179 return false;
180 }
181
182 return true;
183 }
184
185 bool operator<( KIID_PATH const& rhs ) const
186 {
187 if( size() != rhs.size() )
188 return size() < rhs.size();
189
190 for( size_t i = 0; i < size(); ++i )
191 {
192 if( at( i ) < rhs.at( i ) )
193 return true;
194
195 if( at( i ) != rhs.at( i ) )
196 return false;
197 }
198
199 return false;
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
220 {
221 for( const KIID& kiid : aRhs )
222 emplace_back( kiid );
223
224 return *this;
225 }
226
227 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
228 {
229 aLhs += aRhs;
230 return aLhs;
231 }
232};
233
238{
239public:
241 {
242 KIID::CreateNilUuids( true );
243 };
244
246 {
247 KIID::CreateNilUuids( false );
248 }
249};
250
251KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
252
253KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
254
255template<> struct KICOMMON_API std::hash<KIID>
256{
257 std::size_t operator()( const KIID& aId ) const
258 {
259 return aId.Hash();
260 }
261};
262
263#endif // KIID_H
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition kiid.h:219
bool operator>(KIID_PATH const &rhs) const
Definition kiid.h:202
bool operator==(KIID_PATH const &rhs) const
Definition kiid.h:171
KIID_PATH()
Definition kiid.h:150
bool operator<(KIID_PATH const &rhs) const
Definition kiid.h:185
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition kiid.h:227
Definition kiid.h:48
KIID()
Definition kiid.cpp:75
static void SeedGenerator(unsigned int aSeed)
Re-initialize the UUID generator with a given seed (for testing or QA purposes)
Definition kiid.cpp:303
size_t Hash() const
Definition kiid.cpp:232
wxString AsString() const
Definition kiid.cpp:244
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:286
boost::uuids::uuid m_uuid
Definition kiid.h:139
void Increment()
Generates a deterministic replacement for a given ID.
Definition kiid.cpp:271
bool operator>(KIID const &rhs) const
Definition kiid.h:133
std::string AsStdString() const
Definition kiid.cpp:250
wxString AsLegacyTimestampString() const
Definition kiid.cpp:256
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:219
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition kiid.cpp:297
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition kiid.cpp:176
bool operator!=(KIID const &rhs) const
Definition kiid.h:123
bool operator==(KIID const &rhs) const
Definition kiid.h:118
void Clone(const KIID &aUUID)
Definition kiid.cpp:238
bool IsLegacyTimestamp() const
Definition kiid.cpp:213
bool operator<(KIID const &rhs) const
Definition kiid.h:128
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition kiid.cpp:262
#define KICOMMON_API
Definition kicommon.h:27
KIID niluuid(0)
KICOMMON_API KIID & NilUuid()
Definition kiid.cpp:68
KICOMMON_API void from_json(const nlohmann::json &aJson, KIID &aKIID)
Definition kiid.cpp:382
KICOMMON_API void to_json(nlohmann::json &aJson, const KIID &aKIID)
Definition kiid.cpp:376
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:45
STL namespace.
std::size_t operator()(const KIID &aId) const
Definition kiid.h:257