KiCad PCB EDA Suite
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 (C) 1992-2020 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 <boost/uuid/uuid.hpp>
30#include <macros_swig.h>
31#include <nlohmann/json_fwd.hpp>
32
33#include <string>
34
35class wxString;
36
45typedef uint32_t timestamp_t;
46
47class KIID
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
70 static bool SniffTest( const wxString& aCandidate );
71
77 static void CreateNilUuids( bool aNil = true );
78
88 static void SeedGenerator( unsigned int aSeed );
89
96
103 void Increment();
104
105 bool operator==( KIID const& rhs ) const
106 {
107 return m_uuid == rhs.m_uuid;
108 }
109
110 bool operator!=( KIID const& rhs ) const
111 {
112 return m_uuid != rhs.m_uuid;
113 }
114
115 bool operator<( KIID const& rhs ) const
116 {
117 return m_uuid < rhs.m_uuid;
118 }
119
120 bool operator>( KIID const& rhs ) const
121 {
122 return m_uuid > rhs.m_uuid;
123 }
124
125private:
126 boost::uuids::uuid m_uuid;
127
129};
130
131
132extern KIID niluuid;
133
134KIID& NilUuid();
135
136// declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
137DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
138
139class KIID_PATH : public KIID_VECT_LIST
140{
141public:
143 {
144 }
145
146 KIID_PATH( const wxString& aString );
147
148 bool MakeRelativeTo( const KIID_PATH& aPath );
149
150 wxString AsString() const;
151
152 bool operator==( KIID_PATH const& rhs ) const
153 {
154 if( size() != rhs.size() )
155 return false;
156
157 for( size_t i = 0; i < size(); ++i )
158 {
159 if( at( i ) != rhs.at( i ) )
160 return false;
161 }
162
163 return true;
164 }
165
166 bool operator<( KIID_PATH const& rhs ) const
167 {
168 if( size() != rhs.size() )
169 return size() < rhs.size();
170
171 for( size_t i = 0; i < size(); ++i )
172 {
173 if( at( i ) < rhs.at( i ) )
174 return true;
175
176 if( at( i ) != rhs.at( i ) )
177 return false;
178 }
179
180 return false;
181 }
182};
183
188{
189public:
191 {
192 KIID::CreateNilUuids( true );
193 };
194
196 {
197 KIID::CreateNilUuids( false );
198 }
199};
200
201void to_json( nlohmann::json& aJson, const KIID& aKIID );
202
203void from_json( const nlohmann::json& aJson, KIID& aKIID );
204
205#endif // KIID_H
RAII class to safely set/reset nil KIIDs for use in footprint/symbol loading.
Definition: kiid.h:188
KIID_NIL_SET_RESET()
Definition: kiid.h:190
~KIID_NIL_SET_RESET()
Definition: kiid.h:195
bool operator==(KIID_PATH const &rhs) const
Definition: kiid.h:152
KIID_PATH()
Definition: kiid.h:142
bool operator<(KIID_PATH const &rhs) const
Definition: kiid.h:166
Definition: kiid.h:48
timestamp_t m_cached_timestamp
Definition: kiid.h:128
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:294
size_t Hash() const
Definition: kiid.cpp:230
wxString AsString() const
Definition: kiid.cpp:251
boost::uuids::uuid m_uuid
Definition: kiid.h:126
void Increment()
Generates a deterministic replacement for a given ID.
Definition: kiid.cpp:273
bool operator>(KIID const &rhs) const
Definition: kiid.h:120
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:257
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:224
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition: kiid.cpp:288
static bool SniffTest(const wxString &aCandidate)
Returns true if a string has the correct formatting to be a KIID.
Definition: kiid.cpp:173
bool operator!=(KIID const &rhs) const
Definition: kiid.h:110
bool operator==(KIID const &rhs) const
Definition: kiid.h:105
void Clone(const KIID &aUUID)
Definition: kiid.cpp:244
bool IsLegacyTimestamp() const
Definition: kiid.cpp:218
bool operator<(KIID const &rhs) const
Definition: kiid.h:115
void ConvertTimestampToUuid()
Change an existing time stamp based UUID into a true UUID.
Definition: kiid.cpp:263
nlohmann::json json
Definition: gerbview.cpp:44
KIID & NilUuid()
Definition: kiid.cpp:67
KIID niluuid
void from_json(const nlohmann::json &aJson, KIID &aKIID)
Definition: kiid.cpp:349
void to_json(nlohmann::json &aJson, const KIID &aKIID)
Definition: kiid.cpp:343
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
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
_OUT_STRING AsString(const std::string &aString)
Definition: sexpr.h:131