KiCad PCB EDA Suite
KIID Class Reference

#include <kiid.h>

Public Member Functions

 KIID ()
 
 KIID (int null)
 
 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 44 of file kiid.h.

Constructor & Destructor Documentation

◆ KIID() [1/4]

KIID::KIID ( )

Definition at line 68 of file kiid.cpp.

69 {
71 
72 #if BOOST_VERSION >= 106700
73  try
74  {
75 #endif
76 
77  if( g_createNilUuids )
78  {
79  m_uuid = nilGenerator();
80  }
81  else
82  {
83  std::lock_guard<std::mutex> lock( rng_mutex );
85  }
86 
87 #if BOOST_VERSION >= 106700
88  }
89  catch( const boost::uuids::entropy_error& )
90  {
91  wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
92  __FILE__, __FUNCTION__ );
93  }
94 #endif
95 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
static boost::uuids::basic_random_generator< boost::mt19937 > randomGenerator(rng)
timestamp_t m_cached_timestamp
Definition: kiid.h:123
static boost::uuids::nil_generator nilGenerator
Definition: kiid.cpp:49
static bool g_createNilUuids
Definition: kiid.cpp:57
static std::mutex rng_mutex
Definition: kiid.cpp:41

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

◆ KIID() [2/4]

KIID::KIID ( int  null)

Definition at line 98 of file kiid.cpp.

99 {
100  wxASSERT( null == 0 );
101 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
timestamp_t m_cached_timestamp
Definition: kiid.h:123
static boost::uuids::nil_generator nilGenerator
Definition: kiid.cpp:49

◆ KIID() [3/4]

KIID::KIID ( const wxString &  aString)

Definition at line 104 of file kiid.cpp.

104  : m_uuid(), m_cached_timestamp( 0 )
105 {
106  if( aString.length() == 8 )
107  {
108  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
109  // Convert them individually to avoid stepping in the little-endian/big-endian
110  // doo-doo.
111  for( int i = 0; i < 4; ++i )
112  {
113  wxString octet = aString.substr( i * 2, 2 );
114  m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
115  }
116 
117  m_cached_timestamp = strtol( aString.c_str(), nullptr, 16 );
118  }
119  else
120  {
121  try
122  {
123  m_uuid = stringGenerator( aString.wc_str() );
124 
125  if( IsLegacyTimestamp() )
126  m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), nullptr, 16 );
127  }
128  catch( ... )
129  {
130  // Failed to parse string representation; best we can do is assign a new
131  // random one.
132 #if BOOST_VERSION >= 106700
133  try
134  {
135 #endif
136 
138 
139 #if BOOST_VERSION >= 106700
140  }
141  catch( const boost::uuids::entropy_error& )
142  {
143  wxLogFatalError( "A Boost UUID entropy exception was thrown in %s:%s.",
144  __FILE__, __FUNCTION__ );
145  }
146 #endif
147  }
148  }
149 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
static boost::uuids::basic_random_generator< boost::mt19937 > randomGenerator(rng)
bool IsLegacyTimestamp() const
Definition: kiid.cpp:197
timestamp_t m_cached_timestamp
Definition: kiid.h:123
static boost::uuids::string_generator stringGenerator
Definition: kiid.cpp:48

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

◆ KIID() [4/4]

KIID::KIID ( timestamp_t  aTimestamp)

Definition at line 180 of file kiid.cpp.

181 {
182  m_cached_timestamp = aTimestamp;
183 
184  // A legacy-timestamp-based UUID has only the last 4 octets filled in.
185  // Convert them individually to avoid stepping in the little-endian/big-endian
186  // doo-doo.
187  wxString str = AsLegacyTimestampString();
188 
189  for( int i = 0; i < 4; ++i )
190  {
191  wxString octet = str.substr( i * 2, 2 );
192  m_uuid.data[i + 12] = strtol( octet.data(), nullptr, 16 );
193  }
194 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
timestamp_t m_cached_timestamp
Definition: kiid.h:123
wxString AsLegacyTimestampString() const
Definition: kiid.cpp:236

References AsLegacyTimestampString(), m_cached_timestamp, and m_uuid.

Member Function Documentation

◆ AsLegacyTimestamp()

timestamp_t KIID::AsLegacyTimestamp ( ) const

Definition at line 203 of file kiid.cpp.

204 {
205  return m_cached_timestamp;
206 }
timestamp_t m_cached_timestamp
Definition: kiid.h:123

References m_cached_timestamp.

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

◆ AsLegacyTimestampString()

wxString KIID::AsLegacyTimestampString ( ) const

Definition at line 236 of file kiid.cpp.

237 {
238  return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() );
239 }
timestamp_t AsLegacyTimestamp() const
Definition: kiid.cpp:203
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 223 of file kiid.cpp.

