KiCad PCB EDA Suite
KIID Class Reference

#include <kiid.h>

Public Member Functions

 KIID ()
 
 KIID (int null)
 
 KIID (const std::string &aString)
 
 KIID (const char *aString)
 
 KIID (const wxString &aString)
 
 KIID (timestamp_t aTimestamp)
 
void Clone (const KIID &aUUID)
 
size_t Hash () const
 
bool IsLegacyTimestamp () const
 
timestamp_t AsLegacyTimestamp () const
 
wxString AsString () const
 
wxString AsLegacyTimestampString () const
 
void ConvertTimestampToUuid ()
 Change an existing time stamp based UUID into a true UUID. More...
 
void Increment ()
 Generates a deterministic replacement for a given ID. More...
 
bool operator== (KIID const &rhs) const
 
bool operator!= (KIID const &rhs) const
 
bool operator< (KIID const &rhs) const
 
bool operator> (KIID const &rhs) const
 

Static Public Member Functions

static bool SniffTest (const wxString &aCandidate)
 Returns true if a string has the correct formatting to be a KIID. More...
 
static void CreateNilUuids (bool aNil=true)
 A performance optimization which disables/enables the generation of pseudo-random UUIDs. More...
 
static void SeedGenerator (unsigned int aSeed)
 Re-initialize the UUID generator with a given seed (for testing or QA purposes) More...
 

Private Attributes

boost::uuids::uuid m_uuid
 
timestamp_t m_cached_timestamp
 

Detailed Description

Definition at line 47 of file kiid.h.

Constructor & Destructor Documentation

◆ KIID() [1/6]

KIID::KIID ( )

Definition at line 74 of file kiid.cpp.

75{
77
78#if BOOST_VERSION >= 106700
79 try
80 {
81#endif
82
84 {
86 }
87 else
88 {
89 std::lock_guard<std::mutex> lock( rng_mutex );
91 }
92
93#if BOOST_VERSION >= 106700
94 }
95 catch( const boost::uuids::entropy_error& )
96 {
97 wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
98 __FILE__, __FUNCTION__ );
99 }
100#endif
101}
timestamp_t m_cached_timestamp
Definition: kiid.h:128
boost::uuids::uuid m_uuid
Definition: kiid.h:126
static boost::uuids::nil_generator nilGenerator
Definition: kiid.cpp:55
static bool g_createNilUuids
Definition: kiid.cpp:63
static std::mutex rng_mutex
Definition: kiid.cpp:44
static boost::uuids::basic_random_generator< boost::mt19937 > randomGenerator
Definition: kiid.cpp:51

References g_createNilUuids, m_cached_timestamp, m_uuid, nilGenerator, randomGenerator, and rng_mutex.

◆ KIID() [2/6]

KIID::KIID ( int  null)

Definition at line 104 of file kiid.cpp.

105{
106 wxASSERT( null == 0 );
107}

◆ KIID() [3/6]

KIID::KIID ( const std::string &  aString)

Definition at line 110 of file kiid.cpp.

110 : m_uuid(), m_cached_timestamp( 0 )
111{
112 if( aString.length() == 8
113 && std::all_of( aString.begin(), aString.end(),
114 []( unsigned char c )
115 {
116 return std::isxdigit( c );
117 } ) )
118 {
119 // A legacy-timestamp-based UUID has only the last 4 octets filled in.
120 // Convert them individually to avoid stepping in the little-endian/big-endian
121 // doo-doo.
122 for( int i = 0; i < 4; ++i )
123 {
124 std::string octet = aString.substr( i * 2, 2 );
125 m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
126 }
127
128 m_cached_timestamp = strtol( aString.c_str(), nullptr, 16 );
129 }
130 else
131 {
132 try
133 {
134 m_uuid = stringGenerator( aString );
135
136 if( IsLegacyTimestamp() )
137 m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), nullptr, 16 );
138 }
139 catch( ... )
140 {
141 // Failed to parse string representation; best we can do is assign a new
142 // random one.
143#if BOOST_VERSION >= 106700
144 try
145 {
146#endif
147
149
150#if BOOST_VERSION >= 106700
151 }
152 catch( const boost::uuids::entropy_error& )
153 {
154 wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
155 __FILE__, __FUNCTION__ );
156 }
157#endif
158 }
159 }
160}
bool IsLegacyTimestamp() const
Definition: kiid.cpp:218
static boost::uuids::string_generator stringGenerator
Definition: kiid.cpp:54

