| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 3 |
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#ifndef APP_TRANSACTION_H |
| 26 |
#define APP_TRANSACTION_H |
| 27 |
|
| 28 |
#include <unordered_map> |
| 29 |
#include <Base/Factory.h> |
| 30 |
#include <Base/Persistence.h> |
| 31 |
#include <App/PropertyContainer.h> |
| 32 |
|
| 33 |
namespace App |
| 34 |
{ |
| 35 |
|
| 36 |
class Document; |
| 37 |
class Property; |
| 38 |
class Transaction; |
| 39 |
class TransactionObject; |
| 40 |
class TransactionalObject; |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* @brief A class that represents an atomic transaction of the document. |
| 45 |
* |
| 46 |
* A transaction can contain multiple actions. These actions are represented |
| 47 |
* by TransactionObject objects. Actions can be on objects: adding one, |
| 48 |
* removing one, or changing one. It can also be on properties: adding one or |
| 49 |
* removing one. |
| 50 |
*/ |
| 51 |
class AppExport Transaction: public Base::Persistence |
| 52 |
{ |
| 53 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 54 |
|
| 55 |
public: |
| 56 |
/** |
| 57 |
* @brief Construct a transaction. |
| 58 |
* |
| 59 |
* @param[in] id: The transaction id. If zero, then it will be generated |
| 60 |
* automatically as a monotonically increasing index across the entire |
| 61 |
* application. Users can pass in a transaction id to group multiple |
| 62 |
* transactions from different document, so that they can be undone/redone |
| 63 |
* together. |
| 64 |
*/ |
| 65 |
explicit Transaction(int id = 0); |
| 66 |
|
| 67 |
~Transaction() override; |
| 68 |
|
| 69 |
/** |
| 70 |
* @brief Apply the content of this transaction to the document. |
| 71 |
* |
| 72 |
* @param[in] Doc The document to apply the transaction to. |
| 73 |
* @param[in] forward If true, apply the transaction; otherwise, undo it. |
| 74 |
*/ |
| 75 |
void apply(Document& Doc, bool forward); |
| 76 |
|
| 77 |
/// The UTF-8 name of the transaction |
| 78 |
std::string Name; |
| 79 |
|
| 80 |
unsigned int getMemSize() const override; |
| 81 |
void Save(Base::Writer& writer) const override; |
| 82 |
void Restore(Base::XMLReader& reader) override; |
| 83 |
|
| 84 |
/// Get the transaction ID of this transaction. |
| 85 |
int getID() const; |
| 86 |
|
| 87 |
/// Generate a new unique transaction ID. |
| 88 |
static int getNewID(); |
| 89 |
|
| 90 |
/// Get the last transaction ID. |
| 91 |
static int getLastID(); |
| 92 |
|
| 93 |
/// Check if the transaction list is empty. |
| 94 |
bool isEmpty() const; |
| 95 |
|
| 96 |
/** |
| 97 |
* @brief Check if this object is used in a transaction. |
| 98 |
* |
| 99 |
* @param[in] Obj The object to check. |
| 100 |
* @return true if the object is used in a transaction; otherwise false. |
| 101 |
*/ |
| 102 |
bool hasObject(const TransactionalObject* Obj) const; |
| 103 |
|
| 104 |
/** |
| 105 |
* @brief Record renaming a property. |
| 106 |
* |
| 107 |
* @param[in] Obj The object of which the property to rename. |
| 108 |
* @param[in] pcProp The property with a new name. |
| 109 |
* @param[in] oldName The old name of the property. |
| 110 |
*/ |
| 111 |
void renameProperty(TransactionalObject* Obj, const Property* pcProp, const char* oldName); |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief Record adding or removing a property from an object. |
| 115 |
* |
| 116 |
* @param[in] Obj The object to add or remove the property from. |
| 117 |
* @param[in] pcProp The property to add or remove. |
| 118 |
* @param[in] add If true, add the property; otherwise, remove it. |
| 119 |
*/ |
| 120 |
void addOrRemoveProperty(TransactionalObject* Obj, const Property* pcProp, bool add); |
| 121 |
|
| 122 |
/** |
| 123 |
* @brief Record adding a new object to the transaction. |
| 124 |
* |
| 125 |
* @param[in] Obj The object to add. |
| 126 |
*/ |
| 127 |
void addObjectNew(TransactionalObject* Obj); |
| 128 |
|
| 129 |
/** |
| 130 |
* @brief Record removing an object from the transaction. |
| 131 |
* |
| 132 |
* @param[in] Obj The object to remove. |
| 133 |
*/ |
| 134 |
void addObjectDel(const TransactionalObject* Obj); |
| 135 |
|
| 136 |
/** |
| 137 |
*@brief Record changing an object in the transaction. |
| 138 |
* |
| 139 |
* @param[in] Obj The object to change. |
| 140 |
* @param[in] Prop The property that is changed. |
| 141 |
*/ |
| 142 |
void addObjectChange(const TransactionalObject* Obj, const Property* Prop); |
| 143 |
|
| 144 |
private: |
| 145 |
void changeProperty(TransactionalObject* Obj, |
| 146 |
std::function<void(TransactionObject* to)> changeFunc); |
| 147 |
|
| 148 |
private: |
| 149 |
int transID; |
| 150 |
using Info = std::pair<const TransactionalObject*, TransactionObject*>; |
| 151 |
bmi::multi_index_container< |
| 152 |
Info, |
| 153 |
bmi::indexed_by< |
| 154 |
bmi::sequenced<>, |
| 155 |
bmi::hashed_unique<bmi::member<Info, const TransactionalObject*, &Info::first>>>> |
| 156 |
_Objects; |
| 157 |
}; |
| 158 |
|
| 159 |
/** |
| 160 |
* @brief Class that represents an entry for an object in a Transaction. |
| 161 |
* |
| 162 |
* This class is used to store the information about the object and its |
| 163 |
* properties. It should not be confused with the TransactionalObject class |
| 164 |
* that is a base class that contains functionality for a DocumentObject |
| 165 |
* regarding transactions. |
| 166 |
*/ |
| 167 |
class AppExport TransactionObject: public Base::Persistence |
| 168 |
{ |
| 169 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 170 |
|
| 171 |
public: |
| 172 |
TransactionObject(); |
| 173 |
|
| 174 |
~TransactionObject() override; |
| 175 |
|
| 176 |
/** |
| 177 |
* @brief Apply the transaction that adds a new object to the document. |
| 178 |
* |
| 179 |
* @param[in,out] doc The document to apply the transaction to. |
| 180 |
* @param[in,out] obj The object that is added in the transaction. |
| 181 |
*/ |
| 182 |
virtual void applyNew(Document& doc, TransactionalObject* obj); |
| 183 |
|
| 184 |
/** |
| 185 |
* @brief Apply the transaction that removes an object from the document. |
| 186 |
* |
| 187 |
* @param[in,out] doc The document to apply the transaction to. |
| 188 |
* @param[in,out] obj The object that is removed in the transaction. |
| 189 |
*/ |
| 190 |
virtual void applyDel(Document& doc, TransactionalObject* obj); |
| 191 |
|
| 192 |
/** |
| 193 |
* @brief Apply the transaction that changes an object in the document. |
| 194 |
* |
| 195 |
* @param[in,out] doc The document to apply the transaction to. |
| 196 |
* @param[in,out] obj The object that is changed in the transaction. |
| 197 |
* @param[in] forward If true, apply the transaction; otherwise, undo it. |
| 198 |
*/ |
| 199 |
virtual void applyChn(Document& doc, TransactionalObject* obj, bool forward); |
| 200 |
|
| 201 |
/** |
| 202 |
* @brief Set the property of the object that is affected by the transaction. |
| 203 |
* |
| 204 |
* @param[in] prop The property that is affected by the transaction. |
| 205 |
*/ |
| 206 |
void setProperty(const Property* prop); |
| 207 |
|
| 208 |
/** |
| 209 |
* @brief Rename a property. |
| 210 |
* |
| 211 |
* @param[in] pcProp The property with the new name. |
| 212 |
* @param[in] oldName The old name of the property. |
| 213 |
*/ |
| 214 |
void renameProperty(const Property* pcProp, const char* oldName); |
| 215 |
|
| 216 |
/** |
| 217 |
* @brief Add or remove a property from the object. |
| 218 |
* |
| 219 |
* @param[in] prop The property to add or remove. |
| 220 |
* @param[in] add If true, add the property; otherwise, remove it. |
| 221 |
*/ |
| 222 |
void addOrRemoveProperty(const Property* prop, bool add); |
| 223 |
|
| 224 |
unsigned int getMemSize() const override; |
| 225 |
void Save(Base::Writer& writer) const override; |
| 226 |
void Restore(Base::XMLReader& reader) override; |
| 227 |
|
| 228 |
friend class Transaction; |
| 229 |
|
| 230 |
protected: |
| 231 |
/// The status of the transaction object. |
| 232 |
enum Status |
| 233 |
{ |
| 234 |
New, ///< A new object is added to the document. |
| 235 |
Del, ///< An object is deleted from the document. |
| 236 |
Chn ///< An object is changed in the document. |
| 237 |
} status {New}; |
| 238 |
|
| 239 |
/// Struct to maintain property information. |
| 240 |
struct PropData: DynamicProperty::PropData |
| 241 |
{ |
| 242 |
Base::Type propertyType; |
| 243 |
const Property* propertyOrig = nullptr; |
| 244 |
// for property renaming |
| 245 |
std::string nameOrig; |
| 246 |
}; |
| 247 |
|
| 248 |
/// A map to maintain the properties of the object. |
| 249 |
std::unordered_map<int64_t, PropData> _PropChangeMap; |
| 250 |
|
| 251 |
/// The name of the object in the document. |
| 252 |
std::string _NameInDocument; |
| 253 |
}; |
| 254 |
|
| 255 |
/** |
| 256 |
* @brief Class that represents an entry for a document object in a transaction. |
| 257 |
*/ |
| 258 |
class AppExport TransactionDocumentObject: public TransactionObject |
| 259 |
{ |
| 260 |
TYPESYSTEM_HEADER_WITH_OVERRIDE(); |
| 261 |
|
| 262 |
public: |
| 263 |
TransactionDocumentObject(); |
| 264 |
|
| 265 |
~TransactionDocumentObject() override; |
| 266 |
|
| 267 |
void applyNew(Document& Doc, TransactionalObject* pcObj) override; |
| 268 |
void applyDel(Document& Doc, TransactionalObject* pcObj) override; |
| 269 |
}; |
| 270 |
|
| 271 |
/** |
| 272 |
* @brief Class that represents a factory for creating transaction objects. |
| 273 |
* |
| 274 |
* This class is used to create transaction objects for different types of |
| 275 |
* transactions. |
| 276 |
*/ |
| 277 |
class AppExport TransactionFactory |
| 278 |
{ |
| 279 |
public: |
| 280 |
/// Get the singleton instance of the TransactionFactory. |
| 281 |
static TransactionFactory& instance(); |
| 282 |
|
| 283 |
/// Destruct the singleton instance of the TransactionFactory. |
| 284 |
static void destruct(); |
| 285 |
|
| 286 |
/** |
| 287 |
* @brief Create a transaction object for the given type. |
| 288 |
* |
| 289 |
* @param[in] type The type of the transaction object to create. |
| 290 |
* @return A pointer to the created transaction object. |
| 291 |
*/ |
| 292 |
TransactionObject* createTransaction(const Base::Type& type) const; |
| 293 |
|
| 294 |
/** |
| 295 |
* @brief Add a producer for a transaction object. |
| 296 |
* |
| 297 |
* @param[in] type The type id of the transaction object. |
| 298 |
* @param[in,out] producer The producer to add. |
| 299 |
*/ |
| 300 |
void addProducer(const Base::Type& type, Base::AbstractProducer* producer); |
| 301 |
|
| 302 |
private: |
| 303 |
static TransactionFactory* self; |
| 304 |
std::map<Base::Type, Base::AbstractProducer*> producers; |
| 305 |
|
| 306 |
TransactionFactory() = default; |
| 307 |
~TransactionFactory() = default; |
| 308 |
}; |
| 309 |
|
| 310 |
/** |
| 311 |
* @brief Class that represents a producer for creating transaction objects. |
| 312 |
* |
| 313 |
* @tparam CLASS The class of the transaction object to produce. |
| 314 |
*/ |
| 315 |
template<class CLASS> |
| 316 |
class TransactionProducer: public Base::AbstractProducer |
| 317 |
{ |
| 318 |
public: |
| 319 |
/** |
| 320 |
* @brief Construct a transaction producer. |
| 321 |
* |
| 322 |
* @param[in] type The type of the transaction object. |
| 323 |
*/ |
| 324 |
explicit TransactionProducer(const Base::Type& type) |
| 325 |
{ |
| 326 |
TransactionFactory::instance().addProducer(type, this); |
| 327 |
} |
| 328 |
|
| 329 |
~TransactionProducer() override = default; |
| 330 |
|
| 331 |
/// Creates an instance of the specified transaction object. |
| 332 |
void* Produce() const override |
| 333 |
{ |
| 334 |
return (new CLASS); |
| 335 |
} |
| 336 |
}; |
| 337 |
|
| 338 |
} // namespace App |
| 339 |
|
| 340 |
#endif // APP_TRANSACTION_H |