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.

152 lines
3.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. static bool SniffTest( const wxString& aCandidate );
  53. static void CreateNilUuids( bool aNil = true );
  54. /**
  55. * Change an existing time stamp based UUID into a true UUID.
  56. *
  57. * If this is not a time stamp based UUID, then no change is made.
  58. */
  59. void ConvertTimestampToUuid();
  60. bool operator==( KIID const& rhs ) const
  61. {
  62. return m_uuid == rhs.m_uuid;
  63. }
  64. bool operator!=( KIID const& rhs ) const
  65. {
  66. return m_uuid != rhs.m_uuid;
  67. }
  68. bool operator<( KIID const& rhs ) const
  69. {
  70. return m_uuid < rhs.m_uuid;
  71. }
  72. bool operator>( KIID const& rhs ) const
  73. {
  74. return m_uuid > rhs.m_uuid;
  75. }
  76. private:
  77. boost::uuids::uuid m_uuid;
  78. timestamp_t m_cached_timestamp;
  79. };
  80. extern KIID niluuid;
  81. KIID& NilUuid();
  82. // declare KIID_VECT_LIST as std::vector<KIID> both for c++ and swig:
  83. DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID )
  84. class KIID_PATH : public KIID_VECT_LIST
  85. {
  86. public:
  87. KIID_PATH()
  88. {
  89. }
  90. KIID_PATH( const wxString& aString );
  91. bool MakeRelativeTo( const KIID_PATH& aPath );
  92. wxString AsString() const;
  93. bool operator==( KIID_PATH const& rhs ) const
  94. {
  95. if( size() != rhs.size() )
  96. return false;
  97. for( size_t i = 0; i < size(); ++i )
  98. {
  99. if( at( i ) != rhs.at( i ) )
  100. return false;
  101. }
  102. return true;
  103. }
  104. bool operator<( KIID_PATH const& rhs ) const
  105. {
  106. if( size() != rhs.size() )
  107. return size() < rhs.size();
  108. for( size_t i = 0; i < size(); ++i )
  109. {
  110. if( at( i ) < rhs.at( i ) )
  111. return true;
  112. if( at( i ) != rhs.at( i ) )
  113. return false;
  114. }
  115. return false;
  116. }
  117. };
  118. #endif // KIID_H