opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
app_settings.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <filesystem>
7#include <memory>
8#include <optional>
9#include <string_view>
10
11namespace osc
12{
13 // persistent, platform-independent, singleton-ed application settings
15 public:
17 std::string_view organization_name,
18 std::string_view application_name,
19 std::string_view application_config_file_name
20 );
26
27 // if available, returns the filesystem path of the system configuration file
28 //
29 // the system configuration file isn't necessarily available (e.g. the user
30 // may have deleted it)
32
33 std::optional<Variant> find_value(std::string_view key) const;
34
37 std::optional<T> find_value(std::string_view key) const
38 {
39 if (auto variant_value = find_value(key)) {
40 return T{*std::move(variant_value)};
41 }
42 else {
43 return std::nullopt;
44 }
45 }
46
47 Variant get_value(std::string_view key, Variant fallback = Variant{}) const
48 {
49 return find_value(key).value_or(std::move(fallback));
50 }
51
52 template<typename T>
53 requires std::constructible_from<Variant, T&&> and std::constructible_from<T, Variant>
54 T get_value(std::string_view key, T&& fallback = T{}) const
55 {
56 if (auto v = find_value(key)) {
57 return static_cast<T>(*v);
58 }
59 else {
60 return T{std::forward<T>(fallback)};
61 }
62 }
63
65
66 template<typename T>
67 requires std::constructible_from<Variant, T&&>
68 void set_value(std::string_view key, T&& value, AppSettingScope scope = AppSettingScope::User)
69 {
70 set_value(key, Variant{std::forward<T>(value)}, scope);
71 }
72
74
75 template<typename T>
76 requires std::constructible_from<Variant, T&&>
78 {
79 set_value_if_not_found(key, Variant{std::forward<T>(value)}, scope);
80 }
81
82 // if available, returns the filesystem path of the configuration file that
83 // provided the given setting value
84 //
85 // this can be useful if (e.g.) the value is specifying something that is
86 // relative to the configuration file's location on disk
87 //
88 // not available if:
89 //
90 // - `key` isn't set
91 // - `key` is set, but `AppSettings` was unable to find/create a suitable
92 // user configuration file (e.g. user filesystem permissions are wrong)
93 std::optional<std::filesystem::path> find_value_filesystem_source(std::string_view key) const;
94
95 // synchronize the current in-memory state of this settings object to disk
96 //
97 // note #1: this is automatically called by the destructor
98 //
99 // note #2: only user-level and values that were set with `setValue` will
100 // be synchronized to disk - system values are not synchronized.
101 void sync();
102
103 class Impl;
104 private:
105 std::shared_ptr<Impl> impl_;
106 };
107}
Definition app_settings.h:14
std::optional< std::filesystem::path > system_configuration_file_location() const
AppSettings(AppSettings &&) noexcept
void set_value(std::string_view key, Variant, AppSettingScope=AppSettingScope::User)
and std::constructible_from< T, Variant > T get_value(std::string_view key, T &&fallback=T{}) const
Definition app_settings.h:54
AppSettings(const AppSettings &)
void set_value_if_not_found(std::string_view key, T &&value, AppSettingScope scope=AppSettingScope::User)
Definition app_settings.h:77
std::optional< Variant > find_value(std::string_view key) const
std::optional< std::filesystem::path > find_value_filesystem_source(std::string_view key) const
AppSettings(std::string_view organization_name, std::string_view application_name, std::string_view application_config_file_name)
void set_value_if_not_found(std::string_view key, Variant, AppSettingScope=AppSettingScope::User)
Variant get_value(std::string_view key, Variant fallback=Variant{}) const
Definition app_settings.h:47
void set_value(std::string_view key, T &&value, AppSettingScope scope=AppSettingScope::User)
Definition app_settings.h:68
Definition variant.h:18
Definition custom_decoration_generator.h:5
AppSettingScope
Definition app_setting_scope.h:9
constexpr U to(T &&value)
Definition conversion.h:56
and
Definition algorithms.h:158