crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
DatabaseLock.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2021 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * DatabaseLock.hpp
24  *
25  * Template class for safe in-scope database locks.
26  *
27  * Created on: Mar 7, 2019
28  * Author: ans
29  */
30 
31 #ifndef WRAPPER_DATABASELOCK_HPP_
32 #define WRAPPER_DATABASELOCK_HPP_
33 
34 #include <functional> // std::function
35 #include <string> // std::string
36 #include <utility> // std::move
37 
38 namespace crawlservpp::Wrapper {
39 
41 
53  template<class DB>
54  class DatabaseLock {
55  // for convenience
56  using IsRunningCallback = std::function<bool()>;
57 
58  public:
59 
62 
64 
89  DatabaseLock(DB& db, const std::string& lockName, IsRunningCallback isRunningCallback)
90  : ref(db), name(lockName), locked(false) {
91  this->ref.addDatabaseLock(this->name, isRunningCallback);
92 
93  this->locked = true;
94  }
95 
97 
105  virtual ~DatabaseLock() {
106  if(this->locked) {
107  this->ref.removeDatabaseLock(this->name);
108 
109  this->locked = false;
110  }
111  }
112 
116 
118 
122  [[nodiscard]] bool isActive() const noexcept {
123  return this->locked;
124  }
125 
127 
131 
134  DatabaseLock(DatabaseLock&) = delete;
135 
137  DatabaseLock& operator=(DatabaseLock&) = delete;
138 
140 
151  DatabaseLock(DatabaseLock&& other) noexcept
152  : ref(other.ref), name(other.name), locked(other.locked) {
153  other.locked = false;
154  }
155 
157 
182  DatabaseLock& operator=(DatabaseLock&& other) noexcept {
183  if(&other != this) {
184  if(this->locked) {
185  this->ref.removeDatabaseLock(this->name);
186 
187  this->locked = false;
188  }
189 
190  this->ref = std::move(other.ref);
191  this->name = std::move(other.name);
192  this->locked = other.locked;
193  }
194 
195  return *this;
196  }
197 
199 
200  private:
201  // internal reference to a database connection
202  DB& ref;
203 
204  // internal lock state
205  std::string name;
206  bool locked;
207  };
208 
209 } /* namespace crawlservpp::Wrapper */
210 
211 #endif /* WRAPPER_DATABASELOCK_HPP_ */
DatabaseLock(DatabaseLock &&other) noexcept
Move constructor.
Definition: DatabaseLock.hpp:151
DatabaseLock & operator=(DatabaseLock &&other) noexcept
Move assignment operator.
Definition: DatabaseLock.hpp:182
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
bool isActive() const noexcept
Checks the status of the database lock.
Definition: DatabaseLock.hpp:122
virtual ~DatabaseLock()
Destructor unlocking the database.
Definition: DatabaseLock.hpp:105
DatabaseLock(DB &db, const std::string &lockName, IsRunningCallback isRunningCallback)
Constructor locking the database after waiting for another lock if necessary.
Definition: DatabaseLock.hpp:89
Template class for safe in-scope database locks.
Definition: DatabaseLock.hpp:54
DatabaseLock & operator=(DatabaseLock &)=delete
Deleted copy assignment operator.