References IsLegacyTimestamp(), m_cached_timestamp, m_uuid, randomGenerator, and stringGenerator.

◆ KIID() [4/6]

KIID::KIID ( const char *  aString)

Definition at line 163 of file kiid.cpp.

163 : KIID( std::string( aString ) )
164{
165}
KIID()
Definition: kiid.cpp:74

◆ KIID() [5/6]

KIID::KIID ( const wxString &  aString)

Definition at line 168 of file kiid.cpp.

168 : KIID( std::string( aString.ToUTF8() ) )
169{
170}

◆ KIID() [6/6]

KIID::KIID ( timestamp_t  aTimestamp)

Definition at line 201 of file kiid.cpp.

202{
203 m_cached_timestamp = aTimestamp;
204
205 // A legacy-timestamp-based UUID has only the last 4 octets filled in.
206 // Convert them individually to avoid stepping in the little-endian/big-endian
207 // doo-doo.
208 wxString str = AsLegacyTimestampString();
209
210 for( int i = 0; i < 4; ++i )
211 {
212 wxString octet = str.substr( i * 2, 2 );
213 m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
214 }
215}
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:257

References AsLegacyTimestampString(), m_cached_timestamp, and m_uuid.

Member Function Documentation

◆ AsLegacyTimestamp()

timestamp_t KIID::AsLegacyTimestamp ( ) const

Definition at line 224 of file kiid.cpp.

225{
226 return m_cached_timestamp;
227}

References m_cached_timestamp.

Referenced by AsLegacyTimestampString(), SCH_LEGACY_PLUGIN::saveSheet(), and SCH_LEGACY_PLUGIN::saveSymbol().

◆ AsLegacyTimestampString()

wxString KIID::AsLegacyTimestampString ( ) const

Definition at line 257 of file kiid.cpp.

258{
259 return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
260}
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:224
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200

References AsLegacyTimestamp(), and Format().

Referenced by KIID(), and SCH_LEGACY_PLUGIN::saveSymbol().

◆ AsString()

◆ Clone()

void KIID::Clone ( const KIID aUUID)

Definition at line 244 of file kiid.cpp.

245{
246 m_uuid = aUUID.m_uuid;
248}

References m_cached_timestamp, and m_uuid.

◆ ConvertTimestampToUuid()

void KIID::ConvertTimestampToUuid ( )

Change an existing time stamp based UUID into a true UUID.

If this is not a time stamp based UUID, then no change is made.

Definition at line 263 of file kiid.cpp.

264{
265 if( !IsLegacyTimestamp() )
266 return;
267
270}

References IsLegacyTimestamp(), m_cached_timestamp, m_uuid, and randomGenerator.

◆ CreateNilUuids()

void KIID::CreateNilUuids ( bool  aNil = true)
static

A performance optimization which disables/enables the generation of pseudo-random UUIDs.

NB: uses a global. Not thread safe!

Definition at line 288 of file kiid.cpp.

289{
290 g_createNilUuids = aNil;
291}

References g_createNilUuids.

Referenced by SYMBOL_TREE_MODEL_ADAPTER::AddLibraries(), KIID_NIL_SET_RESET::KIID_NIL_SET_RESET(), and KIID_NIL_SET_RESET::~KIID_NIL_SET_RESET().

◆ Hash()

size_t KIID::Hash ( ) const

Definition at line 230 of file kiid.cpp.

231{
232 size_t hash = 0;
233
234 // Note: this is NOT little-endian/big-endian safe, but as long as it's just used
235 // at runtime it won't matter.
236
237 for( int i = 0; i < 4; ++i )
238 boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
239
240 return hash;
241}
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash.h:34

