CuteHMI - Shared Database (CuteHMI.SharedDatabase.0)
DatabaseWorker.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_DATABASEWORKER_HPP
2 #define H_EXTENSIONS_CUTEHMI_SHAREDDATABASE_0_INCLUDE_CUTEHMI_SHAREDDATABASE_DATABASEWORKER_HPP
3 
4 #include "internal/common.hpp"
5 
6 #include <cutehmi/Worker.hpp>
7 
8 #include <QSqlDatabase>
9 
10 namespace cutehmi {
11 namespace shareddatabase {
12 
16 class CUTEHMI_SHAREDDATABASE_API DatabaseWorker:
17  public QObject
18 {
19  Q_OBJECT
20 
21  public:
28  explicit DatabaseWorker(const QString & connectionName, std::function<void(QSqlDatabase & db)> task = nullptr);
29 
34  void setTask(std::function<void(QSqlDatabase & db)> task);
35 
43  virtual void job(QSqlDatabase & db);
44 
51  void wait() const;
52 
59  bool isReady() const;
60 
67  bool isWorking() const;
68 
72  void work();
73 
74  signals:
78  void started();
79 
83  void ready();
84 
88  void refused(const QString & reason);
89 
90  protected:
95  QThread * dbThread() const;
96 
104  QSqlDatabase * db();
105 
113  const QSqlDatabase * db() const;
114 
115  private slots:
116  void updateDbThread(const QString & connectionName);
117 
118  private:
119  void instantiateWorker();
120 
121  struct Members {
122  const QString connectionName;
123  std::function<void(QSqlDatabase & db)> task;
126  };
127 
128  MPtr<Members> m;
129 };
130 
131 }
132 }
133 
134 #endif
135 
136 //(c)C: Copyright © 2020, Michał Policht <michal@policht.pl>. All rights reserved.
137 //(c)C: SPDX-License-Identifier: LGPL-3.0-or-later OR MIT
138 //(c)C: This file is a part of CuteHMI.
139 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
140 //(c)C: CuteHMI 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 Lesser General Public License for more details.
141 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
142 //(c)C: Additionally, this file is licensed under terms of MIT license as expressed below.
143 //(c)C: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
144 //(c)C: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
145 //(c)C: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
QThread
std::function
QSqlDatabase
cutehmi::MPtr< Members >
QObject
cutehmi
QString
cutehmi::shareddatabase::DatabaseWorker
Database worker.
Definition: DatabaseWorker.hpp:16
std::unique_ptr