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 "../concurrency/concurrency.hpp"
9#include "../utility/utility.hpp"
10#include "../macros.hpp"
11
12
13
14namespace hi::inline v1 {
15template<typename>
16class observer;
17
18template<typename T>
20public:
21 using value_type = T;
23
24 ~observable_value() = default;
25
28 constexpr observable_value() noexcept : _rcu()
29 {
30 _rcu.emplace(value_type{});
31 }
32
37 template<typename... Args>
38 constexpr observable_value(Args&&...args) noexcept : _rcu()
39 {
40 _rcu.emplace(std::forward<Args>(args)...);
41 }
42
44 [[nodiscard]] void const *read() const noexcept override
45 {
46 return _rcu.get();
47 }
48
49 [[nodiscard]] void *copy(void const *ptr) const noexcept override
50 {
51 return _rcu.copy(static_cast<value_type const *>(ptr));
52 }
53
54 void commit(void *ptr) noexcept override
55 {
56 _rcu.commit(static_cast<value_type *>(ptr));
57 }
58
59 void abort(void *ptr) const noexcept override
60 {
61 _rcu.abort(static_cast<value_type *>(ptr));
62 }
63
64 void read_lock() const noexcept override
65 {
66 _rcu.lock();
67 }
68
69 void read_unlock() const noexcept override
70 {
71 _rcu.unlock();
72 }
73
74 void write_lock() const noexcept override
75 {
76 _write_mutex.lock();
77 read_lock();
78 }
79
80 void write_unlock() const noexcept override
81 {
82 read_unlock();
83 _write_mutex.unlock();
84 }
86private:
87 rcu<value_type> _rcu;
88 mutable unfair_mutex _write_mutex;
89};
90
91}
@ read_lock
Lock the file for reading, i.e. shared-lock.
@ write_lock
Lock the file for writing, i.e. exclusive-lock.
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
An abstract observable object.
Definition observable.hpp:29
Definition observable_value.hpp:19
constexpr observable_value(Args &&...args) noexcept
Construct the shared state and initialize the value.
Definition observable_value.hpp:38
constexpr observable_value() noexcept
Construct the shared state and default initialize the value.
Definition observable_value.hpp:28
T abort(T... args)