KiCad PCB EDA Suite
Loading...
Searching...
No Matches
kicad_differ.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 */
20
21#ifndef KICAD_DIFFER_H
22#define KICAD_DIFFER_H
23
24#include <kicommon.h>
25
28
29#include <functional>
30#include <set>
31
32
33namespace KICAD_DIFF
34{
35
48{
49public:
50 struct OPTIONS
51 {
54 bool deepCompare = true;
55
59
61 std::function<void( double )> progress;
62 };
63
64 virtual ~KICAD_DIFFER();
65
66 void SetOptions( const OPTIONS& aOptions ) { m_options = aOptions; }
67 const OPTIONS& GetOptions() const { return m_options; }
68
75 virtual DOCUMENT_DIFF Diff() = 0;
76
77protected:
78 KICAD_DIFFER() = default;
79
81};
82
83
93template <typename MAP, typename BBoxFn, typename ChangedFn>
94DOCUMENT_DIFF DiffLibraryByName( const MAP& aBefore, const MAP& aAfter, const wxString& aPath,
95 const wxString& aDocType, const wxString& aTypeName,
96 const KICAD_DIFFER::OPTIONS& aOptions, BBoxFn aBBox,
97 ChangedFn aChanged )
98{
100 result.path = aPath;
101 result.docType = aDocType;
102
103 std::set<wxString> allNames;
104
105 for( const auto& [name, item] : aBefore )
106 allNames.insert( name );
107
108 for( const auto& [name, item] : aAfter )
109 allNames.insert( name );
110
111 if( aOptions.progress )
112 aOptions.progress( 0.1 );
113
114 std::size_t processed = 0;
115
116 for( const wxString& name : allNames )
117 {
118 ++processed;
119
120 if( aOptions.progress )
121 aOptions.progress( 0.1 + 0.9 * processed / allNames.size() );
122
123 auto beforeIt = aBefore.find( name );
124 auto afterIt = aAfter.find( name );
125 const auto* before = beforeIt == aBefore.end() ? nullptr : beforeIt->second;
126 const auto* after = afterIt == aAfter.end() ? nullptr : afterIt->second;
127
128 ITEM_CHANGE c;
130 c.typeName = aTypeName;
131 c.refdes = name;
132
133 if( before && !after )
134 {
136 c.bbox = aBBox( before );
137 result.changes.push_back( std::move( c ) );
138 }
139 else if( !before && after )
140 {
142 c.bbox = aBBox( after );
143 result.changes.push_back( std::move( c ) );
144 }
145 else if( before && after && aChanged( before, after ) )
146 {
148 c.bbox = aBBox( after );
149 result.changes.push_back( std::move( c ) );
150 }
151 }
152
153 if( aOptions.progress )
154 aOptions.progress( 1.0 );
155
156 return result;
157}
158
159} // namespace KICAD_DIFF
160
161#endif // KICAD_DIFFER_H
const char * name
virtual DOCUMENT_DIFF Diff()=0
Produce a DOCUMENT_DIFF of the inputs the concrete differ was constructed with.
void SetOptions(const OPTIONS &aOptions)
const OPTIONS & GetOptions() const
#define KICOMMON_API
Definition kicommon.h:27
DOCUMENT_DIFF DiffLibraryByName(const MAP &aBefore, const MAP &aAfter, const wxString &aPath, const wxString &aDocType, const wxString &aTypeName, const KICAD_DIFFER::OPTIONS &aOptions, BBoxFn aBBox, ChangedFn aChanged)
Shared name-keyed library diff used by FP_LIB_DIFFER and SYM_LIB_DIFFER.
KIID_PATH LibraryItemKiidPath(const wxString &aName)
Build a deterministic synthetic KIID_PATH from a library item name (symbol name or footprint name).
The full set of changes between two parsed documents of one type.
One change record on a single item.
std::optional< wxString > refdes
bool deepCompare
Emit per-property deltas via PROPERTY_MANAGER.
std::function< void(double)> progress
Optional progress reporter — invoked with a fraction in [0, 1].
IDENTITY_RECONCILER::CONFIG identity
Configuration for identity reconciliation.
wxString result
Test unit parsing edge cases and error handling.