$darkmode
environment.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
24 #pragma once
25 
26 #include <cassert> // for assert
27 #include <map> // for map<>
28 #include <optional> // for optional<>
29 #include <string> // for string
30 #include <utility> // for pair<>, move, make_pair
31 
32 namespace fable {
33 
34 class Environment {
35  public:
36  ~Environment() = default;
37  Environment() = default;
38  Environment(const Environment&) = default;
39  Environment(Environment&&) = default;
40  Environment& operator=(const Environment&) = default;
41  Environment& operator=(Environment&&) = default;
42 
43  Environment(std::initializer_list<std::pair<std::string const, std::string>> init)
44  : defines_(init) {}
45  Environment(const std::map<std::string, std::string>& defines) : defines_(defines) {}
46  Environment(std::map<std::string, std::string>&& defines) : defines_(std::move(defines)) {}
47 
48  [[nodiscard]] bool prefer_external() const { return prefer_external_; }
49  void prefer_external(bool value) { prefer_external_ = value; }
50 
51  [[nodiscard]] bool allow_undefined() const { return allow_undefined_; }
52  void allow_undefined(bool value) { allow_undefined_ = value; }
53 
54  void insert(const std::string& key, const std::string& value) {
55  assert(defines_.count(key) == 0);
56  defines_.insert(std::make_pair(key, value));
57  }
58 
59  void set(const std::string& key, const std::string& value) { defines_[key] = value; }
60 
70  [[nodiscard]] std::optional<std::string> get(const std::string& key) const {
71  return get(key, prefer_external_);
72  }
73  [[nodiscard]] std::optional<std::string> get(const std::string& key, bool prefer_external) const;
74 
83  [[nodiscard]] std::string get_or(const std::string& key, const std::string& alternative) const {
84  return get(key).value_or(alternative);
85  }
86  [[nodiscard]] std::string get_or(const std::string& key, const std::string& alternative,
87  bool prefer_external) const {
88  return get(key, prefer_external).value_or(alternative);
89  }
90 
99  [[nodiscard]] std::string require(const std::string& key) const {
100  return require(key, prefer_external_);
101  }
102  [[nodiscard]] std::string require(const std::string& key, bool prefer_external) const;
103 
110  [[nodiscard]] std::string evaluate(const std::string& s) const {
111  return evaluate(s, prefer_external_, allow_undefined_);
112  }
113  [[nodiscard]] std::string evaluate(const std::string& s, bool prefer_external,
114  bool allow_undefined) const;
115 
122  [[nodiscard]] std::string interpolate(const std::string& s) const {
123  return interpolate(s, prefer_external_, allow_undefined_);
124  }
125  [[nodiscard]] std::string interpolate(const std::string& s, bool prefer_external,
126  bool allow_undefined) const;
127 
128  private:
129  bool prefer_external_{true};
130  bool allow_undefined_{false};
131  std::map<std::string, std::string> defines_;
132 };
133 
134 inline std::string interpolate_vars(const std::string& s, const Environment* env = nullptr) {
135  if (env != nullptr) {
136  return env->interpolate(s);
137  }
138  return Environment().interpolate(s);
139 }
140 
141 } // namespace fable
Definition: server_test.cpp:72
Definition: environment.hpp:34
std::optional< std::string > get(const std::string &key) const
Definition: environment.hpp:70
std::string evaluate(const std::string &s) const
Definition: environment.hpp:110
std::string get_or(const std::string &key, const std::string &alternative) const
Definition: environment.hpp:83
std::string interpolate(const std::string &s) const
Definition: environment.hpp:122
std::string require(const std::string &key) const
Definition: environment.hpp:99