File indexing completed on 2024-04-28 16:30:34

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 #ifndef SKGTRANSACTIONMNG_H
0007 #define SKGTRANSACTIONMNG_H
0008 /** @file
0009  * This file defines classes SKGTransactionMng and a macro.
0010  *
0011  * @author Stephane MANKOWSKI / Guillaume DE BURE
0012  */
0013 #include "skgdefine.h"
0014 #include "skgtraces.h"
0015 #include <qstring.h>
0016 
0017 /**
0018  * @def SKGBEGINTRANSACTION(Document, Name, Error)
0019  * Macro to start a new transaction
0020  * Example of usage:
0021  * @code
0022  * { // BEGIN OF THE TRANSACTION SCOPE
0023  *  // Error management of the scope
0024  *  SKGError err;
0025  *
0026  *  // Begin transaction
0027  *   SKGBEGINTRANSACTION(Your_SKGDocument, QStringLiteral("The_Name_Of_The_Transaction"), err)
0028  *
0029  *  // Check the error
0030  *  if(!err)
0031  *  {
0032  *   // You code here
0033  *   // At the end the err variable must contain the result of your operation
0034  *  }
0035  *
0036  *  // A commit or rollback is done here because the scope is close
0037  * } // END OF THE TRANSACTION
0038  * @endcode
0039  */
0040 #define SKGBEGINTRANSACTION(Document, Name, Error) \
0041     SKGTransactionMng TOKENPASTE2(transaction_, __LINE__)(&(Document), Name, &(Error));
0042 
0043 /**
0044  * @def SKGBEGINLIGHTTRANSACTION(Document, Name, Error)
0045  * Macro to start a new light transaction (without views computation)
0046  */
0047 #define SKGBEGINLIGHTTRANSACTION(Document, Name, Error) \
0048     SKGTransactionMng transactionlight_(&(Document), Name, &(Error), 1, false);
0049 /**
0050  * @def SKGBEGINPROGRESSTRANSACTION(Document, Name, Error)
0051  * Macro to start a new transaction
0052  * Example of usage:
0053  * @code
0054  * { // BEGIN OF THE TRANSACTION SCOPE
0055  *  // Error management of the scope
0056  *  SKGError err;
0057  *
0058  *  // Begin transaction
0059  *   SKGBEGINPROGRESSTRANSACTION(Your_SKGDocument, "The_Name_Of_The_Transaction", err, 5)
0060  *
0061  *  // Check the error
0062  *  if(!err)
0063  *  {
0064  *   // You code here
0065  *   // At the end the err variable must contain the result of your operation
0066  *  Your_SKGDocument.stepForward(1);
0067  *  …
0068  *  Your_SKGDocument.stepForward(2);
0069  *  …
0070  *  Your_SKGDocument.stepForward(3);
0071  *  …
0072  *  }
0073  *
0074  *  // A commit or rollback is done here because the scope is close
0075  * } // END OF THE TRANSACTION
0076  * @endcode
0077  */
0078 #define SKGBEGINPROGRESSTRANSACTION(Document, Name, Error, Nb) \
0079     SKGTransactionMng TOKENPASTE2(transaction_, __LINE__)(&(Document), Name, &(Error), Nb);
0080 
0081 /**
0082  * @def SKGBEGINLIGHTPROGRESSTRANSACTION(Document, Name, Error)
0083  * Macro to start a new light transaction (without views computation)
0084  */
0085 #define SKGBEGINLIGHTPROGRESSTRANSACTION(Document, Name, Error, Nb) \
0086     SKGTransactionMng TOKENPASTE2(transactionlight_, __LINE__)(&(Document), Name, &(Error), Nb, false);
0087 
0088 /**
0089  * @def SKGENDTRANSACTION(Document,  Error)
0090  * End correctly a transaction
0091  */
0092 #define SKGENDTRANSACTION(Document,  Error) \
0093     if (!(Error)) {(Error) = (Document)->endTransaction(true);} \
0094     else {(Document)->endTransaction(false);}
0095 
0096 class SKGError;
0097 class SKGDocument;
0098 
0099 /**
0100 * Facilitate the transaction management
0101 */
0102 class SKGBASEMODELER_EXPORT SKGTransactionMng
0103 {
0104 public:
0105     /**
0106      * Constructor.
0107      * This class must be used with the macro
0108      * @see SKGBEGINTRANSACTION
0109      * @param iDocument The parent document of the transaction
0110      * @param iName The message of the transaction
0111      * @param iError A pointer of the error object of the calling scope
0112      * @param iNbStep the number of step in this transaction.
0113      * @param iRefreshViews at the end of the transaction, computed views will be recomputed.
0114      * It is used to call the progress callback.
0115      * @see The SKGError
0116      */
0117     explicit SKGTransactionMng(SKGDocument* iDocument, const QString& iName, SKGError* iError, int iNbStep = 1, bool iRefreshViews = true);
0118 
0119     /**
0120      * Destructor
0121      */
0122     virtual ~SKGTransactionMng();
0123 
0124 private:
0125     Q_DISABLE_COPY(SKGTransactionMng)
0126 
0127     SKGError* m_error;
0128     SKGDocument* m_parentDocument;
0129     bool m_errorInBeginTransaction;
0130 };
0131 
0132 #endif