References hash_combine(), and m_uuid.

Referenced by BOOST_AUTO_TEST_CASE().

◆ Increment()

void KIID::Increment ( )

Generates a deterministic replacement for a given ID.

NB: destroys uniform distribution! But it's the only thing we have when a deterministic replacement for a duplicate ID is required.

Definition at line 273 of file kiid.cpp.

274{
275 // This obviously destroys uniform distribution, but it can be useful when a
276 // deterministic replacement for a duplicate ID is required.
277
278 for( int i = 15; i >= 0; --i )
279 {
280 m_uuid.data[i]++;
281
282 if( m_uuid.data[i] != 0 )
283 break;
284 }
285}

References m_uuid.

◆ IsLegacyTimestamp()

bool KIID::IsLegacyTimestamp ( ) const

Definition at line 218 of file kiid.cpp.

219{
220 return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
221}

References m_uuid.

Referenced by ConvertTimestampToUuid(), and KIID().

◆ operator!=()

bool KIID::operator!= ( KIID const &  rhs) const
inline

Definition at line 110 of file kiid.h.

111 {
112 return m_uuid != rhs.m_uuid;
113 }

References m_uuid.

◆ operator<()

bool KIID::operator< ( KIID const &  rhs) const
inline

Definition at line 115 of file kiid.h.

116 {
117 return m_uuid < rhs.m_uuid;
118 }

References m_uuid.

◆ operator==()

bool KIID::operator== ( KIID const &  rhs) const
inline

Definition at line 105 of file kiid.h.

106 {
107 return m_uuid == rhs.m_uuid;
108 }

References m_uuid.

◆ operator>()

bool KIID::operator> ( KIID const &  rhs) const
inline

Definition at line 120 of file kiid.h.

121 {
122 return m_uuid > rhs.m_uuid;
123 }

References m_uuid.

◆ SeedGenerator()

void KIID::SeedGenerator ( unsigned int  aSeed)
static

Re-initialize the UUID generator with a given seed (for testing or QA purposes)

WARNING: Do not call this function from within KiCad or via a Python action plugin. It is only to be used inside QA tests or in external Python scripts. Resetting the UUID generator in the middle of a KiCad GUI run will potentially have harmful effects on file integrity.

Parameters
aSeedis a seed to pass to the boost::mt19937 pseudo-random number generator

Definition at line 294 of file kiid.cpp.

295{
296 rng.seed( aSeed );
297 randomGenerator = boost::uuids::basic_random_generator<boost::mt19937>( rng );
298}
static boost::mt19937 rng
Definition: kiid.cpp:50

References randomGenerator, and rng.

Referenced by BOOST_AUTO_TEST_CASE().

◆ SniffTest()

bool KIID::SniffTest ( const wxString &  aCandidate)
static

Returns true if a string has the correct formatting to be a KIID.

Definition at line 173 of file kiid.cpp.

174{
175 static wxString niluuidStr = niluuid.AsString();
176
177 if( aCandidate.Length() != niluuidStr.Length() )
178 return false;
179
180 for( wxChar c : aCandidate )
181 {
182 if( c >= '0' && c <= '9' )
183 continue;
184
185 if( c >= 'a' && c <= 'f' )
186 continue;
187
188 if( c >= 'A' && c <= 'F' )
189 continue;
190
191 if( c == '-' )
192 continue;
193
194 return false;
195 }
196
197 return true;
198}
wxString AsString() const
Definition: kiid.cpp:251
KIID niluuid(0)

References AsString(), and niluuid.

Referenced by searchAreas().

Member Data Documentation

◆ m_cached_timestamp

timestamp_t KIID::m_cached_timestamp
private

Definition at line 128 of file kiid.h.

Referenced by AsLegacyTimestamp(), Clone(), ConvertTimestampToUuid(), and KIID().

◆ m_uuid

boost::uuids::uuid KIID::m_uuid
private

The documentation for this class was generated from the following files: