-
Notifications
You must be signed in to change notification settings - Fork 1
/
value.cpp
111 lines (95 loc) · 2.41 KB
/
value.cpp
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
// Copyright (C) High-Performance Computing Center Stuttgart (https://www.hlrs.de/)
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "value.h"
#include "detail/entry.h"
#include "detail/manager.h"
#ifdef CONFIG_NAMESPACE
namespace CONFIG_NAMESPACE {
#endif
namespace config {
using namespace detail;
template<class V>
Value<V>::Value(const std::string &path, const std::string §ion, const std::string &name, const V &value,
Manager *mgr, Flag flags)
: ConfigBase("Value")
{
if (!mgr)
mgr = Manager::the();
m_entry = mgr->getValue<V>(path, section, name, flags);
entry()->setOrCheckDefaultValue(value);
debug() << key() << " initialized to " << entry()->value() << " (default: " << defaultValue() << ")" << std::endl;
entry()->addObserver(this);
entry()->assign();
}
template<class V>
Value<V>::Value(const std::string &path, const std::string §ion, const std::string &name, Manager *mgr)
: ConfigBase("Value")
{
if (!mgr)
mgr = Manager::the();
m_entry = mgr->getValue<V>(path, section, name, Flag::Default);
entry()->addObserver(this);
}
template<class V>
Value<V>::Value(ValueEntry<V> *entry): ConfigBase("Value")
{
m_entry = entry;
entry->addObserver(this);
}
template<class V>
Value<V>::~Value()
{
entry()->removeObserver(this);
}
template<class V>
ValueEntry<V> *Value<V>::entry() const
{
return static_cast<ValueEntry<V> *>(m_entry);
}
template<class V>
const V &Value<V>::defaultValue() const
{
return entry()->defaultValue();
}
template<class V>
const V &Value<V>::value() const
{
return entry()->value();
}
template<class V>
Value<V>::operator V() const
{
return value();
}
template<class V>
Value<V> &Value<V>::operator=(const V &value)
{
*entry() = value;
return *this;
}
template<class V>
void Value<V>::update()
{
debug("update") << key() << ": have updater: " << (m_updater ? "yes" : "no") << ", value=" << entry()->value()
<< std::endl;
if (m_updater)
m_updater(entry()->value());
}
template<class V>
void Value<V>::setUpdater(std::function<void(const V &)> func)
{
m_updater = func;
update();
}
#ifndef WIN32
#undef COVEXPORT
#define COVEXPORT
#endif
template class COVEXPORT Value<bool>;
template class COVEXPORT Value<int64_t>;
template class COVEXPORT Value<double>;
template class COVEXPORT Value<std::string>;
} // namespace config
#ifdef CONFIG_NAMESPACE
}
#endif