| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
#include <initializer_list> |
| 13 |
#include <string> |
| 14 |
#include <string_view> |
| 15 |
|
| 16 |
#include "eckit/config/LocalConfiguration.h" |
| 17 |
#include "eckit/log/JSON.h" |
| 18 |
|
| 19 |
#include "atlas/library/config.h" |
| 20 |
|
| 21 |
namespace eckit { |
| 22 |
class PathName; |
| 23 |
class Hash; |
| 24 |
} // namespace eckit |
| 25 |
|
| 26 |
namespace atlas { |
| 27 |
namespace util { |
| 28 |
|
| 29 |
/// @brief Configuration class used to construct various |
| 30 |
/// atlas components |
| 31 |
class Config : public eckit::LocalConfiguration { |
| 32 |
public: |
| 33 |
// -- Constructors |
| 34 |
|
| 35 |
Config(); |
| 36 |
|
| 37 |
/// @brief Constructor starting from a path |
| 38 |
Config(const eckit::PathName&); |
| 39 |
|
| 40 |
/// @brief Constructor starting from a stream |
| 41 |
Config(std::istream&, const std::string& format = "json"); |
| 42 |
|
| 43 |
/// @brief Constructor starting from a Configuration |
| 44 |
Config(const eckit::Configuration&); |
| 45 |
|
| 46 |
/// @brief Constructor immediately setting a value. |
| 47 |
template <typename ValueT> |
| 48 |
Config(const std::string& name, const ValueT& value); |
| 49 |
|
| 50 |
Config(const std::string& name, const std::string_view value) : |
| 51 |
Config(name, std::string(value)) {} |
| 52 |
|
| 53 |
template <typename ValueT> |
| 54 |
Config(const std::string& name, std::initializer_list<ValueT>&& value); |
| 55 |
|
| 56 |
// -- Mutators |
| 57 |
|
| 58 |
/// @brief Operator that sets a key-value pair. |
| 59 |
template <typename ValueT> |
| 60 |
Config operator()(const std::string& name, const ValueT& value); |
| 61 |
|
| 62 |
template <typename ValueT> |
| 63 |
Config operator()(const std::string& name, std::initializer_list<ValueT>&& value); |
| 64 |
|
| 65 |
// Overload operators to merge two Config objects. |
| 66 |
Config operator|(const eckit::Configuration& other) const; |
| 67 |
|
| 68 |
/// @brief Set a key-value parameter |
| 69 |
using eckit::LocalConfiguration::set; |
| 70 |
|
| 71 |
Config& set(const std::string& name, const std::vector<Config>&); |
| 72 |
|
| 73 |
Config& set(const eckit::Configuration&); |
| 74 |
|
| 75 |
template <typename T> |
| 76 |
Config& set(const std::string& name, std::initializer_list<T>&& value); |
| 77 |
|
| 78 |
Config& remove(const std::string& name); |
| 79 |
|
| 80 |
// -- Accessors, overloaded from eckit::Parametrisation |
| 81 |
|
| 82 |
using eckit::LocalConfiguration::get; |
| 83 |
bool get(const std::string& name, std::vector<Config>& value) const; |
| 84 |
|
| 85 |
#if ! (ATLAS_ECKIT_VERSION_AT_LEAST(1, 26, 0) || ATLAS_ECKIT_DEVELOP) |
| 86 |
std::vector<std::string> keys() const; |
| 87 |
#endif |
| 88 |
|
| 89 |
std::string json(eckit::JSON::Formatting = eckit::JSON::Formatting::indent()) const; |
| 90 |
}; |
| 91 |
|
| 92 |
// ------------------------------------------------------------------ |
| 93 |
|
| 94 |
template <typename ValueT> |
| 95 |
inline Config::Config(const std::string& name, const ValueT& value): eckit::LocalConfiguration() { |
| 96 |
set(name, value); |
| 97 |
} |
| 98 |
|
| 99 |
template <typename ValueT> |
| 100 |
inline Config::Config(const std::string& name, std::initializer_list<ValueT>&& value): eckit::LocalConfiguration() { |
| 101 |
set(name, value); |
| 102 |
} |
| 103 |
|
| 104 |
template <typename ValueT> |
| 105 |
inline Config Config::operator()(const std::string& name, const ValueT& value) { |
| 106 |
set(name, value); |
| 107 |
return *this; |
| 108 |
} |
| 109 |
|
| 110 |
template <typename ValueT> |
| 111 |
inline Config& Config::set(const std::string& name, std::initializer_list<ValueT>&& value) { |
| 112 |
set(name, std::vector<ValueT>(value.begin(), value.end())); |
| 113 |
return *this; |
| 114 |
} |
| 115 |
|
| 116 |
template <typename ValueT> |
| 117 |
inline Config Config::operator()(const std::string& name, std::initializer_list<ValueT>&& value) { |
| 118 |
set(name, std::vector<ValueT>(value.begin(), value.end())); |
| 119 |
return *this; |
| 120 |
} |
| 121 |
|
| 122 |
// ------------------------------------------------------------------ |
| 123 |
// C wrapper interfaces to C++ routines |
| 124 |
// clang-format off |
| 125 |
extern "C" { |
| 126 |
Config* atlas__Config__new(); |
| 127 |
Config* atlas__Config__new_from_json( const char* json ); |
| 128 |
Config* atlas__Config__new_from_file( const char* path ); |
| 129 |
void atlas__Config__delete( Config* This ); |
| 130 |
int atlas__Config__has( Config* This, const char* name ); |
| 131 |
void atlas__Config__set_config( Config* This, const char* name, const Config* value ); |
| 132 |
void atlas__Config__set_config_list( Config* This, const char* name, const Config* value[], int size ); |
| 133 |
void atlas__Config__set_int( Config* This, const char* name, int value ); |
| 134 |
void atlas__Config__set_long( Config* This, const char* name, long value ); |
| 135 |
void atlas__Config__set_float( Config* This, const char* name, float value ); |
| 136 |
void atlas__Config__set_double( Config* This, const char* name, double value ); |
| 137 |
void atlas__Config__set_string( Config* This, const char* name, const char* value ); |
| 138 |
void atlas__Config__set_array_int( Config* This, const char* name, int value[], int size ); |
| 139 |
void atlas__Config__set_array_long( Config* This, const char* name, long value[], int size ); |
| 140 |
void atlas__Config__set_array_float( Config* This, const char* name, float value[], int size ); |
| 141 |
void atlas__Config__set_array_double( Config* This, const char* name, double value[], int size ); |
| 142 |
|
| 143 |
int atlas__Config__get_config( Config* This, const char* name, Config* value ); |
| 144 |
int atlas__Config__get_config_list( Config* This, const char* name, Config** &value, int& size, int& allocated ); |
| 145 |
int atlas__Config__get_int( Config* This, const char* name, int& value ); |
| 146 |
int atlas__Config__get_long( Config* This, const char* name, long& value ); |
| 147 |
int atlas__Config__get_float( Config* This, const char* name, float& value ); |
| 148 |
int atlas__Config__get_double( Config* This, const char* name, double& value ); |
| 149 |
int atlas__Config__get_string( Config* This, const char* name, char*& value, int& size, int& allocated ); |
| 150 |
int atlas__Config__get_array_int( Config* This, const char* name, int*& value, int& size, int& allocated ); |
| 151 |
int atlas__Config__get_array_long( Config* This, const char* name, long*& value, int& size, int& allocated ); |
| 152 |
int atlas__Config__get_array_float( Config* This, const char* name, float*& value, int& size, int& allocated ); |
| 153 |
int atlas__Config__get_array_double( Config* This, const char* name, double*& value, int& size, int& allocated ); |
| 154 |
void atlas__Config__json( Config* This, char*& json, int& size, int& allocated ); |
| 155 |
} |
| 156 |
// clang-format on |
| 157 |
// ------------------------------------------------------------------ |
| 158 |
|
| 159 |
class NoConfig : public Config { |
| 160 |
public: // methods |
| 161 |
/// Destructor redundant but fixes sanity compiler warnings |
| 162 |
virtual ~NoConfig() {} |
| 163 |
|
| 164 |
virtual bool has(const std::string& name) const { return false; } |
| 165 |
|
| 166 |
virtual bool get(const std::string& name, std::string& value) const { return false; } |
| 167 |
virtual bool get(const std::string& name, bool& value) const { return false; } |
| 168 |
virtual bool get(const std::string& name, int& value) const { return false; } |
| 169 |
virtual bool get(const std::string& name, long& value) const { return false; } |
| 170 |
virtual bool get(const std::string& name, long long& value) const { return false; } |
| 171 |
virtual bool get(const std::string& name, std::size_t& value) const { return false; } |
| 172 |
virtual bool get(const std::string& name, float& value) const { return false; } |
| 173 |
virtual bool get(const std::string& name, double& value) const { return false; } |
| 174 |
|
| 175 |
virtual bool get(const std::string& name, std::vector<std::string>& value) const { return false; } |
| 176 |
virtual bool get(const std::string& name, std::vector<int>& value) const { return false; } |
| 177 |
virtual bool get(const std::string& name, std::vector<long>& value) const { return false; } |
| 178 |
virtual bool get(const std::string& name, std::vector<long long>& value) const { return false; } |
| 179 |
virtual bool get(const std::string& name, std::vector<std::size_t>& value) const { return false; } |
| 180 |
virtual bool get(const std::string& name, std::vector<float>& value) const { return false; } |
| 181 |
virtual bool get(const std::string& name, std::vector<double>& value) const { return false; } |
| 182 |
}; |
| 183 |
|
| 184 |
} // namespace util |
| 185 |
} // namespace atlas |