atlas
Config.h
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 
15 #include "eckit/config/LocalConfiguration.h"
16 
17 namespace eckit {
18 class PathName;
19 class Hash;
20 } // namespace eckit
21 
22 namespace atlas {
23 namespace util {
24 
27 class Config : public eckit::LocalConfiguration {
28 public:
29  // -- Constructors
30 
31  Config();
32 
34  Config( const eckit::PathName& );
35 
37  Config( std::istream&, const std::string& format = "json" );
38 
40  Config( const eckit::Configuration& );
41 
43  template <typename ValueT>
44  Config( const std::string& name, const ValueT& value );
45 
46  template <typename ValueT>
47  Config( const std::string& name, std::initializer_list<ValueT>&& value );
48 
49  // -- Mutators
50 
52  template <typename ValueT>
53  Config operator()( const std::string& name, const ValueT& value );
54 
55  template <typename ValueT>
56  Config operator()( const std::string& name, std::initializer_list<ValueT>&& value );
57 
58  // Overload operators to merge two Config objects.
59  Config operator|( const Config& other ) const;
60 
62  using eckit::LocalConfiguration::set;
63 
64  Config& set( const std::string& name, const std::vector<Config>& );
65 
66  Config& set( const eckit::LocalConfiguration& );
67 
68  template <typename T>
69  Config& set( const std::string& name, std::initializer_list<T>&& value );
70 
71  Config& remove( const std::string& name );
72 
73  // -- Accessors, overloaded from eckit::Parametrisation
74 
75  using eckit::LocalConfiguration::get;
76  bool get( const std::string& name, std::vector<Config>& value ) const;
77 
78  std::vector<std::string> keys() const;
79 };
80 
81 // ------------------------------------------------------------------
82 
83 template <typename ValueT>
84 inline Config::Config( const std::string& name, const ValueT& value ) : eckit::LocalConfiguration() {
85  set( name, value );
86 }
87 
88 template <typename ValueT>
89 inline Config::Config( const std::string& name, std::initializer_list<ValueT>&& value ) : eckit::LocalConfiguration() {
90  set( name, value );
91 }
92 
93 template <typename ValueT>
94 inline Config Config::operator()( const std::string& name, const ValueT& value ) {
95  set( name, value );
96  return *this;
97 }
98 
99 template <typename ValueT>
100 inline Config& Config::set( const std::string& name, std::initializer_list<ValueT>&& value ) {
101  set( name, std::vector<ValueT>( value.begin(), value.end() ) );
102  return *this;
103 }
104 
105 template <typename ValueT>
106 inline Config Config::operator()( const std::string& name, std::initializer_list<ValueT>&& value ) {
107  set( name, std::vector<ValueT>( value.begin(), value.end() ) );
108  return *this;
109 }
110 
111 // ------------------------------------------------------------------
112 // C wrapper interfaces to C++ routines
113 // clang-format off
114 extern "C" {
115 Config* atlas__Config__new();
116 Config* atlas__Config__new_from_json( const char* json );
117 Config* atlas__Config__new_from_file( const char* path );
118 void atlas__Config__delete( Config* This );
119 int atlas__Config__has( Config* This, const char* name );
120 void atlas__Config__set_config( Config* This, const char* name, const Config* value );
121 void atlas__Config__set_config_list( Config* This, const char* name, const Config* value[], int size );
122 void atlas__Config__set_int( Config* This, const char* name, int value );
123 void atlas__Config__set_long( Config* This, const char* name, long value );
124 void atlas__Config__set_float( Config* This, const char* name, float value );
125 void atlas__Config__set_double( Config* This, const char* name, double value );
126 void atlas__Config__set_string( Config* This, const char* name, const char* value );
127 void atlas__Config__set_array_int( Config* This, const char* name, int value[], int size );
128 void atlas__Config__set_array_long( Config* This, const char* name, long value[], int size );
129 void atlas__Config__set_array_float( Config* This, const char* name, float value[], int size );
130 void atlas__Config__set_array_double( Config* This, const char* name, double value[], int size );
131 
132 int atlas__Config__get_config( Config* This, const char* name, Config* value );
133 int atlas__Config__get_config_list( Config* This, const char* name, Config** &value, int& size, int& allocated );
134 int atlas__Config__get_int( Config* This, const char* name, int& value );
135 int atlas__Config__get_long( Config* This, const char* name, long& value );
136 int atlas__Config__get_float( Config* This, const char* name, float& value );
137 int atlas__Config__get_double( Config* This, const char* name, double& value );
138 int atlas__Config__get_string( Config* This, const char* name, char*& value, int& size, int& allocated );
139 int atlas__Config__get_array_int( Config* This, const char* name, int*& value, int& size, int& allocated );
140 int atlas__Config__get_array_long( Config* This, const char* name, long*& value, int& size, int& allocated );
141 int atlas__Config__get_array_float( Config* This, const char* name, float*& value, int& size, int& allocated );
142 int atlas__Config__get_array_double( Config* This, const char* name, double*& value, int& size, int& allocated );
143 void atlas__Config__json( Config* This, char*& json, int& size, int& allocated );
144 }
145 // clang-format on
146 // ------------------------------------------------------------------
147 
148 class NoConfig : public Config {
149 public: // methods
151  virtual ~NoConfig() {}
152 
153  virtual bool has( const std::string& name ) const { return false; }
154 
155  virtual bool get( const std::string& name, std::string& value ) const { return false; }
156  virtual bool get( const std::string& name, bool& value ) const { return false; }
157  virtual bool get( const std::string& name, int& value ) const { return false; }
158  virtual bool get( const std::string& name, long& value ) const { return false; }
159  virtual bool get( const std::string& name, long long& value ) const { return false; }
160  virtual bool get( const std::string& name, std::size_t& value ) const { return false; }
161  virtual bool get( const std::string& name, float& value ) const { return false; }
162  virtual bool get( const std::string& name, double& value ) const { return false; }
163 
164  virtual bool get( const std::string& name, std::vector<std::string>& value ) const { return false; }
165  virtual bool get( const std::string& name, std::vector<int>& value ) const { return false; }
166  virtual bool get( const std::string& name, std::vector<long>& value ) const { return false; }
167  virtual bool get( const std::string& name, std::vector<long long>& value ) const { return false; }
168  virtual bool get( const std::string& name, std::vector<std::size_t>& value ) const { return false; }
169  virtual bool get( const std::string& name, std::vector<float>& value ) const { return false; }
170  virtual bool get( const std::string& name, std::vector<double>& value ) const { return false; }
171 };
172 
173 } // namespace util
174 } // namespace atlas
virtual ~NoConfig()
Destructor redundant but fixes sanity compiler warnings.
Definition: Config.h:151
Definition: Domain.h:19
Config operator()(const std::string &name, const ValueT &value)
Operator that sets a key-value pair.
Definition: Config.h:94
Contains all atlas classes and methods.
Definition: atlas-grids.cc:33
Definition: Config.h:148
Configuration class used to construct various atlas components.
Definition: Config.h:27