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 <macros_swig.h>
32#include <nlohmann/json_fwd.hpp>
33
34#include <string>
35
36class wxString;
37
46typedef uint32_t timestamp_t;
47
49{
50public:
51 KIID();
52 KIID( int null );
53 KIID( const std::string& aString );
54 KIID( const char* aString );
55 KIID( const wxString& aString );
56 KIID( timestamp_t aTimestamp );
57
58 void Clone( const KIID& aUUID );
59
60 size_t Hash() const;
61
62 bool IsLegacyTimestamp() const;
64
65 wxString AsString() const;
66 wxString AsLegacyTimestampString() const;
67 std::string AsStdString() const;
68
72 static bool SniffTest( const wxString& aCandidate );
73
79 static void CreateNilUuids( bool aNil = true );
80
90 static void SeedGenerator( unsigned int aSeed );
91
98
109 static KIID Combine( const KIID& aFirst, const KIID& aSecond );
110
117 void Increment();
118
119 bool operator==( KIID const& rhs ) const
120 {
121 return m_uuid == rhs.m_uuid;
122 }
123
124 bool operator!=( KIID const& rhs ) const
125 {
126 return m_uuid != rhs.m_uuid;
127 }
128
129 bool operator<( KIID const& rhs ) const
130 {
131 return m_uuid < rhs.m_uuid;
132 }
133
134 bool operator>( KIID const& rhs ) const
135 {
136 return m_uuid > rhs.m_uuid;
137 }
138
139private:
140 boost::uuids::uuid m_uuid;
141};
142
143
145
147
148// declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
149DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
150
151class KICOMMON_API KIID_PATH : public KIID_VECT_LIST
152{
153public:
155 {
156 }
157
158 KIID_PATH( const wxString& aString );
159
160 bool MakeRelativeTo( const KIID_PATH& aPath );
161
171 bool EndsWith( const KIID_PATH& aPath ) const;
172
173 wxString AsString() const;
174
175 bool operator==( KIID_PATH const& rhs ) const
176 {
177 if( size() != rhs.size() )
178 return false;
179
180 for( size_t i = 0; i < size(); ++i )
181 {
182 if( at( i ) != rhs.at( i ) )
183 return false;
184 }
185
186 return true;
187 }
188
189 bool operator<( KIID_PATH const& rhs ) const
190 {
191 if( size() != rhs.size() )
192 return size() < rhs.size();
193
194 for( size_t i = 0; i < size(); ++i )
195 {
196 if( at( i ) < rhs.at( i ) )
197 return true;
198
199 if( at( i ) != rhs.at( i ) )
200 return false;
201 }
202
203 return false;
204 }
205
206 bool operator>( KIID_PATH const& rhs ) const
207 {
208 if( size() != rhs.size() )
209 return size() > rhs.size();
210
211 for( size_t i = 0; i < size(); ++i )
212 {
213 if( at( i ) > rhs.at( i ) )
214 return true;
215
216 if( at( i ) != rhs.at( i ) )
217 return false;
218 }
219
220 return false;
221 }
222
224 {
225 for( const KIID& kiid : aRhs )
226 emplace_back( kiid );
227
228 return *this;
229 }
230
231 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
232 {
233 aLhs += aRhs;
234 return aLhs;
235 }
236};
237
242{
243public:
245 {
246 KIID::CreateNilUuids( true );
247 };
248
250 {
251 KIID::CreateNilUuids( false );
252 }
253};
254
255KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
256
257KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
258
259template<> struct KICOMMON_API std::hash<KIID>
260{
261 std::size_t operator()( const KIID& aId ) const
262 {
263 return aId.Hash();
264 }
265};
266
267#endif // KIID_H
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition kiid.h:223
bool operator>(KIID_PATH const &rhs) const
Definition kiid.h:206
bool operator==(KIID_PATH const &rhs) const
Definition kiid.h:175
KIID_PATH()
Definition kiid.h:154
bool operator<(KIID_PATH const &rhs) const
Definition kiid.h:189
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition kiid.h:231
Definition kiid.h:49
KIID()
Definition kiid.cpp:77
static void SeedGenerator(unsigned int aSeed)
Re-initialize the UUID generator with a given seed (for testing or QA purposes)
Definition kiid.cpp:306
size_t Hash() const
Definition kiid.cpp:235
wxString AsString() const
Definition kiid.cpp:247
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:289
boost::uuids::uuid m_uuid
Definition kiid.h:140
void Increment()
Generates a deterministic replacement for a given ID.
Definition kiid.cpp:274
bool operator>(KIID const &rhs) const
Definition kiid.h:134
std::string AsStdString() const
Definition kiid.cpp:253
wxString AsLegacyTimestampString() const
Definition kiid.cpp:259
timestamp_t AsLegacyTimestamp() const
Definition kiid.cpp:222
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition kiid.cpp:300
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition kiid.cpp:179
bool operator!=(KIID const &rhs) const
Definition kiid.h:124
bool operator==(KIID const &rhs) const
Definition kiid.h:119
void Clone(const KIID &aUUID)
Definition kiid.cpp:241
bool IsLegacyTimestamp() const
Definition kiid.cpp:216
bool operator<(KIID const &rhs) const
Definition kiid.h:129
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition kiid.cpp:265
#define KICOMMON_API
Definition kicommon.h:28
KIID niluuid(0)
KICOMMON_API KIID & NilUuid()
Definition kiid.cpp:70
KICOMMON_API void from_json(const nlohmann::json &aJson, KIID &aKIID)
Definition kiid.cpp:385
KICOMMON_API void to_json(nlohmann::json &aJson, const KIID &aKIID)
Definition kiid.cpp:379
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:46
This file contains macros just for swig binding.
#define DECL_VEC_FOR_SWIG(TypeName, MemberType)
Declare a std::vector but no swig template.
Definition macros_swig.h:50
STL namespace.
std::size_t operator()(const KIID &aId) const
Definition kiid.h:261