17 #include <teiacare/sdk/non_copyable.hpp>
18 #include <teiacare/sdk/non_moveable.hpp>
24 #include <type_traits>
44 template <std::equality_comparable T>
55 explicit observable(
const T& t,
const std::function<
void(T)>& callback)
58 , _is_callback_enabled{true}
71 explicit observable(T&& t,
const std::function<
void(T)>& callback)
72 : _value{std::forward<T>(t)}
74 , _is_callback_enabled{true}
93 std::lock_guard lock(_mutex);
94 return _is_callback_enabled.load();
103 std::lock_guard lock(_mutex);
104 _is_callback_enabled.store(is_enabled);
111 operator T
const()
const
131 std::lock_guard lock(_mutex);
144 set(std::forward<T>(t));
162 void set(
const T&
value)
164 std::lock_guard lock(_mutex);
171 if (_is_callback_enabled)
177 std::lock_guard lock(_mutex);
184 if (_is_callback_enabled)
189 mutable std::mutex _mutex;
191 const std::function<void(T)> _callback;
192 std::atomic_bool _is_callback_enabled;
Utility class to observe a value with a user provided callback.
observable & operator=(T &&t)
Update the observed value.
observable(T &&t, const std::function< void(T)> &callback)
Constructor.
~observable()=default
Destructor.
void callback_enabled(bool is_enabled)
Enable or disable the callback temporarily.
observable(const T &t, const std::function< void(T)> &callback)
Constructor.
observable & operator=(const T &t)
Update the observed value.
T value() const
Return the observed value.
bool callback_enabled() const
Check if the callback is currently enabled.