Line data Source code
1 : #ifndef ASYNCOPERATION_H
2 : #define ASYNCOPERATION_H
3 :
4 : #include <QObject>
5 : #include <QSqlDatabase>
6 : #include <QSqlQuery>
7 : #include <QSqlError>
8 :
9 : #include "../FangObject.h"
10 :
11 : #define FANG_BACKGROUND_CHECK do { if (shouldTerminate()) return; } while(0)
12 :
13 : class OperationManager;
14 :
15 : /*!
16 : \brief Base class for asynchronous operations. Signals indicate completion.
17 :
18 : Intended to be used for network requests, etc.
19 : */
20 : class AsyncOperation : public FangObject
21 : {
22 : Q_OBJECT
23 :
24 : public:
25 : enum Priority {
26 : BACKGROUND, // Will get queued up in the background
27 : IMMEDIATE // Schedule for the immediate future
28 : };
29 :
30 : explicit AsyncOperation(Priority priority, OperationManager* parent);
31 : virtual ~AsyncOperation() = default;
32 :
33 : protected slots:
34 :
35 : /*!
36 : \brief Call this to report an error. Automatically emits finished()
37 : \param errorString The text that will be logged along with the error.
38 : */
39 : void reportError(const QString& errorString);
40 :
41 : /*!
42 : \brief Pass any QObjects that might be deleted during the run into this method.
43 : It will cause the operation to be canceled gracefully, emitting finished().
44 : See also: FANG_BACKGROUND_CHECK macro.
45 : \param object
46 : */
47 : void requireObject(QObject* object);
48 :
49 : /*!
50 : \brief Used by the FANG_BACKGROUND_CHECK macro.
51 : \return
52 : */
53 0 : bool shouldTerminate() { return terminate; }
54 :
55 : signals:
56 :
57 : /*!
58 : \brief Indicates that the operation is complete and may be freed.
59 : */
60 : void finished(AsyncOperation* myself);
61 :
62 : public slots:
63 :
64 : /*!
65 : \brief Runs the operation. When complete, finished() must be emitted.
66 : */
67 : virtual void execute() = 0;
68 :
69 : /*!
70 : \return Priority of this operation.
71 : */
72 0 : inline Priority getPriority() { return priority; }
73 :
74 : /*!
75 : \return Returns true if there was an error, else false.
76 : */
77 0 : inline bool isError() { return error; }
78 :
79 : /*!
80 : \brief If your operation needs to invoke other operations (yo dawg, I heard you like
81 : operations) then you'll want to use this.
82 : \return The operation manager used for getting/setting operations.
83 : */
84 0 : OperationManager* getOperationManager() { return operationManager; }
85 :
86 : /*!
87 : \brief Returns the database connection.
88 : */
89 : QSqlDatabase db();
90 :
91 : protected:
92 :
93 : /*!
94 : \brief Call this to report a SQL error. Automatically emits finished()
95 : \param query
96 : \param errorString The text that will be logged along with the error.
97 : */
98 : void reportSQLError(const QSqlQuery& query, const QString& errorString);
99 :
100 : private slots:
101 :
102 : // Called when a required object is destroyed.
103 : void onRequiredQObjectDestroyed(QObject* object);
104 :
105 : private:
106 : OperationManager* operationManager;
107 : Priority priority;
108 : bool error;
109 : bool terminate;
110 : };
111 :
112 : #endif // ASYNCOPERATION_H
|