5#ifndef MITTELIB_OBSERVABLE_HPP
6#define MITTELIB_OBSERVABLE_HPP
21 class observable_cref;
26 using read_value_type = std::conditional_t<std::is_trivially_copyable_v<T>, T, T
const &>;
46 [[nodiscard]] std::size_t
append_observer(std::pair<read_observer_fn, write_observer_fn> observers);
50 mutable std::recursive_mutex
_lock;
51 std::vector<std::pair<read_observer_fn, write_observer_fn>>
_observers;
93 std::size_t
_idx = std::numeric_limits<std::size_t>::max();
124 auto guard = std::scoped_lock{_lock};
125 _observers.template emplace_back(std::move(observers));
126 return _observers.size() - 1;
131 auto guard = std::scoped_lock{_lock};
132 _observers.at(idx) = {
nullptr,
nullptr};
137 auto guard = std::scoped_lock{_lock};
138 for (
auto &[r_fn, w_fn] : _observers) {
148 auto guard = std::scoped_lock{_lock};
149 for (
auto &[r_fn, w_fn] : _observers) {
154 if constexpr (std::is_trivially_copyable_v<T>) {
157 _value = std::move(t);
169 return {*
this, std::move(read_fn), std::move(write_fn)};
174 if (_obs_value !=
nullptr and _idx < std::numeric_limits<std::size_t>::max()) {
175 _obs_value->delete_observer(_idx);
176 _idx = std::numeric_limits<std::size_t>::max();
177 _obs_value =
nullptr;
183 *
this = std::move(other);
188 std::swap(_obs_value, other._obs_value);
189 std::swap(_idx, other._idx);
196 _idx{obs_value.append_observer({std::move(read_fn), std::move(write_fn)})} {}
201 _idx{obs_value_ref._ref.append_observer({std::move(read_fn), std::move(write_fn)})} {}
233 return const_cast<observable<T> &
>(_ref).do_observe(std::move(read_fn), std::move(write_fn));
Definition observable.hpp:55
observable_cref(observable< T > const &ref)
Definition observable.hpp:215
typename observable< T >::read_value_type read_value_type
Definition observable.hpp:62
observable< T > const & _ref
Definition observable.hpp:57
Definition observable.hpp:70
observe< T > do_observe(read_observer_fn read_fn, write_observer_fn write_fn)
Definition observable.hpp:232
typename observable< T >::read_observer_fn read_observer_fn
Definition observable.hpp:77
observable_ref< T > & operator=(T t)
Definition observable.hpp:226
typename observable< T >::write_observer_fn write_observer_fn
Definition observable.hpp:78
observable_ref(observable< T > &ref)
Definition observable.hpp:218
typename observable_cref< T >::read_value_type read_value_type
Definition observable.hpp:76
Definition observable.hpp:24
std::recursive_mutex _lock
Definition observable.hpp:50
std::size_t append_observer(std::pair< read_observer_fn, write_observer_fn > observers)
Definition observable.hpp:123
observable_cref< T > cref() const
Definition observable.hpp:210
T _value
Definition observable.hpp:49
observable & operator=(T t)
Definition observable.hpp:147
observable_ref< T > ref()
Definition observable.hpp:205
std::function< void(read_value_type, read_value_type)> write_observer_fn
Definition observable.hpp:28
void delete_observer(std::size_t idx)
Definition observable.hpp:130
std::function< void(read_value_type)> read_observer_fn
Definition observable.hpp:27
observable()
Definition observable.hpp:120
observe< T > do_observe(read_observer_fn read_fn, write_observer_fn write_fn)
Definition observable.hpp:168
std::conditional_t< std::is_trivially_copyable_v< T >, T, T const & > read_value_type
Definition observable.hpp:26
std::vector< std::pair< read_observer_fn, write_observer_fn > > _observers
Definition observable.hpp:51
Definition observable.hpp:91
observe & operator=(observe const &)=delete
observe(observe const &)=delete
~observe()
Definition observable.hpp:163
observable< T > * _obs_value
Definition observable.hpp:92
void deregister()
Definition observable.hpp:173
std::size_t _idx
Definition observable.hpp:93