PIProtectedVariable - user now can`t mistake

PIHTTPServer improvements
This commit is contained in:
2024-11-18 11:11:19 +03:00
parent 3483edbd76
commit c9a5ddd89f
8 changed files with 169 additions and 53 deletions

View File

@@ -32,6 +32,28 @@
template<typename T>
class PIP_EXPORT PIProtectedVariable {
public:
//! \~english
//! \~russian
class PIP_EXPORT Pointer {
friend class PIProtectedVariable<T>;
public:
Pointer(const Pointer & v): pv(v.pv), counter(v.counter + 1) {}
~Pointer() {
if (counter == 0) pv.mutex.unlock();
}
T * operator->() { return &pv.var; }
T & operator*() { return pv.var; }
private:
Pointer() = delete;
Pointer(PIProtectedVariable<T> & v): pv(v) {}
PIProtectedVariable<T> & pv;
int counter = 0;
};
//! \~english Sets value to \"v\"
//! \~russian Устанавливает значение как \"v\"
void set(T v) {
@@ -39,6 +61,13 @@ public:
var = std::move(v);
}
//! \~english Lock mutex and returns reference wrapper of value. Unlock on variable destructor.
//! \~russian Блокирует мьютекс и возвращает класс-обертку на значение. Разблокирует в деструкторе переменной.
Pointer getRef() {
mutex.lock();
return Pointer(*this);
}
//! \~english Returns copy of value
//! \~russian Возвращает копию значения
T get() const {
@@ -46,17 +75,6 @@ public:
return var;
}
//! \~english Lock mutex and returns reference of value
//! \~russian Блокирует мьютекс и возвращает ссылку на значение
T & lock() {
mutex.lock();
return var;
}
//! \~english Unlock mutex
//! \~russian Разблокирует мьютекс
void unlock() { mutex.unlock(); }
//! \~english Sets value to \"v\"
//! \~russian Устанавливает значение как \"v\"
PIProtectedVariable<T> & operator=(T v) {