8#include "../utility/utility.hpp"
9#include "../dispatch/dispatch.hpp"
11#include "../macros.hpp"
19hi_export_module(hikogui.observer : observer_intf);
21hi_export
namespace hi {
inline namespace v1 {
35 using notifier_type = notifier<
void(value_type)>;
36 using callback_type = notifier_type::callback_type;
37 using awaiter_type = notifier_type::awaiter_type;
47 using reference = value_type &;
48 using const_reference = value_type
const &;
49 using pointer = value_type *;
50 using const_pointer = value_type
const *;
60 hi_axiom_not_null(_ptr);
61 if (_original_value) {
62 if constexpr (
requires(value_type
const& a, value_type
const& b) { a != b; }) {
63 if (*_original_value != *_ptr) {
78 _ptr(std::exchange(
other._ptr,
nullptr)),
79 _original_value(std::exchange(
other._original_value, std::nullopt))
87 _ptr = std::exchange(
other._ptr,
nullptr);
88 _original_value = std::exchange(
other._original_value, std::nullopt);
102 hi_axiom_not_null(_ptr);
136 hi_axiom_not_null(_ptr);
162 hi_axiom_not_null(_ptr);
188 hi_axiom_not_null(_ptr);
196 template<typename Rhs> \
197 decltype(auto) operator op() noexcept \
198 requires requires(value_type& a) { op a; } \
210 template<typename Rhs> \
211 auto operator op(int) noexcept \
212 requires requires(value_type& a) { a op; } \
224 template<typename Rhs> \
225 decltype(auto) operator op(Rhs const& rhs) noexcept \
226 requires requires(value_type& a, Rhs const& b) { a op b; } \
229 return (*_ptr) op rhs; \
246 template<typename Rhs> \
247 auto operator op() const noexcept \
248 requires requires(value_type const& a) { op a; } \
250 hi_axiom_not_null(_ptr); \
262 template<typename Rhs> \
263 auto operator op(Rhs const& rhs) const noexcept \
264 requires requires(value_type const& a, Rhs const& b) { a op b; } \
266 hi_axiom_not_null(_ptr); \
267 return (*_ptr) op rhs; \
285 template<
typename...
Args>
286 auto operator()(
Args &&...
args)
const noexcept
287 requires requires(value_type
const & a,
Args &&...args) { a(std::forward<Args>(
args)...); }
289 hi_axiom_not_null(_ptr);
290 return (*_ptr)(std::forward<Args>(
args)...);
293 template<
typename...
Args>
294 decltype(
auto)
operator()(
Args &&...
args)
noexcept
295 requires requires(value_type & a,
Args &&...
args) { a(std::forward<Args>(
args)...); }
298 return (*_ptr)(std::forward<Args>(
args)...);
303 template<
typename Arg>
304 auto operator[](
Arg &&
arg)
const noexcept
305 requires requires(value_type
const & a,
Arg &&
arg) { a[std::forward<Arg>(
arg)]; }
307 hi_axiom_not_null(_ptr);
308 return (*_ptr)[std::forward<Arg>(
arg)];
316 std::optional<value_type> _original_value;
320 if (
not _original_value) {
321 hi_axiom_not_null(_ptr);
322 _original_value = *_ptr;
326 friend class observer;
329 using const_reference = value_type
const &;
330 using pointer = proxy_type;
331 using const_pointer = value_type
const *;
333 constexpr ~observer() =
default;
339 template<forward_of<std::shared_ptr<hi::observed_base>> Observed>
355 template<std::convertible_to<value_type> Value>
387 _observed =
other._observed;
389 _convert =
other._convert;
392 update_state_callback();
393 _observed->notify_group_ptr(
observable_msg{_observed->get(), _path});
412 update_state_callback();
413 _observed->notify_group_ptr(
observable_msg{_observed->get(), _path});
423 _observed = std::make_shared<observed<value_type>>();
425 _convert = [](
void *base) {
428 update_state_callback();
437 return convert(_observed->get());
446 return proxy_type{
this, convert(_observed->get())};
455 template<forward_of<
void(value_type)> Func>
458 return _notifier.subscribe(std::forward<Func>(
func), flags);
461 awaiter_type
operator co_await()
const noexcept
463 return _notifier.operator
co_await();
472 requires(
requires() { std::declval<value_type>()[index]; })
474 using result_type = std::decay_t<decltype(std::declval<value_type>()[index])>;
490 template<fixed_
string Name>
503 *std::launder(
static_cast<value_type *
>(
convert_copy(base)))));
516 template<
typename Rhs>
518 requires requires (value_type &a,
Rhs &&b) { a = std::forward<Rhs>(b); }
520 *
get() = std::forward<Rhs>(rhs);
554 template<typename Rhs> \
555 observer& operator op() noexcept \
556 requires requires(value_type& a) { op a; } \
568 template<typename Rhs> \
569 auto operator op(int) noexcept \
570 requires requires(value_type& a) { a op; } \
581 template<typename Rhs> \
582 observer& operator op(Rhs const& rhs) noexcept \
583 requires requires(value_type& a, Rhs const& b) { a op b; } \
603 template<typename Rhs> \
604 auto operator op() const noexcept \
605 requires requires(value_type const& a) { op a; } \
618 template<typename Rhs> \
619 auto operator op(Rhs const& rhs) const noexcept \
620 requires requires(value_type const& a, Rhs const& b) { a op b; } \
622 return *get() op rhs; \
640 template<
typename...
Args>
641 auto operator()(
Args &&...
args)
const noexcept
642 requires requires(value_type
const & a,
Args &&...args) { a(std::forward<Args>(
args)...); }
644 return (*
get())(std::forward<Args>(
args)...);
649 template<
typename Arg>
650 auto operator[](
Arg &&
arg)
const noexcept
651 requires requires(value_type
const & a,
Arg &&
arg) { a[std::forward<Arg>(
arg)]; }
653 return (*
get())[std::forward<Arg>(
arg)];
661 path_type _path = {};
663 notifier_type _notifier;
665 value_type _debug_value;
670 template<forward_of<observed_type> ObservedBase, forward_of<path_type> Path, forward_of<
void *(
void *)> Converter>
675 _observed(std::forward<ObservedBase>(
observed_base)), _path(std::forward<Path>(path)), _convert(std::forward<Converter>(
converter)), _notifier()
677 update_state_callback();
682 _observed->notify_group_ptr(
observable_msg{_observed->get(), _path});
685 value_type *convert(
void *base)
const noexcept
687 return std::launder(
static_cast<value_type *
>(_convert(base)));
690 value_type
const *convert(
void const *base)
const noexcept
692 return std::launder(
static_cast<value_type
const *
>(_convert(
const_cast<void *
>(base))));
695 void update_state_callback()
noexcept
697 _observed.subscribe([
this](observable_msg
const&
msg) {
703 _debug_value = *convert(
msg.ptr);
705 _notifier(*convert(
msg.ptr));
710 _debug_value = *convert(_observed->get());
716 friend class observer;
733 using type = std::remove_cvref_t<T>;
747template<
typename Context,
typename Expected>
749 std::conditional_t<std::is_convertible_v<Context, observer<Expected>>, std::true_type, std::false_type> {};
752template<
typename Context,
typename Expected>
Functions and types for accessing operating system threads.
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition callback.hpp:77
Definition observed.hpp:17
std::vector< std::string > path_type
The type of the path used for notifying observers.
Definition observed.hpp:20
An abstract observed object.
Definition observed.hpp:30
Definition observed.hpp:53
A observer pointing to the whole or part of a observed_base.
Definition observer_intf.hpp:32
void reset() noexcept
Reset the observer.
Definition observer_intf.hpp:421
const_pointer get() const noexcept
Read the observed_base value.
Definition observer_intf.hpp:435
observer & operator=(Rhs &&rhs) noexcept
Assign a new value to the observed_base value.
Definition observer_intf.hpp:517
observer(Observed &&observed_base) noexcept
Create an observer from an observed_base.
Definition observer_intf.hpp:340
const_pointer operator->() const noexcept
Constant pointer-to-member of the value being observed_base.
Definition observer_intf.hpp:540
pointer operator->() noexcept
Constant pointer-to-member of the value being observed_base.
Definition observer_intf.hpp:547
constexpr observer(observer &&other) noexcept
Move construct.
Definition observer_intf.hpp:373
constexpr observer & operator=(observer &&other) noexcept
Move assign.
Definition observer_intf.hpp:404
constexpr observer(Value &&value) noexcept
Create a observer linked to an anonymous observed_base-value.
Definition observer_intf.hpp:356
pointer get() noexcept
Make a copy of the observed_base value for modification.
Definition observer_intf.hpp:444
auto sub() const noexcept
Create a sub-observer by selecting a member-variable of the value.
Definition observer_intf.hpp:491
const_reference operator*() const noexcept
Get the value being observed_base.
Definition observer_intf.hpp:526
constexpr observer & operator=(observer const &other) noexcept
Copy assign.
Definition observer_intf.hpp:385
callback< void(value_type)> subscribe(Func &&func, callback_flags flags=callback_flags::synchronous) noexcept
Subscribe a callback to this observer.
Definition observer_intf.hpp:456
constexpr observer(observer const &other) noexcept
Copy construct.
Definition observer_intf.hpp:366
constexpr observer() noexcept
Create a observer linked to an anonymous default initialized observed_base-value.
Definition observer_intf.hpp:351
observer(ObservedBase &&observed_base, Path &&path, Converter &&converter) noexcept
Construct an observer from an observed_base.
Definition observer_intf.hpp:671
auto sub(auto const &index) const noexcept
Create a sub-observer by indexing into the value.
Definition observer_intf.hpp:471
A proxy object of the observer.
Definition observer_intf.hpp:45
reference operator*() noexcept
Dereference the value.
Definition observer_intf.hpp:122
proxy_type() noexcept
Create a proxy object.
Definition observer_intf.hpp:111
pointer operator&() noexcept
Pointer dereference the value.
Definition observer_intf.hpp:173
const_pointer operator&() const noexcept
Pointer dereference the value.
Definition observer_intf.hpp:186
~proxy_type() noexcept
Commits and destruct the proxy object.
Definition observer_intf.hpp:57
pointer operator->() noexcept
Pointer dereference the value.
Definition observer_intf.hpp:147
const_pointer operator->() const noexcept
Pointer dereference the value.
Definition observer_intf.hpp:160
const_reference operator*() const noexcept
Dereference the value.
Definition observer_intf.hpp:134
proxy_type(observer *observer, value_type *ptr) noexcept
Create a proxy object.
Definition observer_intf.hpp:98
A type-trait for observer arguments.
Definition observer_intf.hpp:732
Is context a form of the expected type.
Definition type_traits.hpp:593
This selector allows access to member variable by name.
Definition type_traits.hpp:691
Definition observer_intf.hpp:753
True if T is a forwarded type of Forward.
Definition concepts.hpp:137