/
Transactions.cpp
522 lines (451 loc) · 17.7 KB
/
Transactions.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/***************************************************************************
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library 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 Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#ifndef _PreComp_
# include <cassert>
#endif
#include <atomic>
/// Here the FreeCAD includes sorted by Base,App,Gui......
#include <Base/Writer.h>
using Base::Writer;
#include <Base/Reader.h>
using Base::XMLReader;
#include <Base/Console.h>
#include "Transactions.h"
#include "Property.h"
#include "Document.h"
#include "DocumentObject.h"
FC_LOG_LEVEL_INIT("App",true,true)
using namespace App;
using namespace std;
TYPESYSTEM_SOURCE(App::Transaction, Base::Persistence)
//**************************************************************************
// Construction/Destruction
Transaction::Transaction(int id)
{
if(!id) id = getNewID();
transID = id;
}
/**
* A destructor.
* A more elaborate description of the destructor.
*/
Transaction::~Transaction()
{
auto &index = _Objects.get<0>();
for (auto It= index.begin();It!=index.end();++It) {
if (It->second->status == TransactionObject::New) {
// If an object has been removed from the document the transaction
// status is 'New'. The 'pcNameInDocument' member serves as criterion
// to check whether the object is part of the document or not.
// Note, it's possible that the transaction status is 'New' while the
// object is (again) part of the document. This usually happens when
// a previous removal is undone.
// Thus, if the object has been removed, i.e. the status is 'New' and
// is still not part of the document the object must be destroyed not
// to cause a memory leak. This usually is the case when the removal
// of an object is not undone or when an addition is undone.
if (!It->first->isAttachedToDocument()) {
if (It->first->getTypeId().isDerivedFrom(DocumentObject::getClassTypeId())) {
// #0003323: Crash when clearing transaction list
// It can happen that when clearing the transaction list several objects
// are destroyed with dependencies which can lead to dangling pointers.
// When setting the 'Destroy' flag of an object the destructors of link
// properties don't ry to remove backlinks, i.e. they don't try to access
// possible dangling pointers.
// An alternative solution is to call breakDependency inside
// Document::_removeObject. Make this change in v0.18.
const DocumentObject* obj = static_cast<const DocumentObject*>(It->first);
const_cast<DocumentObject*>(obj)->setStatus(ObjectStatus::Destroy, true);
}
delete It->first;
}
}
delete It->second;
}
}
static std::atomic<int> _TransactionID;
int Transaction::getNewID() {
int id = ++_TransactionID;
if(id) return id;
// wrap around? really?
return ++_TransactionID;
}
int Transaction::getLastID() {
return _TransactionID;
}
unsigned int Transaction::getMemSize (void) const
{
return 0;
}
void Transaction::Save (Base::Writer &/*writer*/) const
{
assert(0);
}
void Transaction::Restore(Base::XMLReader &/*reader*/)
{
assert(0);
}
int Transaction::getID(void) const
{
return transID;
}
bool Transaction::isEmpty() const
{
return _Objects.empty();
}
bool Transaction::hasObject(const TransactionalObject *Obj) const
{
return !!_Objects.get<1>().count(Obj);
}
void Transaction::addOrRemoveProperty(TransactionalObject *Obj,
const Property* pcProp, bool add)
{
auto &index = _Objects.get<1>();
auto pos = index.find(Obj);
TransactionObject *To;
if (pos != index.end()) {
To = pos->second;
}
else {
To = TransactionFactory::instance().createTransaction(Obj->getTypeId());
To->status = TransactionObject::Chn;
index.emplace(Obj,To);
}
To->addOrRemoveProperty(pcProp,add);
}
//**************************************************************************
// separator for other implementation aspects
void Transaction::apply(Document &Doc, bool forward)
{
std::string errMsg;
try {
auto &index = _Objects.get<0>();
for(auto &info : index)
info.second->applyDel(Doc, const_cast<TransactionalObject*>(info.first));
for(auto &info : index)
info.second->applyNew(Doc, const_cast<TransactionalObject*>(info.first));
for(auto &info : index)
info.second->applyChn(Doc, const_cast<TransactionalObject*>(info.first), forward);
}catch(Base::Exception &e) {
e.ReportException();
errMsg = e.what();
}catch(std::exception &e) {
errMsg = e.what();
}catch(...) {
errMsg = "Unknown exception";
}
if(errMsg.size()) {
FC_ERR("Exception on " << (forward?"redo":"undo") << " '"
<< Name << "':" << errMsg);
}
}
void Transaction::addObjectNew(TransactionalObject *Obj)
{
auto &index = _Objects.get<1>();
auto pos = index.find(Obj);
if (pos != index.end()) {
if (pos->second->status == TransactionObject::Del) {
delete pos->second;
delete pos->first;
index.erase(pos);
}
else {
pos->second->status = TransactionObject::New;
pos->second->_NameInDocument = Obj->detachFromDocument();
// move item at the end to make sure the order of removal is kept
auto &seq = _Objects.get<0>();
seq.relocate(seq.end(),_Objects.project<0>(pos));
}
}
else {
TransactionObject *To = TransactionFactory::instance().createTransaction(Obj->getTypeId());
To->status = TransactionObject::New;
To->_NameInDocument = Obj->detachFromDocument();
index.emplace(Obj,To);
}
}
void Transaction::addObjectDel(const TransactionalObject *Obj)
{
auto &index = _Objects.get<1>();
auto pos = index.find(Obj);
// is it created in this transaction ?
if (pos != index.end() && pos->second->status == TransactionObject::New) {
// remove completely from transaction
delete pos->second;
index.erase(pos);
}
else if (pos != index.end() && pos->second->status == TransactionObject::Chn) {
pos->second->status = TransactionObject::Del;
}
else {
TransactionObject *To = TransactionFactory::instance().createTransaction(Obj->getTypeId());
To->status = TransactionObject::Del;
index.emplace(Obj,To);
}
}
void Transaction::addObjectChange(const TransactionalObject *Obj, const Property *Prop)
{
auto &index = _Objects.get<1>();
auto pos = index.find(Obj);
TransactionObject *To;
if (pos != index.end()) {
To = pos->second;
}
else {
To = TransactionFactory::instance().createTransaction(Obj->getTypeId());
To->status = TransactionObject::Chn;
index.emplace(Obj,To);
}
To->setProperty(Prop);
}
//**************************************************************************
//**************************************************************************
// TransactionObject
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TYPESYSTEM_SOURCE_ABSTRACT(App::TransactionObject, Base::Persistence)
//**************************************************************************
// Construction/Destruction
/**
* A constructor.
* A more elaborate description of the constructor.
*/
TransactionObject::TransactionObject()
: status(New)
{
}
/**
* A destructor.
* A more elaborate description of the destructor.
*/
TransactionObject::~TransactionObject()
{
for(auto &v : _PropChangeMap)
delete v.second.property;
}
void TransactionObject::applyDel(Document & /*Doc*/, TransactionalObject * /*pcObj*/)
{
}
void TransactionObject::applyNew(Document & /*Doc*/, TransactionalObject * /*pcObj*/)
{
}
void TransactionObject::applyChn(Document & /*Doc*/, TransactionalObject *pcObj, bool /* Forward */)
{
if (status == New || status == Chn) {
// Property change order is not preserved, as it is recursive in nature
for(auto &v : _PropChangeMap) {
auto &data = v.second;
auto prop = const_cast<Property*>(data.propertyOrig);
if(!data.property) {
// here means we are undoing/redoing and property add operation
pcObj->removeDynamicProperty(v.second.name.c_str());
continue;
}
// getPropertyName() is specially coded to be safe even if prop has
// been destroies. We must prepare for the case where user removed
// a dynamic property but does not recordered as transaction.
auto name = pcObj->getPropertyName(prop);
if(!name || (data.name.size() && data.name != name) || data.propertyType != prop->getTypeId()) {
// Here means the original property is not found, probably removed
if(data.name.empty()) {
// not a dynamic property, nothing to do
continue;
}
// It is possible for the dynamic property to be removed and
// restored. But since restoring property is actually creating
// a new property, the property key inside redo stack will not
// match. So we search by name first.
prop = pcObj->getDynamicPropertyByName(data.name.c_str());
if(!prop) {
// Still not found, re-create the property
prop = pcObj->addDynamicProperty(
data.propertyType.getName(),
data.name.c_str(), data.group.c_str(), data.doc.c_str(),
data.attr, data.readonly, data.hidden);
if(!prop)
continue;
prop->setStatusValue(data.property->getStatus());
}
}
// Many properties do not bother implement Copy() and accepts
// derived types just fine in Paste(). So we do not enforce type
// matching here. But instead, strengthen type checking in all
// Paste() implementation.
//
// if(data.propertyType != prop->getTypeId()) {
// FC_WARN("Cannot " << (Forward?"redo":"undo")
// << " change of property " << prop->getName()
// << " because of type change: "
// << data.propertyType.getName()
// << " -> " << prop->getTypeId().getName());
// continue;
// }
try {
prop->Paste(*data.property);
} catch (Base::Exception &e) {
e.ReportException();
FC_ERR("exception while restoring " << prop->getFullName() << ": " << e.what());
} catch (std::exception &e) {
FC_ERR("exception while restoring " << prop->getFullName() << ": " << e.what());
} catch (...)
{}
}
}
}
void TransactionObject::setProperty(const Property* pcProp)
{
auto &data = _PropChangeMap[pcProp->getID()];
if(!data.property && data.name.empty()) {
static_cast<DynamicProperty::PropData&>(data) =
pcProp->getContainer()->getDynamicPropertyData(pcProp);
data.propertyOrig = pcProp;
data.property = pcProp->Copy();
data.propertyType = pcProp->getTypeId();
data.property->setStatusValue(pcProp->getStatus());
}
}
void TransactionObject::addOrRemoveProperty(const Property* pcProp, bool add)
{
(void)add;
if(!pcProp || !pcProp->getContainer())
return;
auto &data = _PropChangeMap[pcProp->getID()];
if(data.name.size()) {
if(!add && !data.property) {
// this means add and remove the same property inside a single
// transaction, so they cancel each other out.
_PropChangeMap.erase(pcProp->getID());
}
return;
}
if(data.property) {
delete data.property;
data.property = 0;
}
data.propertyOrig = pcProp;
static_cast<DynamicProperty::PropData&>(data) =
pcProp->getContainer()->getDynamicPropertyData(pcProp);
if(add)
data.property = 0;
else {
data.property = pcProp->Copy();
data.propertyType = pcProp->getTypeId();
data.property->setStatusValue(pcProp->getStatus());
}
}
unsigned int TransactionObject::getMemSize (void) const
{
return 0;
}
void TransactionObject::Save (Base::Writer &/*writer*/) const
{
assert(0);
}
void TransactionObject::Restore(Base::XMLReader &/*reader*/)
{
assert(0);
}
//**************************************************************************
//**************************************************************************
// TransactionDocumentObject
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TYPESYSTEM_SOURCE_ABSTRACT(App::TransactionDocumentObject, App::TransactionObject)
//**************************************************************************
// Construction/Destruction
/**
* A constructor.
* A more elaborate description of the constructor.
*/
TransactionDocumentObject::TransactionDocumentObject()
{
}
/**
* A destructor.
* A more elaborate description of the destructor.
*/
TransactionDocumentObject::~TransactionDocumentObject()
{
}
void TransactionDocumentObject::applyDel(Document &Doc, TransactionalObject *pcObj)
{
if (status == Del) {
DocumentObject* obj = static_cast<DocumentObject*>(pcObj);
#ifndef USE_OLD_DAG
//Make sure the backlinks of all linked objects are updated. As the links of the removed
//object are never set to [] they also do not remove the backlink. But as they are
//not in the document anymore we need to remove them anyway to ensure a correct graph
auto list = obj->getOutList();
for (auto link : list)
link->_removeBackLink(obj);
#endif
// simply filling in the saved object
Doc._removeObject(obj);
}
}
void TransactionDocumentObject::applyNew(Document &Doc, TransactionalObject *pcObj)
{
if (status == New) {
DocumentObject* obj = static_cast<DocumentObject*>(pcObj);
Doc._addObject(obj, _NameInDocument.c_str());
#ifndef USE_OLD_DAG
//make sure the backlinks of all linked objects are updated
auto list = obj->getOutList();
for (auto link : list)
link->_addBackLink(obj);
#endif
}
}
//**************************************************************************
//**************************************************************************
// TransactionFactory
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
App::TransactionFactory* App::TransactionFactory::self = nullptr;
TransactionFactory& TransactionFactory::instance()
{
if (self == nullptr)
self = new TransactionFactory;
return *self;
}
void TransactionFactory::destruct()
{
delete self;
self = nullptr;
}
void TransactionFactory::addProducer (const Base::Type& type, Base::AbstractProducer *producer)
{
producers[type] = producer;
}
/**
* Creates a transaction object for the given type id.
*/
TransactionObject* TransactionFactory::createTransaction (const Base::Type& type) const
{
std::map<Base::Type, Base::AbstractProducer*>::const_iterator it;
for (it = producers.begin(); it != producers.end(); ++it) {
if (type.isDerivedFrom(it->first)) {
return static_cast<TransactionObject*>(it->second->Produce());
}
}
assert(0);
return nullptr;
}