-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacct.h
138 lines (109 loc) · 3.54 KB
/
acct.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* COPYRIGHT 2014 (C) Jason Volk
* COPYRIGHT 2014 (C) Svetlana Tkachenko
*
* DISTRIBUTED UNDER THE GNU GENERAL PUBLIC LICENSE (GPL) (see: LICENSE)
*/
class Acct
{
Adb *adb;
const std::string *acct; // the document key ; subclass holds data
auto &get_adb() { return *adb; }
public:
auto &get_adb() const { return *adb; }
auto &get_acct() const { return *acct; }
bool exists() const { return adb->exists(get_acct()); }
// Get document
Adoc get() const { return adb->get(std::nothrow,get_acct()); }
Adoc get() { return adb->get(std::nothrow,get_acct()); }
Adoc get(const std::string &key) const { return get().get_child(key,Adoc{}); }
Adoc get(const std::string &key) { return get().get_child(key,Adoc{}); }
Adoc operator[](const std::string &key) const { return get(key); }
Adoc operator[](const std::string &key) { return get(key); }
// Get value of document
template<class T = std::string> T get_val(const std::string &key, const T &def) const;
template<class T = std::string> T get_val(const std::string &key, const T &def);
template<class T = std::string> T get_val(const std::string &key) const;
template<class T = std::string> T get_val(const std::string &key);
// Check if document exists
bool has(const std::string &key) const { return !get_val(key).empty(); }
// Set document
void set(const Adoc &doc) { adb->set(get_acct(),doc); }
void set(const std::string &key, const Adoc &doc);
// Convenience for single key => value
template<class It> void set_val(const std::string &key, It&& begin, It &&end);
template<class T> void set_val(const std::string &key, const T &t);
Acct(const std::string *const &acct, Adb *const &adb = bot::adb);
Acct(Acct &&) = delete;
Acct(const Acct &) = delete;
Acct &operator=(Acct &&) = default;
Acct &operator=(const Acct &) = default;
virtual ~Acct() = default;
};
inline
Acct::Acct(const std::string *const &acct,
Adb *const &adb):
adb(adb),
acct(acct)
{
}
template<> inline
void Acct::set_val(const std::string &key,
const Adoc &val)
{
Adoc doc(get());
doc.put_child(key,val);
set(doc);
}
template<class T>
void Acct::set_val(const std::string &key,
const T &t)
{
Adoc doc(get());
doc.put(key,t);
set(doc);
}
template<class It>
void Acct::set_val(const std::string &key,
It&& begin,
It&& end)
{
Adoc doc;
for(auto it(begin); it != end; ++it)
doc.push(*it);
set_val(key,doc);
}
inline
void Acct::set(const std::string &key,
const Adoc &doc)
{
Adoc main(get());
main.put_child(key,doc);
set(main);
}
template<class T>
T Acct::get_val(const std::string &key)
{
return get_val(key,T{});
}
template<class T>
T Acct::get_val(const std::string &key)
const
{
return get_val(key,T{});
}
template<class T>
T Acct::get_val(const std::string &key,
const T &def)
{
const Adoc doc(get(key));
return doc.get_value<T>(def);
}
template<class T>
T Acct::get_val(const std::string &key,
const T &def)
const
{
const Adoc doc(get(key));
return doc.get_value<T>(def);
}