25 #ifndef COMMON_UTILS_VALUE_LATCH_H
26 #define COMMON_UTILS_VALUE_LATCH_H
32 #include <condition_variable>
44 void set(
const T& value);
67 T
get(
const unsigned int timeoutMs);
78 std::unique_lock<std::mutex> lock(mMutex);
82 mValue.reset(
new T(value));
83 mCondition.notify_one();
89 std::unique_lock<std::mutex> lock(mMutex);
93 mValue.reset(
new T(std::move(value)));
94 mCondition.notify_one();
100 std::unique_lock<std::mutex> lock(mMutex);
101 mCondition.wait(lock, [
this]() {
104 std::unique_ptr<T> retValue(std::move(mValue));
105 return T(std::move(*retValue));
108 template <
typename T>
111 std::unique_lock<std::mutex> lock(mMutex);
112 if (mCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs), [
this]() {
115 std::unique_ptr<T> retValue(std::move(mValue));
116 return T(std::move(*retValue));
118 throw UtilsException(
"Timeout occured");
124 #endif // COMMON_UTILS_VALUE_LATCH_H
std::unique_ptr< T > mValue
Definition: value-latch.hpp:72
Base class for exceptions in utils.
Definition: exception.hpp:37
T get()
Waits until set() is called, then set value is moved to caller.
Definition: value-latch.hpp:98
std::condition_variable mCondition
Definition: value-latch.hpp:71
Exceptions for the server.
void set(const T &value)
Assigns value to kept variable and sets Latch.
Definition: value-latch.hpp:76
Definition: value-latch.hpp:37
std::mutex mMutex
Definition: value-latch.hpp:70