HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
observable_value.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.hpp"
8#include "rcu.hpp"
9#include "unfair_mutex.hpp"
10
11namespace hi::inline v1 {
12template<typename>
13class observer;
14
15template<typename T>
16class observable_value final : public observable {
17public:
18 using value_type = T;
20
21 ~observable_value() = default;
22
25 constexpr observable_value() noexcept : _rcu()
26 {
27 _rcu.emplace(value_type{});
28 }
29
34 template<typename... Args>
35 constexpr observable_value(Args&&...args) noexcept : _rcu()
36 {
37 _rcu.emplace(std::forward<Args>(args)...);
38 }
39
41 [[nodiscard]] void const *read() const noexcept override
42 {
43 return _rcu.get();
44 }
45
46 [[nodiscard]] void *copy(void const *ptr) const noexcept override
47 {
48 return _rcu.copy(static_cast<value_type const *>(ptr));
49 }
50
51 void commit(void *ptr) noexcept override
52 {
53 _rcu.commit(static_cast<value_type *>(ptr));
54 }
55
56 void abort(void *ptr) const noexcept override
57 {
58 _rcu.abort(static_cast<value_type *>(ptr));
59 }
60
61 void read_lock() const noexcept override
62 {
63 _rcu.lock();
64 }
65
66 void read_unlock() const noexcept override
67 {
68 _rcu.unlock();
69 }
70
71 void write_lock() const noexcept override
72 {
73 _write_mutex.lock();
74 read_lock();
75 }
76
77 void write_unlock() const noexcept override
78 {
79 read_unlock();
80 _write_mutex.unlock();
81 }
83private:
84 rcu<value_type> _rcu;
85 mutable unfair_mutex _write_mutex;
86};
87
88}
DOXYGEN BUG.
Definition algorithm.hpp:15
@ read_lock
Lock the file for reading, i.e. shared-lock.
@ write_lock
Lock the file for writing, i.e. exclusive-lock.
@ read
Allow read access to a file.
An abstract observable object.
Definition observable.hpp:28
Definition observable_value.hpp:16
constexpr observable_value(Args &&...args) noexcept
Construct the shared state and initialize the value.
Definition observable_value.hpp:35
constexpr observable_value() noexcept
Construct the shared state and default initialize the value.
Definition observable_value.hpp:25
T abort(T... args)