/
Transactions.h
191 lines (155 loc) · 6.26 KB
/
Transactions.h
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
/***************************************************************************
* Copyright (c) Juergen Riegel <juergen.riegel@web.de> *
* *
* 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 *
* *
***************************************************************************/
#ifndef APP_TRANSACTION_H
#define APP_TRANSACTION_H
#include <unordered_map>
#include <Base/Factory.h>
#include <Base/Persistence.h>
#include <App/PropertyContainer.h>
namespace App
{
class Document;
class Property;
class Transaction;
class TransactionObject;
class TransactionalObject;
/** Represents a atomic transaction of the document
*/
class AppExport Transaction : public Base::Persistence
{
TYPESYSTEM_HEADER();
public:
/** Construction
*
* @param id: transaction id. If zero, then it will be generated
* automatically as a monotonically increasing index across the entire
* application. User can pass in a transaction id to group multiple
* transactions from different document, so that they can be undo/redo
* together.
*/
Transaction(int id = 0);
/// Construction
virtual ~Transaction();
/// apply the content to the document
void apply(Document &Doc,bool forward);
// the utf-8 name of the transaction
std::string Name;
virtual unsigned int getMemSize (void) const;
virtual void Save (Base::Writer &writer) const;
/// This method is used to restore properties from an XML document.
virtual void Restore(Base::XMLReader &reader);
/// Return the transaction ID
int getID(void) const;
/// Generate a new unique transaction ID
static int getNewID(void);
static int getLastID(void);
/// Returns true if the transaction list is empty; otherwise returns false.
bool isEmpty() const;
/// check if this object is used in a transaction
bool hasObject(const TransactionalObject *Obj) const;
void addOrRemoveProperty(TransactionalObject *Obj, const Property* pcProp, bool add);
void addObjectNew(TransactionalObject *Obj);
void addObjectDel(const TransactionalObject *Obj);
void addObjectChange(const TransactionalObject *Obj, const Property *Prop);
private:
int transID;
typedef std::pair<const TransactionalObject*, TransactionObject*> Info;
bmi::multi_index_container<
Info,
bmi::indexed_by<
bmi::sequenced<>,
bmi::hashed_unique<
bmi::member<Info, const TransactionalObject*, &Info::first>
>
>
> _Objects;
};
/** Represents an entry for an object in a Transaction
*/
class AppExport TransactionObject : public Base::Persistence
{
TYPESYSTEM_HEADER();
public:
/// Construction
TransactionObject();
/// Destruction
virtual ~TransactionObject();
virtual void applyNew(Document &Doc, TransactionalObject *pcObj);
virtual void applyDel(Document &Doc, TransactionalObject *pcObj);
virtual void applyChn(Document &Doc, TransactionalObject *pcObj, bool Forward);
void setProperty(const Property* pcProp);
void addOrRemoveProperty(const Property* pcProp, bool add);
virtual unsigned int getMemSize (void) const;
virtual void Save (Base::Writer &writer) const;
/// This method is used to restore properties from an XML document.
virtual void Restore(Base::XMLReader &reader);
friend class Transaction;
protected:
enum Status {New,Del,Chn} status;
std::unordered_map<const Property*, DynamicProperty::PropData> _PropChangeMap;
std::string _NameInDocument;
};
/** Represents an entry for a document object in a transaction
*/
class AppExport TransactionDocumentObject : public TransactionObject
{
TYPESYSTEM_HEADER();
public:
/// Construction
TransactionDocumentObject();
/// Destruction
virtual ~TransactionDocumentObject();
void applyNew(Document &Doc, TransactionalObject *pcObj);
void applyDel(Document &Doc, TransactionalObject *pcObj);
};
class AppExport TransactionFactory
{
public:
static TransactionFactory& instance();
static void destruct ();
TransactionObject* createTransaction (const Base::Type& type) const;
void addProducer (const Base::Type& type, Base::AbstractProducer *producer);
private:
static TransactionFactory* self;
std::map<Base::Type, Base::AbstractProducer*> producers;
TransactionFactory(){}
~TransactionFactory(){}
};
template <class CLASS>
class TransactionProducer : public Base::AbstractProducer
{
public:
TransactionProducer (const Base::Type& type)
{
TransactionFactory::instance().addProducer(type, this);
}
virtual ~TransactionProducer (){}
/**
* Creates an instance of the specified transaction object.
*/
virtual void* Produce () const
{
return (new CLASS);
}
};
} //namespace App
#endif // APP_TRANSACTION_H