HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
preferences.hpp
1// Copyright Take Vos 2020.
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 "URL.hpp"
6#include "datum.hpp"
7#include "timer.hpp"
8#include "log.hpp"
9#include "jsonpath.hpp"
10#include "observable.hpp"
11#include "pickle.hpp"
12#include <typeinfo>
13
14#pragma once
15
16namespace tt {
17class preferences;
18
19namespace detail {
20
22public:
23 preference_item_base(preferences &parent, std::string_view path) noexcept;
24
27 preference_item_base &operator=(preference_item_base const &) = delete;
28 preference_item_base &operator=(preference_item_base &&) = delete;
29 virtual ~preference_item_base() = default;
30
33 virtual void reset() noexcept = 0;
34
37 void load() noexcept;
38
39protected:
40 std::shared_ptr<std::function<void()>> _modified_callback_ptr;
41 preferences &_parent;
42 jsonpath _path;
43
48 [[nodiscard]] virtual datum encode() const noexcept = 0;
49
50 virtual void decode(datum const &data) = 0;
51};
52
53template<typename T>
55public:
56 preference_item(preferences &parent, std::string_view path, observable<T> const &value, T init) noexcept :
57 preference_item_base(parent, path), _value(value), _init(std::move(init))
58 {
59 _value.subscribe(this->_modified_callback_ptr);
60 }
61
62 void reset() noexcept override
63 {
64 _value = _init;
65 }
66
67protected:
68 [[nodiscard]] datum encode() const noexcept override
69 {
70 auto tmp = *_value;
71 if (tmp != _init) {
72 return tt::pickle<T>{}.encode(tmp);
73 } else {
74 return datum{std::monostate{}};
75 }
76 }
77
78 void decode(datum const &data) override
79 {
80 _value = tt::pickle<T>{}.decode(data);
81 }
82
83private:
84 T _init;
85 observable<T> _value;
86};
87
88} // namespace detail
89
108public:
115
122 preferences() noexcept;
123
130 preferences(URL location) noexcept;
131
132 ~preferences();
133 preferences(preferences const &) = delete;
134 preferences(preferences &&) = delete;
135 preferences &operator=(preferences const &) = delete;
136 preferences &operator=(preferences &&) = delete;
137
142 void save() const noexcept;
143
150 void save(URL location) noexcept;
151
156 void load() noexcept;
157
164 void load(URL location) noexcept;
165
168 [[nodiscard]] void reset() noexcept;
169
176 template<typename T>
177 void add(std::string_view path, observable<T> const &item, T init = T{}) noexcept
178 {
179 auto item_ = std::make_unique<detail::preference_item<T>>(*this, path, item, std::move(init));
180 item_->load();
181 _items.push_back(std::move(item_));
182 }
183
184private:
187 URL _location;
188
191 datum _data;
192
196 mutable bool _modified = false;
197
200 timer::callback_ptr_type _check_modified_callback_ptr;
201
205
206 void _load() noexcept;
207 void _save() const noexcept;
208
211 void check_modified() noexcept;
212
215 void write(jsonpath const &path, datum const value) noexcept;
216
219 datum read(jsonpath const &path) noexcept;
220
223 void remove(jsonpath const &path) noexcept;
224
225 friend class detail::preference_item_base;
226};
227
228} // namespace tt
STL namespace.
A dynamic data type.
Definition datum.hpp:213
Definition jsonpath.hpp:379
A value which can be observed for modifications.
Definition observable.hpp:464
Encode and decode a type to and from a UTF-8 string.
Definition pickle.hpp:22
T decode(datum rhs) const
Decode a UTF-8 string into a value of a given type.
datum encode(T const &rhs) const noexcept
Encode the value of a type into a UTF-8 string.
Definition preferences.hpp:21
virtual void reset() noexcept=0
Reset the value.
void load() noexcept
Load a value from the preferences.
Definition preferences.hpp:54
void reset() noexcept override
Reset the value.
Definition preferences.hpp:62
user preferences.
Definition preferences.hpp:107
void load() noexcept
Load the preferences.
std::mutex mutex
Mutex used to synchronize changes to the preferences.
Definition preferences.hpp:114
preferences() noexcept
Construct a preferences instance.
Definition URL.hpp:47
T move(T... args)