-
Notifications
You must be signed in to change notification settings - Fork 1
/
array.cpp
185 lines (161 loc) · 4.11 KB
/
array.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (C) High-Performance Computing Center Stuttgart (https://www.hlrs.de/)
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "array.h"
#include "detail/entry.h"
#include "detail/manager.h"
#include "detail/output.h"
#include <cassert>
#ifdef CONFIG_NAMESPACE
namespace CONFIG_NAMESPACE {
#endif
namespace config {
using namespace detail;
template<class V>
Array<V>::Array(const std::string &path, const std::string §ion, const std::string &name, detail::Manager *mgr)
: ConfigBase("Array")
{
if (!mgr)
mgr = Manager::the();
m_entry = mgr->getArray<V>(path, section, name, Flag::Default);
entry()->addObserver(this);
}
template<class V>
Array<V>::Array(const std::string &path, const std::string §ion, const std::string &name,
const std::vector<V> &value, detail::Manager *mgr, Flag flags)
: ConfigBase("Array")
{
if (!mgr)
mgr = Manager::the();
m_entry = mgr->getArray<V>(path, section, name, flags);
std::vector<typename detail::VectorStorage<V>::Type> val(value.size());
for (size_t i = 0; i < value.size(); ++i) {
val[i] = value[i];
}
entry()->setOrCheckDefaultValue(val);
debug() << key() << " initialized to " << entry()->value() << " (default: " << defaultValue() << ")" << std::endl;
entry()->addObserver(this);
entry()->assign();
}
template<class V>
Array<V>::Array(ArrayEntry<V> *entry): ConfigBase("Array")
{
m_entry = entry;
entry->addObserver(this);
}
template<class V>
Array<V>::~Array()
{
entry()->removeObserver(this);
}
template<class V>
ArrayEntry<V> *Array<V>::entry() const
{
return static_cast<ArrayEntry<V> *>(m_entry);
}
template<class V>
size_t Array<V>::size() const
{
return entry()->size();
}
template<class V>
void Array<V>::resize(size_t size)
{
return entry()->resize(size);
}
template<class V>
V Array<V>::operator[](size_t index) const
{
return entry()->at(index);
}
template<class V>
typename Array<V>::ValueProxy &Array<V>::ValueProxy::operator=(const V &value)
{
assert(array);
if (array) {
if (V(array->entry()->at(index)) != value) {
array->entry()->at(index) = value;
array->entry()->setModified();
}
}
return *this;
}
template<class V>
typename Array<V>::ValueProxy Array<V>::operator[](size_t index)
{
if (index >= size()) {
debug("operator[]") << "resizing from " << size() << " for access at " << index << std::endl;
resize(index + 1);
}
ValueProxy vp{this, index};
return vp;
}
template<class V>
Array<V>::ValueProxy::ValueProxy(Array<V> *array, size_t index): array(array), index(index)
{}
template<class V>
Array<V>::ValueProxy::~ValueProxy()
{
array->entry()->store();
}
template<class V>
Array<V> &Array<V>::operator=(const std::vector<V> &val)
{
if (size() != val.size())
resize(val.size());
for (size_t c = 0; c < size(); ++c) {
if (V(entry()->at(c)) != val[c]) {
entry()->at(c) = val[c];
entry()->setModified();
}
}
entry()->store();
return *this;
}
template<class V>
std::vector<V> Array<V>::value() const
{
std::vector<V> vec(size());
for (size_t c = 0; c < size(); ++c) {
vec[c] = entry()->at(c);
}
return vec;
}
template<class V>
std::vector<V> Array<V>::defaultValue() const
{
auto &d = entry()->defaultValue();
std::vector<V> vec(d.size());
std::copy(d.begin(), d.end(), vec.begin());
return vec;
}
template<class V>
void Array<V>::update()
{
auto &str = debug("update");
str << key() << ": have updater: " << (m_updater ? "yes" : "no");
str << " {";
for (auto v: entry()->value())
str << " " << v;
str << " }";
str << std::endl;
if (m_updater)
m_updater();
}
template<class V>
void Array<V>::setUpdater(std::function<void()> func)
{
m_updater = func;
update();
}
#ifndef WIN32
#undef COVEXPORT
#define COVEXPORT
#endif
template class COVEXPORT Array<bool>;
template class COVEXPORT Array<int64_t>;
template class COVEXPORT Array<double>;
template class COVEXPORT Array<std::string>;
} // namespace config
#ifdef CONFIG_NAMESPACE
}
#endif