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