You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.7 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2020 Ian McInerney <ian.s.mcinerney@ieee.org>
  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. #ifndef KIID_H
  26. #define KIID_H
  27. #include <boost/uuid/uuid.hpp>
  28. #include <macros_swig.h>
  29. class wxString;
  30. /**
  31. * timestamp_t is our type to represent unique IDs for all kinds of elements;
  32. * historically simply the timestamp when they were created.
  33. *
  34. * Long term, this type might be renamed to something like unique_id_t
  35. * (and then rename all the methods from {Get,Set}TimeStamp()
  36. * to {Get,Set}Id()) ?
  37. */
  38. typedef uint32_t timestamp_t;
  39. class KIID
  40. {
  41. public:
  42. KIID();
  43. KIID( int null );
  44. KIID( const wxString& aString );
  45. KIID( timestamp_t aTimestamp );
  46. void Clone( const KIID& aUUID );
  47. size_t Hash() const;
  48. bool IsLegacyTimestamp() const;
  49. timestamp_t AsLegacyTimestamp() const;
  50. wxString AsString() const;
  51. wxString AsLegacyTimestampString() const;
  52. /**
  53. * Returns true if a string has the correct formatting to be a KIID.
  54. */
  55. static bool SniffTest( const wxString& aCandidate );
  56. /**
  57. * A performance optimization which disables/enables the generation of pseudo-random UUIDs.
  58. *
  59. * NB: uses a global. Not thread safe!
  60. */
  61. static void CreateNilUuids( bool aNil = true );
  62. /**
  63. * Re-initialize the UUID generator with a given seed (for testing or QA purposes)
  64. *
  65. * WARNING: Do not call this function from within KiCad or via a Python action plugin. It is
  66. * only to be used inside QA tests or in external Python scripts. Resetting the UUID generator
  67. * in the middle of a KiCad GUI run will potentially have harmful effects on file integrity.
  68. *
  69. * @param aSeed is a seed to pass to the boost::mt19937 pseudo-random number generator
  70. */
  71. static void SeedGenerator( unsigned int aSeed );
  72. /**
  73. * Change an existing time stamp based UUID into a true UUID.
  74. *
  75. * If this is not a time stamp based UUID, then no change is made.
  76. */
  77. void ConvertTimestampToUuid();
  78. /**
  79. * Generates a deterministic replacement for a given ID.
  80. *
  81. * NB: destroys uniform distribution! But it's the only thing we have when a deterministic
  82. * replacement for a duplicate ID is required.
  83. */
  84. void Increment();
  85. bool operator==( KIID const& rhs ) const
  86. {
  87. return m_uuid == rhs.m_uuid;
  88. }
  89. bool operator!=( KIID const& rhs ) const
  90. {
  91. return m_uuid != rhs.m_uuid;
  92. }
  93. bool operator<( KIID const& rhs ) const
  94. {
  95. return m_uuid < rhs.m_uuid;
  96. }
  97. bool operator>( KIID const& rhs ) const
  98. {
  99. return m_uuid > rhs.m_uuid;
  100. }
  101. private:
  102. boost::uuids::uuid m_uuid;
  103. timestamp_t m_cached_timestamp;
  104. };
  105. extern KIID niluuid;
  106. KIID& NilUuid();
  107. // declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
  108. DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
  109. class KIID_PATH : public KIID_VECT_LIST
  110. {
  111. public:
  112. KIID_PATH()
  113. {
  114. }
  115. KIID_PATH( const wxString& aString );
  116. bool MakeRelativeTo( const KIID_PATH& aPath );
  117. wxString AsString() const;
  118. bool operator==( KIID_PATH const& rhs ) const
  119. {
  120. if( size() != rhs.size() )
  121. return false;
  122. for( size_t i = 0; i < size(); ++i )
  123. {
  124. if( at( i ) != rhs.at( i ) )
  125. return false;
  126. }
  127. return true;
  128. }
  129. bool operator<( KIID_PATH const& rhs ) const
  130. {
  131. if( size() != rhs.size() )
  132. return size() < rhs.size();
  133. for( size_t i = 0; i < size(); ++i )
  134. {
  135. if( at( i ) < rhs.at( i ) )
  136. return true;
  137. if( at( i ) != rhs.at( i ) )
  138. return false;
  139. }
  140. return false;
  141. }
  142. };
  143. #endif // KIID_H