HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
shared_state.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "observed.hpp"
8#include "observer_intf.hpp"
9#include "../utility/utility.hpp"
10#include "../concurrency/thread.hpp" // XXX #616
11#include "../macros.hpp"
12#include <memory>
13
14hi_export_module(hikogui.observer : shared_state);
15
16hi_export namespace hi::inline v1 {
17
40template<typename T>
42public:
43 using value_type = T;
44
45 ~shared_state() = default;
46 constexpr shared_state(shared_state const&) noexcept = default;
47 constexpr shared_state(shared_state&&) noexcept = default;
48 constexpr shared_state& operator=(shared_state const&) noexcept = default;
49 constexpr shared_state& operator=(shared_state&&) noexcept = default;
50
55 template<typename... Args>
56 constexpr shared_state(Args&&...args) noexcept :
57 _pimpl(std::make_shared<observed<value_type>>(std::forward<Args>(args)...))
58 {
59 }
60
61 observer<value_type> observer() const & noexcept
62 {
63 return ::hi::observer<value_type>(_pimpl);
64 }
65
66 // clang-format off
72 [[nodiscard]] auto sub(auto const& index) const& noexcept
73 requires requires { observer().sub(index); }
74 {
75 return observer().sub(index);
76 }
77 // clang-format on
78
85 template<fixed_string Name>
86 [[nodiscard]] auto sub() const& noexcept
87 {
88 return observer().template sub<Name>();
89 }
90
91private:
93};
94
95}
Functions and types for accessing operating system threads.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Shared state of an application.
Definition shared_state.hpp:41
constexpr shared_state(Args &&...args) noexcept
Construct the shared state and initialize the value.
Definition shared_state.hpp:56
auto sub() const &noexcept
Get a observer to a member variable of the value.
Definition shared_state.hpp:86
auto sub(auto const &index) const &noexcept
Get a observer to a sub-object of value accessed by the index operator.
Definition shared_state.hpp:72
T make_shared(T... args)