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 "observable_value.hpp"
8#include "observer.hpp"
9#include <memory>
10
11namespace hi::inline v1 {
12
35template<typename T>
37public:
38 using value_type = T;
39
40 ~shared_state() = default;
41 constexpr shared_state(shared_state const&) noexcept = default;
42 constexpr shared_state(shared_state&&) noexcept = default;
43 constexpr shared_state& operator=(shared_state const&) noexcept = default;
44 constexpr shared_state& operator=(shared_state&&) noexcept = default;
45
50 template<typename... Args>
51 constexpr shared_state(Args&&...args) noexcept :
52 _pimpl(std::make_shared<observable_value<value_type>>(std::forward<Args>(args)...))
53 {
54 }
55
56 observer<value_type> observer() const & noexcept
57 {
58 return ::hi::observer<value_type>(_pimpl);
59 }
60
61 // clang-format off
67 [[nodiscard]] auto get(auto const& index) const& noexcept
68 requires requires { observer().get(index); }
69 {
70 return observer().get(index);
71 }
72 // clang-format on
73
80 template<fixed_string Name>
81 [[nodiscard]] auto get() const& noexcept
82 {
83 return observer().get<Name>();
84 }
85
86private:
88};
89
90}
DOXYGEN BUG.
Definition algorithm.hpp:15
A observer pointing to the whole or part of a observable.
Definition observer.hpp:23
auto get(auto const &index) const noexcept
Create a sub-observer by indexing into the value.
Definition observer.hpp:514
Definition observable_value.hpp:16
Shared state of an application.
Definition shared_state.hpp:36
constexpr shared_state(Args &&...args) noexcept
Construct the shared state and initialize the value.
Definition shared_state.hpp:51
auto get(auto const &index) const &noexcept
Get a observer to a sub-object of value accessed by the index operator.
Definition shared_state.hpp:67
auto get() const &noexcept
Get a observer to a member variable of the value.
Definition shared_state.hpp:81
T make_shared(T... args)