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 <typeinfo>
9
10#pragma once
11
12namespace tt {
13
25public:
26 mutable std::recursive_mutex mutex;
27
28 preferences(URL location) noexcept;
29 virtual ~preferences();
30 preferences(preferences const &) = delete;
31 preferences(preferences &&) = delete;
32 preferences &operator=(preferences const &) = delete;
33 preferences &operator=(preferences &&) = delete;
34
37 [[nodiscard]] virtual void reset() noexcept {}
38
41 void save() const noexcept;
42
45 void load() noexcept;
46
54 [[nodiscard]] virtual datum serialize() const noexcept
55 {
56 return {datum::map{}};
57 }
58
66 virtual void deserialize(datum const &data) noexcept
67 {
68 ttlet lock = std::scoped_lock(mutex);
69 tt_axiom(_deserializing >= 0);
70 ++_deserializing;
71
72 --_deserializing;
73 }
74
75protected:
80 std::shared_ptr<std::function<void()>> _set_modified_ptr;
81
86 int _deserializing = 0;
87
88 template<typename T, typename Observable, typename Key>
89 static void deserialize_value(Observable &obj, datum const &data, Key const &key) noexcept
90 {
91 if (data.contains(key)) {
92 auto const &data_value = data[key];
93 if (holds_alternative<T>(data_value)) {
94 obj = static_cast<T>(data_value);
95 } else {
96 tt_log_error(
97 "Could not deserialize '{}' for key '{}' to a '{}' type.",
98 to_string(data_value),
99 key,
100 typeid(T).name());
101 }
102 }
103 }
104
105private:
106 URL _location;
107
111 mutable bool _modified = false;
112
113 timer::callback_ptr_type _check_modified_ptr;
114
117 void set_modified() noexcept
118 {
119 ttlet lock = std::scoped_lock(mutex);
120 tt_axiom(_deserializing >= 0);
121 if (_deserializing == 0) {
122 _modified = true;
123 }
124 }
125
126 void check_modified() noexcept
127 {
128 ttlet lock = std::scoped_lock(mutex);
129 tt_axiom(_deserializing >= 0);
130
131 if (_modified) {
132 save();
133 }
134 }
135};
136
137} // namespace tt
user preferences.
Definition preferences.hpp:24
void load() noexcept
Load the preferences.
void save() const noexcept
Save the preferences.
virtual datum serialize() const noexcept
Serialize the preferences data.
Definition preferences.hpp:54
virtual void deserialize(datum const &data) noexcept
Deserialize the preferences.
Definition preferences.hpp:66
virtual void reset() noexcept
Reset data members to their default value.
Definition preferences.hpp:37
Definition URL.hpp:46
T lock(T... args)