224 {
225  m_uuid = aUUID.m_uuid;
227 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
timestamp_t m_cached_timestamp
Definition: kiid.h:123

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 242 of file kiid.cpp.

243 {
244  if( !IsLegacyTimestamp() )
245  return;
246 
247  m_cached_timestamp = 0;
249 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
static boost::uuids::basic_random_generator< boost::mt19937 > randomGenerator(rng)
bool IsLegacyTimestamp() const
Definition: kiid.cpp:197
timestamp_t m_cached_timestamp
Definition: kiid.h:123

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 267 of file kiid.cpp.

268 {
269  g_createNilUuids = aNil;
270 }
static bool g_createNilUuids
Definition: kiid.cpp:57

References g_createNilUuids.

Referenced by FOOTPRINT_ASYNC_LOADER::Abort(), SYMBOL_TREE_MODEL_ADAPTER::AddLibraries(), and FOOTPRINT_ASYNC_LOADER::Start().

◆ Hash()

size_t KIID::Hash ( ) const

Definition at line 209 of file kiid.cpp.

210 {
211  size_t hash = 0;
212 
213  // Note: this is NOT little-endian/big-endian safe, but as long as it's just used
214  // at runtime it won't matter.
215 
216  for( int i = 0; i < 4; ++i )
217  boost::hash_combine( hash, reinterpret_cast<const uint32_t*>( m_uuid.data )[i] );
218 
219  return hash;
220 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121
static void hash_combine(std::size_t &seed)
This is a dummy function to take the final case of hash_combine below.
Definition: hash_eda.h:67

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 252 of file kiid.cpp.

253 {
254  // This obviously destroys uniform distribution, but it can be useful when a
255  // deterministic replacement for a duplicate ID is required.
256 
257  for( int i = 15; i >= 0; --i )
258  {
259  m_uuid.data[i]++;
260 
261  if( m_uuid.data[i] != 0 )
262  break;
263  }
264 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

References m_uuid.

◆ IsLegacyTimestamp()

bool KIID::IsLegacyTimestamp ( ) const

Definition at line 197 of file kiid.cpp.

198 {
199  return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11];
200 }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

References m_uuid.

Referenced by ConvertTimestampToUuid(), and KIID().

◆ 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  }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

References m_uuid.

◆ 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  }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

References m_uuid.

◆ operator==()

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

Definition at line 100 of file kiid.h.

101  {
102  return m_uuid == rhs.m_uuid;
103  }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

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  }
boost::uuids::uuid m_uuid
Definition: kiid.h:121

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 273 of file kiid.cpp.

274 {
275  rng.seed( aSeed );
276 }
static boost::mt19937 rng
Definition: kiid.cpp:44

References 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 152 of file kiid.cpp.

153 {
154  static wxString niluuidStr = niluuid.AsString();
155 
156  if( aCandidate.Length() != niluuidStr.Length() )
157  return false;
158 
159  for( wxChar c : aCandidate )
160  {
161  if( c >= '0' && c <= '9' )
162  continue;
163 
164  if( c >= 'a' && c <= 'f' )
165  continue;
166 
167  if( c >= 'A' && c <= 'F' )
168  continue;
169 
170  if( c == '-' )
171  continue;
172 
173  return false;
174  }
175 
176  return true;
177 }
KIID niluuid(0)
wxString AsString() const
Definition: kiid.cpp:230

References AsString(), and niluuid.

Referenced by insideArea().

Member Data Documentation

◆ m_cached_timestamp

timestamp_t KIID::m_cached_timestamp
private

Definition at line 123 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: