crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
DatabaseTryLock.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 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  * DatabaseTryLock.hpp
24  *
25  * Template class for safe in-scope database try locks.
26  *
27  * Created on: Mar 7, 2019
28  * Author: ans
29  */
30 
31 #ifndef WRAPPER_DATABASETRYLOCK_HPP_
32 #define WRAPPER_DATABASETRYLOCK_HPP_
33 
34 #include <string> // std::string
35 #include <utility> // std::move
36 
37 namespace crawlservpp::Wrapper {
38 
40 
50  template<class DB>
52  public:
53 
56 
58 
75  DatabaseTryLock(DB& db, const std::string& lockName)
76  : ref(db), name(lockName), locked(false) {
77  this->locked = this->ref.tryDatabaseLock(this->name);
78  }
79 
81 
87  virtual ~DatabaseTryLock() {
88  if(this->locked) {
89  this->ref.removeDatabaseLock(this->name);
90 
91  this->locked = false;
92  }
93  }
94 
98 
100 
104  [[nodiscard]] bool isActive() const noexcept {
105  return this->locked;
106  }
107 
109 
112 
115  DatabaseTryLock(DatabaseTryLock&) = delete;
116 
119 
121 
131  : ref(other.ref), name(other.name), locked(other.locked) {
132  other.locked = false;
133  }
134 
136 
155  if(&other != this) {
156  if(this->locked) {
157  this->ref.removeDatabaseLock(this->name);
158 
159  this->locked = false;
160  }
161 
162  this->ref = std::move(other.ref);
163  this->name = std::move(other.name);
164  this->locked = other.locked;
165  }
166 
167  return *this;
168  }
169 
171 
172  private:
173  // internal reference to a database connection
174  DB& ref;
175 
176  // internal lock state
177  std::string name;
178  bool locked;
179  };
180 
181 } /* namespace crawlservpp::Wrapper */
182 
183 #endif /* WRAPPER_DATABASETRYLOCK_HPP_ */
DatabaseTryLock(DB &db, const std::string &lockName)
Constructor locking the database if it is not already locked.
Definition: DatabaseTryLock.hpp:75
bool isActive() const noexcept
Checks the status of the database lock.
Definition: DatabaseTryLock.hpp:104
DatabaseTryLock(DatabaseTryLock &&other) noexcept
Move constructor.
Definition: DatabaseTryLock.hpp:130
Template class for safe in-scope database locks.
Definition: DatabaseTryLock.hpp:51
Namespace for RAII wrappers and Wrapper::Database.
Definition: Database.hpp:109
virtual ~DatabaseTryLock()
Destructor unlocking the database if necessary.
Definition: DatabaseTryLock.hpp:87
DatabaseTryLock & operator=(DatabaseTryLock &)=delete
Deleted copy assignment operator.
DatabaseTryLock & operator=(DatabaseTryLock &&other) noexcept
Move assignment operator.
Definition: DatabaseTryLock.hpp:154