/
MergeOperation.cpp
245 lines (207 loc) · 6.5 KB
/
MergeOperation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "MergeOperation.h"
#include "ientity.h"
#include "../Clone.h"
#include "../scenelib.h"
namespace scene
{
namespace merge
{
class RemoveNodeFromParentAction :
public MergeAction
{
private:
scene::INodePtr _child;
protected:
RemoveNodeFromParentAction(const scene::INodePtr& child, ActionType type) :
MergeAction(type),
_child(child)
{
assert(_child);
}
public:
void applyChanges() override
{
removeNodeFromParent(_child);
}
};
class RemoveChildAction :
public RemoveNodeFromParentAction
{
public:
RemoveChildAction(const scene::INodePtr& node) :
RemoveNodeFromParentAction(node, ActionType::RemoveChildNode)
{}
};
class RemoveEntityAction :
public RemoveNodeFromParentAction
{
public:
RemoveEntityAction(const scene::INodePtr& node) :
RemoveNodeFromParentAction(node, ActionType::RemoveEntity)
{}
};
class AddCloneToParentAction :
public MergeAction
{
private:
scene::INodePtr _node;
scene::INodePtr _parent;
protected:
// Will add the given node to the parent when applyChanges() is called
AddCloneToParentAction(const scene::INodePtr& node, const scene::INodePtr& parent, ActionType type) :
MergeAction(type),
_node(node),
_parent(parent)
{
assert(_node);
assert(Node_getCloneable(node));
}
public:
void applyChanges() override
{
// No post-clone callback since we don't care about selection groups right now
auto cloned = cloneNodeIncludingDescendants(_node, PostCloneCallback());
if (!cloned)
{
throw std::runtime_error("Node " + _node->name() + " is not cloneable");
}
addNodeToContainer(cloned, _parent);
}
};
class AddEntityAction :
public AddCloneToParentAction
{
public:
AddEntityAction(const scene::INodePtr& node, const scene::IMapRootNodePtr& targetRoot) :
AddCloneToParentAction(node, targetRoot, ActionType::AddEntity)
{}
};
class AddChildAction :
public AddCloneToParentAction
{
public:
AddChildAction(const scene::INodePtr& node, const scene::INodePtr& parent) :
AddCloneToParentAction(node, parent, ActionType::AddChildNode)
{}
};
class SetEntityKeyValueAction :
public MergeAction
{
private:
scene::INodePtr _node;
std::string _key;
std::string _value;
public:
// Will call setKeyValue(key, value) on the targetnode when applyChanges() is called
SetEntityKeyValueAction(const scene::INodePtr& node, const std::string& key, const std::string& value, ActionType mergeActionType) :
MergeAction(mergeActionType),
_node(node),
_key(key),
_value(value)
{
assert(_node);
assert(!_key.empty());
}
void applyChanges() override
{
// No post-clone callback since we don't care about selection groups right now
auto entity = Node_getEntity(_node);
if (!entity)
{
throw std::runtime_error("Node " + _node->name() + " is not an entity");
}
entity->setKeyValue(_key, _value);
}
};
class AddEntityKeyValueAction :
public SetEntityKeyValueAction
{
public:
AddEntityKeyValueAction(const scene::INodePtr& node, const std::string& key, const std::string& value) :
SetEntityKeyValueAction(node, key, value, ActionType::AddKeyValue)
{}
};
class RemoveEntityKeyValueAction :
public SetEntityKeyValueAction
{
public:
RemoveEntityKeyValueAction(const scene::INodePtr& node, const std::string& key) :
SetEntityKeyValueAction(node, key, std::string(), ActionType::RemoveKeyValue)
{}
};
class ChangeEntityKeyValueAction :
public SetEntityKeyValueAction
{
public:
ChangeEntityKeyValueAction(const scene::INodePtr& node, const std::string& key, const std::string& value) :
SetEntityKeyValueAction(node, key, value, ActionType::ChangeKeyValue)
{}
};
void MergeOperation::addAction(const MergeAction::Ptr& action)
{
_actions.push_back(action);
}
void MergeOperation::createActionsForKeyValueDiff(const ComparisonResult::KeyValueDifference& difference,
const scene::INodePtr& targetEntity)
{
switch (difference.type)
{
case ComparisonResult::KeyValueDifference::Type::KeyValueAdded:
addAction(std::make_shared<AddEntityKeyValueAction>(targetEntity, difference.key, difference.value));
break;
case ComparisonResult::KeyValueDifference::Type::KeyValueRemoved:
addAction(std::make_shared<RemoveEntityKeyValueAction>(targetEntity, difference.key));
break;
case ComparisonResult::KeyValueDifference::Type::KeyValueChanged:
addAction(std::make_shared<ChangeEntityKeyValueAction>(targetEntity, difference.key, difference.value));
break;
}
}
void MergeOperation::createActionsForPrimitiveDiff(const ComparisonResult::PrimitiveDifference& difference,
const scene::INodePtr& targetEntity)
{
switch (difference.type)
{
case ComparisonResult::PrimitiveDifference::Type::PrimitiveAdded:
addAction(std::make_shared<AddChildAction>(difference.node, targetEntity));
break;
case ComparisonResult::PrimitiveDifference::Type::PrimitiveRemoved:
addAction(std::make_shared<RemoveChildAction>(difference.node));
break;
}
}
void MergeOperation::createActionsForEntity(const ComparisonResult::EntityDifference& difference)
{
switch (difference.type)
{
case ComparisonResult::EntityDifference::Type::EntityMissingInSource:
addAction(std::make_shared<RemoveEntityAction>(difference.baseNode));
break;
case ComparisonResult::EntityDifference::Type::EntityMissingInBase:
addAction(std::make_shared<AddEntityAction>(difference.sourceNode, _targetRoot));
break;
case ComparisonResult::EntityDifference::Type::EntityPresentButDifferent:
{
for (const auto& keyValueDiff : difference.differingKeyValues)
{
createActionsForKeyValueDiff(keyValueDiff, difference.baseNode);
}
for (const auto& primitiveDiff : difference.differingChildren)
{
createActionsForPrimitiveDiff(primitiveDiff, difference.baseNode);
}
break;
}
};
}
MergeOperation::Ptr MergeOperation::CreateFromComparisonResult(const ComparisonResult& comparisonResult)
{
auto operation = std::make_shared<MergeOperation>(comparisonResult.getBaseRootNode());
for (const auto& difference : comparisonResult.differingEntities)
{
operation->createActionsForEntity(difference);
}
return operation;
}
}
}