diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 50837cb6f0..37915adcf9 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -343,6 +343,7 @@ set( COMMON_SRCS hotkeys_basic.cpp html_messagebox.cpp kiface_i.cpp + kiid.cpp kiway.cpp kiway_express.cpp kiway_holder.cpp diff --git a/common/common.cpp b/common/common.cpp index b7b5ba52a0..1fb2a9a1e0 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -35,176 +35,6 @@ #include #include #include -#include -#include -#include - - -// Create only once, as seeding is *very* expensive -static boost::uuids::random_generator randomGenerator; - -// These don't have the same performance penalty, but might as well be consistent -static boost::uuids::string_generator stringGenerator; -static boost::uuids::nil_generator nilGenerator; - -// Global nil reference -KIID niluuid( 0 ); - - -// For static initialization -KIID& NilUuid() -{ - static KIID nil( 0 ); - return nil; -} - - -KIID::KIID() : - m_uuid( randomGenerator() ), - m_cached_timestamp( 0 ) -{ -} - - -KIID::KIID( int null ) : - m_uuid( nilGenerator() ), - m_cached_timestamp( 0 ) -{ - wxASSERT( null == 0 ); -} - - -KIID::KIID( const wxString& aString ) : - m_uuid(), - m_cached_timestamp( 0 ) -{ - if( aString.length() == 8 ) - { - // A legacy-timestamp-based UUID has only the last 4 octets filled in. - // Convert them individually to avoid stepping in the little-endian/big-endian - // doo-doo. - for( int i = 0; i < 4; ++i ) - { - wxString octet = aString.substr( i * 2, 2 ); - m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 ); - } - - m_cached_timestamp = strtol( aString.c_str(), NULL, 16 ); - } - else - { - try - { - m_uuid = stringGenerator( aString.wc_str() ); - - if( IsLegacyTimestamp() ) - m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), NULL, 16 ); - } - catch( ... ) - { - // Failed to parse string representation; best we can do is assign a new - // random one. - m_uuid = randomGenerator(); - } - } -} - - -bool KIID::SniffTest( const wxString& aCandidate ) -{ - static wxString niluuidStr = niluuid.AsString(); - - if( aCandidate.Length() != niluuidStr.Length() ) - return false; - - for( wxChar c : aCandidate ) - { - if( c >= '0' && c <= '9' ) - continue; - - if( c >= 'a' && c <= 'f' ) - continue; - - if( c >= 'A' && c <= 'F' ) - continue; - - return false; - } - - return true; -} - - -KIID::KIID( timestamp_t aTimestamp ) -{ - m_cached_timestamp = aTimestamp; - - // A legacy-timestamp-based UUID has only the last 4 octets filled in. - // Convert them individually to avoid stepping in the little-endian/big-endian - // doo-doo. - wxString str = AsLegacyTimestampString(); - - for( int i = 0; i < 4; ++i ) - { - wxString octet = str.substr( i * 2, 2 ); - m_uuid.data[ i + 12 ] = strtol( octet.data(), NULL, 16 ); - } -} - - -bool KIID::IsLegacyTimestamp() const -{ - return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11]; -} - - -timestamp_t KIID::AsLegacyTimestamp() const -{ - return m_cached_timestamp; -} - - -size_t KIID::Hash() const -{ - size_t hash = 0; - - // Note: this is NOT little-endian/big-endian safe, but as long as it's just used - // at runtime it won't matter. - - for( int i = 0; i < 4; ++i ) - boost::hash_combine( hash, reinterpret_cast( m_uuid.data )[i] ); - - return hash; -} - - -void KIID::Clone( const KIID& aUUID ) -{ - m_uuid = aUUID.m_uuid; - m_cached_timestamp = aUUID.m_cached_timestamp; -} - - -wxString KIID::AsString() const -{ - return boost::uuids::to_string( m_uuid ); -} - - -wxString KIID::AsLegacyTimestampString() const -{ - return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() ); -} - - -void KIID::ConvertTimestampToUuid() -{ - if( !IsLegacyTimestamp() ) - return; - - m_cached_timestamp = 0; - m_uuid = randomGenerator(); -} bool IsImperialUnit( EDA_UNITS aUnit ) diff --git a/common/eda_doc.cpp b/common/eda_doc.cpp index aeca824911..cdc3e8d851 100644 --- a/common/eda_doc.cpp +++ b/common/eda_doc.cpp @@ -27,6 +27,7 @@ */ #include +#include #include #include #include diff --git a/common/gestfich.cpp b/common/gestfich.cpp index a3a9f7730f..ddcc6f9155 100644 --- a/common/gestfich.cpp +++ b/common/gestfich.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include diff --git a/common/kiid.cpp b/common/kiid.cpp new file mode 100644 index 0000000000..1334484c20 --- /dev/null +++ b/common/kiid.cpp @@ -0,0 +1,211 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr + * Copyright (C) 1992-2020 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include // AsLegacyTimestampString, AsString +#include + +#include +#include +#include + +// Create only once, as seeding is *very* expensive +static boost::uuids::random_generator randomGenerator; + +// These don't have the same performance penalty, but might as well be consistent +static boost::uuids::string_generator stringGenerator; +static boost::uuids::nil_generator nilGenerator; + +// Global nil reference +KIID niluuid( 0 ); + +// For static initialization +KIID& NilUuid() +{ + static KIID nil( 0 ); + return nil; +} + + +KIID::KIID() : m_uuid( randomGenerator() ), m_cached_timestamp( 0 ) +{ +} + + +KIID::KIID( int null ) : m_uuid( nilGenerator() ), m_cached_timestamp( 0 ) +{ + wxASSERT( null == 0 ); +} + + +KIID::KIID( const wxString& aString ) : m_uuid(), m_cached_timestamp( 0 ) +{ + if( aString.length() == 8 ) + { + // A legacy-timestamp-based UUID has only the last 4 octets filled in. + // Convert them individually to avoid stepping in the little-endian/big-endian + // doo-doo. + for( int i = 0; i < 4; ++i ) + { + wxString octet = aString.substr( i * 2, 2 ); + m_uuid.data[i + 12] = strtol( octet.data(), NULL, 16 ); + } + + m_cached_timestamp = strtol( aString.c_str(), NULL, 16 ); + } + else + { + try + { + m_uuid = stringGenerator( aString.wc_str() ); + + if( IsLegacyTimestamp() ) + m_cached_timestamp = strtol( aString.substr( 28 ).c_str(), NULL, 16 ); + } + catch( ... ) + { + // Failed to parse string representation; best we can do is assign a new + // random one. + m_uuid = randomGenerator(); + } + } +} + + +bool KIID::SniffTest( const wxString& aCandidate ) +{ + static wxString niluuidStr = niluuid.AsString(); + + if( aCandidate.Length() != niluuidStr.Length() ) + return false; + + for( wxChar c : aCandidate ) + { + if( c >= '0' && c <= '9' ) + continue; + + if( c >= 'a' && c <= 'f' ) + continue; + + if( c >= 'A' && c <= 'F' ) + continue; + + return false; + } + + return true; +} + + +KIID::KIID( timestamp_t aTimestamp ) +{ + m_cached_timestamp = aTimestamp; + + // A legacy-timestamp-based UUID has only the last 4 octets filled in. + // Convert them individually to avoid stepping in the little-endian/big-endian + // doo-doo. + wxString str = AsLegacyTimestampString(); + + for( int i = 0; i < 4; ++i ) + { + wxString octet = str.substr( i * 2, 2 ); + m_uuid.data[i + 12] = strtol( octet.data(), NULL, 16 ); + } +} + + +bool KIID::IsLegacyTimestamp() const +{ + return !m_uuid.data[8] && !m_uuid.data[9] && !m_uuid.data[10] && !m_uuid.data[11]; +} + + +timestamp_t KIID::AsLegacyTimestamp() const +{ + return m_cached_timestamp; +} + + +size_t KIID::Hash() const +{ + size_t hash = 0; + + // Note: this is NOT little-endian/big-endian safe, but as long as it's just used + // at runtime it won't matter. + + for( int i = 0; i < 4; ++i ) + boost::hash_combine( hash, reinterpret_cast( m_uuid.data )[i] ); + + return hash; +} + + +void KIID::Clone( const KIID& aUUID ) +{ + m_uuid = aUUID.m_uuid; + m_cached_timestamp = aUUID.m_cached_timestamp; +} + + +wxString KIID::AsString() const +{ + return boost::uuids::to_string( m_uuid ); +} + + +wxString KIID::AsLegacyTimestampString() const +{ + return wxString::Format( "%8.8lX", (unsigned long) AsLegacyTimestamp() ); +} + + +void KIID::ConvertTimestampToUuid() +{ + if( !IsLegacyTimestamp() ) + return; + + m_cached_timestamp = 0; + m_uuid = randomGenerator(); +} + + +KIID_PATH::KIID_PATH( const wxString& aString ) +{ + for( const wxString& pathStep : wxSplit( aString, '/' ) ) + { + if( !pathStep.empty() ) + emplace_back( KIID( pathStep ) ); + } +} + + +wxString KIID_PATH::AsString() const +{ + wxString path; + + for( const KIID& pathStep : *this ) + path += '/' + pathStep.AsString(); + + return path; +} \ No newline at end of file diff --git a/common/plugins/eagle/eagle_parser.h b/common/plugins/eagle/eagle_parser.h index fe2cd6b80b..e996502d38 100644 --- a/common/plugins/eagle/eagle_parser.h +++ b/common/plugins/eagle/eagle_parser.h @@ -29,6 +29,7 @@ #define _EAGLE_PARSER_H_ #include +#include #include #include diff --git a/common/swig/kicad.i b/common/swig/kicad.i index 4fcb25965e..f37cf17d2d 100644 --- a/common/swig/kicad.i +++ b/common/swig/kicad.i @@ -82,6 +82,7 @@ principle should be easily implemented by adapting the current STL containers. %{ #include #include + #include #include #include #include @@ -110,6 +111,7 @@ principle should be easily implemented by adapting the current STL containers. // header files that must be wrapped %include %include macros_swig.h +%include kiid.h %include core/typeinfo.h %include eda_item.h %include eda_rect.h diff --git a/include/common.h b/include/common.h index a69bd9ebc4..ae3f7e6864 100644 --- a/include/common.h +++ b/include/common.h @@ -49,139 +49,13 @@ #include #include #include -#include #include #include -#include class PROJECT; class SEARCH_STACK; class REPORTER; -/** - * timestamp_t is our type to represent unique IDs for all kinds of elements; - * historically simply the timestamp when they were created. - * - * Long term, this type might be renamed to something like unique_id_t - * (and then rename all the methods from {Get,Set}TimeStamp() - * to {Get,Set}Id()) ? - */ -typedef uint32_t timestamp_t; - -class KIID -{ -public: - 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; - - static bool SniffTest( const wxString& aCandidate ); - - /** - * 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. - */ - void ConvertTimestampToUuid(); - - bool operator==( KIID const& rhs) const - { - return m_uuid == rhs.m_uuid; - } - - bool operator!=( KIID const& rhs) const - { - return m_uuid != rhs.m_uuid; - } - - bool operator<( KIID const& rhs) const - { - return m_uuid < rhs.m_uuid; - } - -private: - boost::uuids::uuid m_uuid; - - timestamp_t m_cached_timestamp; -}; - - -extern KIID niluuid; - -KIID& NilUuid(); - -// declare KIID_VECT_LIST as std::vector both for c++ and swig: -DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID ) - -class KIID_PATH : public KIID_VECT_LIST -{ -public: - KIID_PATH() - {} - - KIID_PATH( const wxString& aString ) - { - for( const wxString& pathStep : wxSplit( aString, '/' ) ) - { - if( !pathStep.empty() ) - emplace_back( KIID( pathStep ) ); - } - } - - wxString AsString() const - { - wxString path; - - for( const KIID& pathStep : *this ) - path += '/' + pathStep.AsString(); - - return path; - } - - bool operator==( KIID_PATH const& rhs) const - { - if( size() != rhs.size() ) - return false; - - for( size_t i = 0; i < size(); ++i ) - { - if( at( i ) != rhs.at( i ) ) - return false; - } - - return true; - } - - bool operator<( KIID_PATH const& rhs) const - { - if( size() != rhs.size() ) - return size() < rhs.size(); - - for( size_t i = 0; i < size(); ++i ) - { - if( at( i ) < rhs.at( i ) ) - return true; - - if( at( i ) != rhs.at( i ) ) - return false; - } - - return false; - } -}; - - /// default name for nameless projects #define NAMELESS_PROJECT wxT( "noname" ) diff --git a/include/eda_item.h b/include/eda_item.h index 44ce51bd2c..4775c15691 100644 --- a/include/eda_item.h +++ b/include/eda_item.h @@ -33,6 +33,7 @@ #include #include #include +#include diff --git a/include/kiid.h b/include/kiid.h new file mode 100644 index 0000000000..76824d21c2 --- /dev/null +++ b/include/kiid.h @@ -0,0 +1,144 @@ +/* + * This program source code file is part of KiCad, a free EDA CAD application. + * + * Copyright (C) 2020 Ian McInerney + * Copyright (C) 2007-2014 Jean-Pierre Charras, jp.charras at wanadoo.fr + * Copyright (C) 1992-2020 KiCad Developers, see CHANGELOG.TXT for contributors. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, you may find one here: + * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + * or you may search the http://www.gnu.org website for the version 2 license, + * or you may write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef KIID_H +#define KIID_H + +#include +#include + +class wxString; + +/** + * timestamp_t is our type to represent unique IDs for all kinds of elements; + * historically simply the timestamp when they were created. + * + * Long term, this type might be renamed to something like unique_id_t + * (and then rename all the methods from {Get,Set}TimeStamp() + * to {Get,Set}Id()) ? + */ +typedef uint32_t timestamp_t; + +class KIID +{ +public: + 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; + + static bool SniffTest( const wxString& aCandidate ); + + /** + * 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. + */ + void ConvertTimestampToUuid(); + + bool operator==( KIID const& rhs ) const + { + return m_uuid == rhs.m_uuid; + } + + bool operator!=( KIID const& rhs ) const + { + return m_uuid != rhs.m_uuid; + } + + bool operator<( KIID const& rhs ) const + { + return m_uuid < rhs.m_uuid; + } + +private: + boost::uuids::uuid m_uuid; + + timestamp_t m_cached_timestamp; +}; + + +extern KIID niluuid; + +KIID& NilUuid(); + +// declare KIID_VECT_LIST as std::vector both for c++ and swig: +DECL_VEC_FOR_SWIG( KIID_VECT_LIST, KIID ) + +class KIID_PATH : public KIID_VECT_LIST +{ +public: + KIID_PATH() + { + } + + KIID_PATH( const wxString& aString ); + + + wxString AsString() const; + + bool operator==( KIID_PATH const& rhs ) const + { + if( size() != rhs.size() ) + return false; + + for( size_t i = 0; i < size(); ++i ) + { + if( at( i ) != rhs.at( i ) ) + return false; + } + + return true; + } + + bool operator<( KIID_PATH const& rhs ) const + { + if( size() != rhs.size() ) + return size() < rhs.size(); + + for( size_t i = 0; i < size(); ++i ) + { + if( at( i ) < rhs.at( i ) ) + return true; + + if( at( i ) != rhs.at( i ) ) + return false; + } + + return false; + } +}; + +#endif // KIID_H \ No newline at end of file diff --git a/include/macros_swig.h b/include/macros_swig.h index 365721788e..69d06a7f9d 100644 --- a/include/macros_swig.h +++ b/include/macros_swig.h @@ -54,4 +54,4 @@ #define DECL_SET_FOR_SWIG(TypeName, MemberType) typedef std::set TypeName; #endif -#endif // MACROS2_H +#endif // MACROS_SWIG_H diff --git a/include/project.h b/include/project.h index 6ddb74bbac..0875d28761 100644 --- a/include/project.h +++ b/include/project.h @@ -26,12 +26,12 @@ /** * @file project.h */ - +#include #include +#include #include #include #include -#include /// A variable name whose value holds the current project directory. /// Currently an environment variable, eventually a project variable. diff --git a/pcbnew/netinfo.h b/pcbnew/netinfo.h index 38d7190ea4..df74897b8d 100644 --- a/pcbnew/netinfo.h +++ b/pcbnew/netinfo.h @@ -29,7 +29,7 @@ #ifndef CLASS_NETINFO_ #define CLASS_NETINFO_ -#include +#include #include #include #include