blob: bc8f52e84f3060731cae5f796fab15ba7843ad34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#ifndef SMOLBOTE_CONFIGURATION_H
#define SMOLBOTE_CONFIGURATION_H
#include <optional>
#include <string>
#include <vector>
#include <unordered_map>
#include <variant>
#include <initializer_list>
#include <memory>
#include <QAction>
#include <QString>
typedef std::variant<std::string, int, bool> conf_value_t;
class [[clang::consumable(unconsumed)]] Configuration : private std::unordered_map<std::string, conf_value_t>
{
public:
[[clang::return_typestate(unconsumed)]]
explicit Configuration();
[[clang::return_typestate(unconsumed)]]
explicit Configuration(std::initializer_list<std::pair<std::string, conf_value_t>> l) noexcept;
explicit Configuration(Configuration &&other [[clang::param_typestate(unconsumed)]]) = default;
~Configuration() = default;
[[clang::callable_when(unconsumed)]]
void read(std::basic_istream<char> &input);
template <typename T>
[[clang::callable_when(unconsumed)]] std::optional<T> value(const char *path) const
{
if(use_global)
return instance()->value<T>(path);
if(this->count(path) == 0) {
return std::nullopt;
}
// path is guaranteed to exist
const auto value = at(path);
if constexpr(std::is_same_v<T, QString> || std::is_same_v<T, std::string>) {
auto r = std::get<std::string>(value);
if(r.front() == '~')
r.replace(0, 1, m_homePath);
if constexpr(std::is_same_v<T, QString>)
return std::make_optional(QString::fromStdString(r));
else
return std::make_optional(r);
} else if constexpr(std::is_same_v<T, QStringList>) {
return std::make_optional(QString::fromStdString(std::get<std::string>(value)).split(';'));
} else if (std::holds_alternative<T>(value)) {
return std::optional<T>(std::get<T>(value));
} else
return std::nullopt;
} // std::optional<T> value(path) const
static void move_global(std::unique_ptr<Configuration> &&conf);
private:
static Configuration *instance();
const std::string m_homePath;
const bool use_global = false;
};
void setShortcut(QAction *action, const char *name);
#endif // SMOLBOTE_CONFIGURATION_H
|