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 "../macros.hpp"
10#include <memory>
11
12namespace hi::inline v1 {
13
36template<typename T>
38public:
39 using value_type = T;
40
41 ~shared_state() = default;
42 constexpr shared_state(shared_state const&) noexcept = default;
43 constexpr shared_state(shared_state&&) noexcept = default;
44 constexpr shared_state& operator=(shared_state const&) noexcept = default;
45 constexpr shared_state& operator=(shared_state&&) noexcept = default;
46
51 template<typename... Args>
52 constexpr shared_state(Args&&...args) noexcept :
53 _pimpl(std::make_shared<observable_value<value_type>>(std::forward<Args>(args)...))
54 {
55 }
56
57 observer<value_type> observer() const & noexcept
58 {
59 return ::hi::observer<value_type>(_pimpl);
60 }
61
62 // clang-format off
68 [[nodiscard]] auto get(auto const& index) const& noexcept
69 requires requires { observer().get(index); }
70 {
71 return observer().get(index);
72 }
73 // clang-format on
74
81 template<fixed_string Name>
82 [[nodiscard]] auto get() const& noexcept
83 {
84 return observer().get<Name>();
85 }
86
87private:
89};
90
91}
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
A observer pointing to the whole or part of a observable.
Definition observer.hpp:26
auto get(auto const &index) const noexcept
Create a sub-observer by indexing into the value.
Definition observer.hpp:517
Definition observable_value.hpp:19
Shared state of an application.
Definition shared_state.hpp:37
constexpr shared_state(Args &&...args) noexcept
Construct the shared state and initialize the value.
Definition shared_state.hpp:52
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:68
auto get() const &noexcept
Get a observer to a member variable of the value.
Definition shared_state.hpp:82
T make_shared(T... args)