-
Notifications
You must be signed in to change notification settings - Fork 1
/
access.h
137 lines (123 loc) · 8.03 KB
/
access.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
// Copyright (C) High-Performance Computing Center Stuttgart (https://www.hlrs.de/)
// SPDX-License-Identifier: LGPL-2.1-or-later
/// \file access.h
/// organize access to configuration system
#pragma once
#include <string>
#include <memory>
#include <vector>
#include <functional>
#include "detail/export.h"
#include "detail/flags.h"
#include "detail/logger.h"
#ifdef CONFIG_NAMESPACE
namespace CONFIG_NAMESPACE {
#endif
namespace config {
namespace detail {
class Manager;
}
class File;
class ConfigBase;
template<class V>
class Value;
template<class V>
class Array;
/// provide a bridge for retrieving and storing per-model configuration values
/** When configuration entries tagged with Flag::PerModel are changed, the registered bridge is notified of this via \ref Bridge::wasChanged.
It is the Bridge's responsibility to store the changed entry as required.
If the bridge needs to change configuration entries, then it can do so by modifying them with the interfaces provided by \ref Array or \ref Value.
*/
class COVEXPORT Bridge {
public:
virtual ~Bridge();
virtual bool wasChanged(
const ConfigBase
*entry) = 0; ///< called to notify that a configuration entry (\ref Value or \ref Array) has been changed
};
/// organize access to configuration system
/** Creating an instance of Access controls access to the configuration system via a Manager. This Manager is created and destroyed as needed. */
class COVEXPORT Access: detail::Logger {
public:
static bool isInitialized(); ///< check if Access has already been initialized with the non-default constructor
Access(); ///< initiate access to configuration system with default search path only
Access(const std::string &host, const std::string &cluster,
int rank = -1); ///< initiate access to configuration, preferring configuration files for host and cluster
virtual ~Access(); ///< revoke access to configuration system
const std::string &cluster() const; ///< query configured cluster for which configuration is read
const std::string &hostname() const; ///< query configured hostname for which configuration is read
void setPrefix(const std::string &dir); ///< set software installation prefix as additional search path
bool setWorkspaceBridge(Bridge *bridge); ///< specify \ref Bridge for accessing per-model configuration values
bool removeWorkspaceBridge(Bridge *bridge); ///< remove per-model configuration \ref Bridge
void setErrorHandler(std::function<void()> handler = nullptr); ///< what to do in case of errors, initially calls exit
bool save(); ///< save changes in all files that should be saved on exit
std::unique_ptr<File> file(const std::string &path) const; ///< get interface to a configuration file
template<class V>
std::unique_ptr<Value<V>> value(const std::string &path, const std::string §ion,
const std::string &name); ///< query existing configuration value
template<class V>
std::unique_ptr<Value<V>>
value(const std::string &path, const std::string §ion, const std::string &name, const V &def,
Flag flags = Flag::Default); ///< create configuration value with the provided default
template<class V>
std::unique_ptr<Array<V>> array(const std::string &path, const std::string §ion,
const std::string &name); ///< query existing configuration array
template<class V>
std::unique_ptr<Array<V>>
array(const std::string &path, const std::string §ion, const std::string &name, const std::vector<V> &def,
Flag flags = Flag::Default); ///< create configuration array with the provided default
private:
Bridge *m_bridge = nullptr;
detail::Manager *m_manager = nullptr;
};
extern template std::unique_ptr<Value<bool>>
COVEXPORT Access::value(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Value<int64_t>>
COVEXPORT Access::value(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Value<double>>
COVEXPORT Access::value(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Value<std::string>>
COVEXPORT Access::value(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Value<bool>> COVEXPORT Access::value(const std::string &path,
const std::string §ion,
const std::string &name, const bool &def,
Flag flags);
extern template std::unique_ptr<Value<int64_t>> COVEXPORT Access::value(const std::string &path,
const std::string §ion,
const std::string &name,
const int64_t &def, Flag flags);
extern template std::unique_ptr<Value<double>> COVEXPORT Access::value(const std::string &path,
const std::string §ion,
const std::string &name,
const double &def, Flag flags);
extern template std::unique_ptr<Value<std::string>> COVEXPORT Access::value(const std::string &path,
const std::string §ion,
const std::string &name,
const std::string &def, Flag flags);
extern template std::unique_ptr<Array<bool>>
COVEXPORT Access::array(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Array<int64_t>>
COVEXPORT Access::array(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Array<double>>
COVEXPORT Access::array(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Array<std::string>>
COVEXPORT Access::array(const std::string &path, const std::string §ion, const std::string &name);
extern template std::unique_ptr<Array<bool>> COVEXPORT Access::array(const std::string &path,
const std::string §ion,
const std::string &name,
const std::vector<bool> &def, Flag flags);
extern template std::unique_ptr<Array<int64_t>> COVEXPORT Access::array(const std::string &path,
const std::string §ion,
const std::string &name,
const std::vector<int64_t> &def, Flag flags);
extern template std::unique_ptr<Array<double>> COVEXPORT Access::array(const std::string &path,
const std::string §ion,
const std::string &name,
const std::vector<double> &def, Flag flags);
extern template std::unique_ptr<Array<std::string>>
COVEXPORT Access::array(const std::string &path, const std::string §ion, const std::string &name,
const std::vector<std::string> &def, Flag flags);
}
#ifdef CONFIG_NAMESPACE
}
#endif