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 (C) 1992-2023 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;
63 timestamp_t AsLegacyTimestamp() 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
97 void ConvertTimestampToUuid();
98
105 void Increment();
106
107 bool operator==( KIID const& rhs ) const
108 {
109 return m_uuid == rhs.m_uuid;
110 }
111
112 bool operator!=( KIID const& rhs ) const
113 {
114 return m_uuid != rhs.m_uuid;
115 }
116
117 bool operator<( KIID const& rhs ) const
118 {
119 return m_uuid < rhs.m_uuid;
120 }
121
122 bool operator>( KIID const& rhs ) const
123 {
124 return m_uuid > rhs.m_uuid;
125 }
126
127private:
128 boost::uuids::uuid m_uuid;
129
131};
132
133
135
137
138// declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
139DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
140
141class KICOMMON_API KIID_PATH : public KIID_VECT_LIST
142{
143public:
145 {
146 }
147
148 KIID_PATH( const wxString& aString );
149
150 bool MakeRelativeTo( const KIID_PATH& aPath );
151
161 bool EndsWith( const KIID_PATH& aPath ) const;
162
163 wxString AsString() const;
164
165 bool operator==( KIID_PATH const& rhs ) const
166 {
167 if( size() != rhs.size() )
168 return false;
169
170 for( size_t i = 0; i < size(); ++i )
171 {
172 if( at( i ) != rhs.at( i ) )
173 return false;
174 }
175
176 return true;
177 }
178
179 bool operator<( KIID_PATH const& rhs ) const
180 {
181 if( size() != rhs.size() )
182 return size() < rhs.size();
183
184 for( size_t i = 0; i < size(); ++i )
185 {
186 if( at( i ) < rhs.at( i ) )
187 return true;
188
189 if( at( i ) != rhs.at( i ) )
190 return false;
191 }
192
193 return false;
194 }
195
197 {
198 for( const KIID& kiid : aRhs )
199 emplace_back( kiid );
200
201 return *this;
202 }
203
204 friend KIID_PATH operator+( KIID_PATH aLhs, const KIID_PATH& aRhs )
205 {
206 aLhs += aRhs;
207 return aLhs;
208 }
209};
210
215{
216public:
218 {
219 KIID::CreateNilUuids( true );
220 };
221
223 {
224 KIID::CreateNilUuids( false );
225 }
226};
227
228KICOMMON_API void to_json( nlohmann::json& aJson, const KIID& aKIID );
229
230KICOMMON_API void from_json( const nlohmann::json& aJson, KIID& aKIID );
231
232template<> struct KICOMMON_API std::hash<KIID>
233{
234 std::size_t operator()( const KIID& aId ) const
235 {
236 return aId.Hash();
237 }
238};
239
240#endif // KIID_H
RAII class to safely set/reset nil KIIDs for use in footprint/symbol loading.
Definition: kiid.h:215
KIID_NIL_SET_RESET()
Definition: kiid.h:217
~KIID_NIL_SET_RESET()
Definition: kiid.h:222
KIID_PATH & operator+=(const KIID_PATH &aRhs)
Definition: kiid.h:196
bool operator==(KIID_PATH const &rhs) const
Definition: kiid.h:165
KIID_PATH()
Definition: kiid.h:144
bool operator<(KIID_PATH const &rhs) const
Definition: kiid.h:179
friend KIID_PATH operator+(KIID_PATH aLhs, const KIID_PATH &aRhs)
Definition: kiid.h:204
Definition: kiid.h:49
timestamp_t m_cached_timestamp
Definition: kiid.h:130
size_t Hash() const
Definition: kiid.cpp:236
boost::uuids::uuid m_uuid
Definition: kiid.h:128
bool operator>(KIID const &rhs) const
Definition: kiid.h:122
static void CreateNilUuids(bool aNil=true)
A performance optimization which disables/enables the generation of pseudo-random UUIDs.
Definition: kiid.cpp:300
bool operator!=(KIID const &rhs) const
Definition: kiid.h:112
bool operator==(KIID const &rhs) const
Definition: kiid.h:107
bool operator<(KIID const &rhs) const
Definition: kiid.h:117
#define KICOMMON_API
Definition: kicommon.h:28
KICOMMON_API KIID & NilUuid()
Definition: kiid.cpp:67
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
KICOMMON_API KIID niluuid
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:234