KiCad PCB EDA Suite
Loading...
Searching...
No Matches
lib_merge_applier.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 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24#ifndef LIB_MERGE_APPLIER_H
25#define LIB_MERGE_APPLIER_H
26
29
30#include <trace_helpers.h>
31
32#include <functional>
33#include <map>
34#include <memory>
35#include <set>
36#include <vector>
37
38#include <wx/log.h>
39#include <wx/string.h>
40
41
42namespace KICAD_DIFF
43{
44
58template <typename ITEM>
60{
61public:
62 using ITEM_MAP = std::map<wxString, const ITEM*>;
63
65 using COMBINER = std::function<std::unique_ptr<ITEM>( const KIID_PATH& aId )>;
66
67 LIB_MERGE_APPLIER( const ITEM_MAP& aAncestor, const ITEM_MAP& aOurs, const ITEM_MAP& aTheirs, MERGE_PLAN aPlan,
68 COMBINER aCombine = {} ) :
69 m_ancestor( aAncestor ),
70 m_ours( aOurs ),
71 m_theirs( aTheirs ),
72 m_plan( std::move( aPlan ) ),
73 m_combine( std::move( aCombine ) )
74 {
75 }
76
77 struct REPORT
78 {
79 std::size_t itemsTakenOurs = 0;
80 std::size_t itemsTakenTheirs = 0;
81 std::size_t itemsTakenAncestor = 0;
82 std::size_t itemsDeleted = 0;
83 std::size_t itemsKept = 0;
84 std::size_t itemsCombined = 0;
85 std::size_t mergePropsFallback = 0;
86
91 std::vector<KIID_PATH> mergePropsFallbackIds;
92 };
93
94 std::vector<std::unique_ptr<ITEM>> Apply()
95 {
96 m_report = {};
97
98 std::map<wxString, const ITEM*> live = m_ancestor;
99 std::map<wxString, std::unique_ptr<ITEM>> combined;
100
101 std::map<KIID_PATH, const ITEM_RESOLUTION*> actionsById;
102
103 for( const ITEM_RESOLUTION& res : m_plan.actions )
104 actionsById[res.id] = &res;
105
106 const std::set<wxString> allNames = collectNames( m_ancestor, m_ours, m_theirs );
107
108 for( const wxString& name : allNames )
109 {
110 auto resIt = actionsById.find( LibraryItemKiidPath( name ) );
111
112 if( resIt == actionsById.end() )
113 continue;
114
115 const ITEM_RESOLUTION& res = *resIt->second;
116
117 switch( res.kind )
118 {
119 case ITEM_RES::TAKE_OURS: takeFrom( m_ours, name, live, m_report.itemsTakenOurs ); break;
120 case ITEM_RES::TAKE_THEIRS: takeFrom( m_theirs, name, live, m_report.itemsTakenTheirs ); break;
121 case ITEM_RES::TAKE_ANCESTOR: takeFrom( m_ancestor, name, live, m_report.itemsTakenAncestor ); break;
122
124 live.erase( name );
125 ++m_report.itemsDeleted;
126 break;
127
128 case ITEM_RES::KEEP:
129 {
130 // KEEP is emitted for unresolved both-added collisions (and
131 // similar) where the engine wants the applier to do something
132 // safe. Walk ancestor -> ours -> theirs so an independent
133 // same-name add on both sides doesn't silently vanish.
134 const ITEM* src = findItem( m_ancestor, name );
135
136 if( !src )
137 src = findItem( m_ours, name );
138
139 if( !src )
140 src = findItem( m_theirs, name );
141
142 if( src )
143 live[name] = src;
144 else
145 live.erase( name );
146
147 ++m_report.itemsKept;
148 break;
149 }
150
152 if( std::unique_ptr<ITEM> item = m_combine ? m_combine( res.id ) : nullptr )
153 {
154 live[name] = item.get();
155 combined[name] = std::move( item );
156 ++m_report.itemsCombined;
157 break;
158 }
159
160 // Take ours so the chosen side at least lands intact, and
161 // surface the count + ID so the handler can rebuild the
162 // marker plan with this item flagged.
163 takeFrom( m_ours, name, live, m_report.mergePropsFallback );
164 m_report.mergePropsFallbackIds.push_back( res.id );
165 wxLogTrace( traceDiffMerge,
166 wxT( "lib_merge_applier: MERGE_PROPS for '%s' fell back to "
167 "TAKE_OURS" ),
168 name );
169 break;
170 }
171 }
172
173 std::vector<std::unique_ptr<ITEM>> out;
174 out.reserve( live.size() );
175
176 for( const auto& [name, item] : live )
177 {
178 if( auto it = combined.find( name ); it != combined.end() )
179 out.push_back( std::move( it->second ) );
180 else if( item )
181 out.emplace_back( std::make_unique<ITEM>( *item ) );
182 }
183
184 return out;
185 }
186
187 const REPORT& GetReport() const { return m_report; }
188
189private:
190 static const ITEM* findItem( const ITEM_MAP& aMap, const wxString& aName )
191 {
192 auto it = aMap.find( aName );
193 return it == aMap.end() ? nullptr : it->second;
194 }
195
196 static std::set<wxString> collectNames( const ITEM_MAP& aAncestor, const ITEM_MAP& aOurs, const ITEM_MAP& aTheirs )
197 {
198 std::set<wxString> out;
199
200 for( const auto& [name, _] : aAncestor )
201 out.insert( name );
202 for( const auto& [name, _] : aOurs )
203 out.insert( name );
204 for( const auto& [name, _] : aTheirs )
205 out.insert( name );
206
207 return out;
208 }
209
210 static void takeFrom( const ITEM_MAP& aSource, const wxString& aName, std::map<wxString, const ITEM*>& aLive,
211 std::size_t& aCounter )
212 {
213 if( const ITEM* src = findItem( aSource, aName ) )
214 aLive[aName] = src;
215 else
216 aLive.erase( aName );
217
218 ++aCounter;
219 }
220
226 REPORT m_report;
227};
228
229} // namespace KICAD_DIFF
230
231#endif // LIB_MERGE_APPLIER_H
const char * name
static const ITEM * findItem(const ITEM_MAP &aMap, const wxString &aName)
static void takeFrom(const ITEM_MAP &aSource, const wxString &aName, std::map< wxString, const ITEM * > &aLive, std::size_t &aCounter)
const REPORT & GetReport() const
std::map< wxString, const LIB_SYMBOL * > ITEM_MAP
std::vector< std::unique_ptr< ITEM > > Apply()
LIB_MERGE_APPLIER(const ITEM_MAP &aAncestor, const ITEM_MAP &aOurs, const ITEM_MAP &aTheirs, MERGE_PLAN aPlan, COMBINER aCombine={})
static std::set< wxString > collectNames(const ITEM_MAP &aAncestor, const ITEM_MAP &aOurs, const ITEM_MAP &aTheirs)
std::function< std::unique_ptr< LIB_SYMBOL >(const KIID_PATH &aId)> COMBINER
#define _(s)
const wxChar *const traceDiffMerge
Flag to enable diff/merge engine and renderer debugging output.
KIID_PATH LibraryItemKiidPath(const wxString &aName)
Build a deterministic synthetic KIID_PATH from a library item name (symbol name or footprint name).
STL namespace.
std::vector< KIID_PATH > mergePropsFallbackIds
Item IDs (KIID_PATH from LibraryItemKiidPath) for resolutions that the applier silently downgraded (c...
Result of planning a 3-way merge.
nanodbc::result res
wxLogTrace helper definitions.