HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
preferences.hpp
1// Copyright Take Vos 2020-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#include "datum.hpp"
6#include "log.hpp"
7#include "jsonpath.hpp"
8#include "observer.hpp"
9#include "pickle.hpp"
10#include <typeinfo>
11#include <filesystem>
12
13#pragma once
14
15namespace hi::inline v1 {
16class preferences;
17
18namespace detail {
19
21public:
22 preference_item_base(preferences& parent, std::string_view path) noexcept;
23
26 preference_item_base& operator=(preference_item_base const&) = delete;
27 preference_item_base& operator=(preference_item_base&&) = delete;
28 virtual ~preference_item_base() = default;
29
32 virtual void reset() noexcept = 0;
33
36 void load() noexcept;
37
38protected:
39 preferences& _parent;
40 jsonpath _path;
41
46 [[nodiscard]] virtual datum encode() const noexcept = 0;
47
48 virtual void decode(datum const& data) = 0;
49};
50
51template<typename T>
53public:
54 preference_item(preferences& parent, std::string_view path, observer<T> const& value, T init) noexcept :
55 preference_item_base(parent, path), _value(value), _init(std::move(init))
56 {
57 _value_cbt = _value.subscribe(
58 [this](auto...) {
59 if (auto tmp = this->encode(); not holds_alternative<std::monostate>(tmp)) {
60 this->_parent.write(_path, this->encode());
61 } else {
62 this->_parent.remove(_path);
63 }
64 },
65 callback_flags::local);
66 }
67
68 void reset() noexcept override
69 {
70 _value = _init;
71 }
72
73protected:
74 [[nodiscard]] datum encode() const noexcept override
75 {
76 if (*_value != _init) {
77 return hi::pickle<T>{}.encode(*_value);
78 } else {
79 return datum{std::monostate{}};
80 }
81 }
82
83 void decode(datum const& data) override
84 {
85 _value = hi::pickle<T>{}.decode(data);
86 }
87
88private:
89 T _init;
90 observer<T> _value;
91 typename decltype(_value)::callback_token _value_cbt;
92};
93
94} // namespace detail
95
114public:
121
128 preferences() noexcept;
129
136 preferences(std::filesystem::path location) noexcept;
137
138 preferences(std::string_view location) : preferences(std::filesystem::path{location}) {}
139 preferences(std::string const &location) : preferences(std::filesystem::path{location}) {}
140 preferences(char const *location) : preferences(std::filesystem::path{location}) {}
141
142 ~preferences();
143 preferences(preferences const&) = delete;
144 preferences(preferences&&) = delete;
145 preferences& operator=(preferences const&) = delete;
146 preferences& operator=(preferences&&) = delete;
147
152 void save() const noexcept;
153
160 void save(std::filesystem::path location) noexcept;
161
166 void load() noexcept;
167
174 void load(std::filesystem::path location) noexcept;
175
178 void reset() noexcept;
179
186 template<typename T>
187 void add(std::string_view path, observer<T> const& item, T init = T{}) noexcept
188 {
189 auto item_ = std::make_unique<detail::preference_item<T>>(*this, path, item, std::move(init));
190 item_->load();
191 _items.push_back(std::move(item_));
192 }
193
194private:
197 std::filesystem::path _location;
198
201 datum _data;
202
206 mutable bool _modified = false;
207
208 loop::timer_callback_token _check_modified_cbt;
209
213
214 void _load() noexcept;
215 void _save() const noexcept;
216
219 void check_modified() noexcept;
220
223 void write(jsonpath const& path, datum const value) noexcept;
224
227 datum read(jsonpath const& path) noexcept;
228
231 void remove(jsonpath const& path) noexcept;
232
233 friend class detail::preference_item_base;
234 template<typename T>
235 friend class detail::preference_item;
236};
237
238} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
A dynamic data type.
Definition datum.hpp:219
Definition jsonpath.hpp:379
A observer pointing to the whole or part of a observable.
Definition observer.hpp:22
Definition preferences.hpp:20
virtual void reset() noexcept=0
Reset the value.
Definition preferences.hpp:52
void reset() noexcept override
Reset the value.
Definition preferences.hpp:68
user preferences.
Definition preferences.hpp:113
void save() const noexcept
Save the preferences.
std::mutex mutex
Mutex used to synchronize changes to the preferences.
Definition preferences.hpp:120
preferences() noexcept
Construct a preferences instance.
T move(T... args)