15namespace hi::inline
v1 {
20class preference_item_base {
22 preference_item_base(
preferences& parent, std::string_view path)
noexcept;
24 preference_item_base(preference_item_base
const&) =
delete;
25 preference_item_base(preference_item_base&&) =
delete;
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;
32 virtual void reset() noexcept = 0;
46 [[nodiscard]] virtual
datum encode() const noexcept = 0;
48 virtual
void decode(
datum const& data) = 0;
52class preference_item : public preference_item_base {
55 preference_item_base(parent, path), _value(value), _init(
std::move(init))
57 _value_cbt = _value.subscribe(
59 if (
auto tmp = this->encode(); not holds_alternative<std::monostate>(tmp)) {
60 this->_parent.write(_path, this->encode());
62 this->_parent.remove(_path);
74 [[nodiscard]]
datum encode() const noexcept
override
76 if (*_value != _init) {
77 return hi::pickle<T>{}.encode(*_value);
79 return datum{std::monostate{}};
83 void decode(datum
const& data)
override
85 _value = hi::pickle<T>{}.decode(data);
91 typename decltype(_value)::callback_token _value_cbt;
139 preferences(
std::string const &location) : preferences(
std::filesystem::path{location}) {}
140 preferences(
char const *location) : preferences(std::filesystem::path{location}) {}
143 preferences(preferences
const&) =
delete;
144 preferences(preferences&&) =
delete;
145 preferences& operator=(preferences
const&) =
delete;
146 preferences& operator=(preferences&&) =
delete;
160 void save(
std::filesystem::path location) noexcept;
174 void load(
std::filesystem::path location) noexcept;
189 auto item_ = std::make_unique<detail::preference_item<T>>(*
this, path, item,
std::move(init));
197 std::filesystem::path _location;
206 mutable bool _modified =
false;
208 loop::timer_callback_token _check_modified_cbt;
214 void _load() noexcept;
215 void _save() const noexcept;
219 void check_modified() noexcept;
223 void write(jsonpath const& path, datum const value) noexcept;
227 datum read(jsonpath const& path) noexcept;
231 void remove(jsonpath const& path) noexcept;
233 friend class detail::preference_item_base;
235 friend class detail::preference_item;
DOXYGEN BUG.
Definition algorithm.hpp:13
@ local
Call the function asynchronously from the current thread's loop.
Definition callback_flags.hpp:18
A dynamic data type.
Definition datum.hpp:223
Definition jsonpath.hpp:379
A observer pointing to the whole or part of a observable.
Definition observer.hpp:22
virtual void reset() noexcept=0
Reset the value.
void load() noexcept
Load a value from the preferences.
void reset() noexcept override
Reset the value.
Definition preferences.hpp:68
user preferences.
Definition preferences.hpp:113
void save() const noexcept
Save the preferences.
void reset() noexcept
Reset data members to their default value.
std::mutex mutex
Mutex used to synchronize changes to the preferences.
Definition preferences.hpp:120
preferences() noexcept
Construct a preferences instance.
void load() noexcept
Load the preferences.
void add(std::string_view path, observer< T > const &item, T init=T{}) noexcept
Register an observer to a preferences file.
Definition preferences.hpp:187