93 lines
2.4 KiB
C++
93 lines
2.4 KiB
C++
/*! \file piprotectedvariable.h
|
|
* \ingroup Thread
|
|
* \~\brief
|
|
* \~english Thread-safe variable
|
|
* \~russian Потокобезопасная переменная
|
|
*/
|
|
/*
|
|
PIP - Platform Independent Primitives
|
|
Thread-safe variable
|
|
Ivan Pelipenko peri4ko@yandex.ru, Stephan Fomenko, Andrey Bychkov work.a.b@yandex.ru
|
|
|
|
This program 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.
|
|
|
|
This program 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.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef PIPROTECTEDVARIABLE_H
|
|
#define PIPROTECTEDVARIABLE_H
|
|
|
|
#include "pimutex.h"
|
|
|
|
|
|
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) {
|
|
PIMutexLocker _ml(mutex);
|
|
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 {
|
|
PIMutexLocker _ml(mutex);
|
|
return var;
|
|
}
|
|
|
|
//! \~english Sets value to \"v\"
|
|
//! \~russian Устанавливает значение как \"v\"
|
|
PIProtectedVariable<T> & operator=(T v) {
|
|
set(std::move(v));
|
|
return *this;
|
|
}
|
|
|
|
|
|
private:
|
|
mutable PIMutex mutex;
|
|
T var;
|
|
};
|
|
|
|
|
|
#endif
|