$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 #ifndef FABLE_ENVIRONMENT_HPP_
26 #define FABLE_ENVIRONMENT_HPP_
27 
28 #include <cassert> // for assert
29 #include <map> // for map<>
30 #include <string> // for string
31 #include <utility> // for pair<>, move, make_pair
32 
33 #include <boost/optional.hpp> // for optional<>
34 
35 namespace fable {
36 
37 class Environment {
38  public:
39  Environment() = default;
40  Environment(std::initializer_list<std::pair<std::string const, std::string>> init)
41  : defines_(init) {}
42  Environment(const std::map<std::string, std::string>& defines) : defines_(defines) {}
43  Environment(std::map<std::string, std::string>&& defines) : defines_(std::move(defines)) {}
44  ~Environment() = default;
45 
46  bool prefer_external() const { return prefer_external_; }
47  void prefer_external(bool value) { prefer_external_ = value; }
48 
49  bool allow_undefined() const { return allow_undefined_; }
50  void allow_undefined(bool value) { allow_undefined_ = value; }
51 
52  void insert(const std::string& key, const std::string& value) {
53  assert(defines_.count(key) == 0);
54  defines_.insert(std::make_pair(key, value));
55  }
56 
57  void set(const std::string& key, const std::string& value) { defines_[key] = value; }
58 
68  boost::optional<std::string> get(const std::string& key) const {
69  return get(key, prefer_external_);
70  }
71  boost::optional<std::string> get(const std::string& key, bool prefer_external) const;
72 
81  std::string get_or(const std::string& key, const std::string& alternative) const {
82  return get(key).value_or(alternative);
83  }
84  std::string get_or(const std::string& key, const std::string& alternative,
85  bool prefer_external) const {
86  return get(key, prefer_external).value_or(alternative);
87  }
88 
97  std::string require(const std::string& key) const { return require(key, prefer_external_); }
98  std::string require(const std::string& key, bool prefer_external) const;
99 
106  std::string evaluate(const std::string& s) const {
107  return evaluate(s, prefer_external_, allow_undefined_);
108  }
109  std::string evaluate(const std::string& s, bool prefer_external, bool allow_undefined) const;
110 
117  std::string interpolate(const std::string& s) const {
118  return interpolate(s, prefer_external_, allow_undefined_);
119  }
120  std::string interpolate(const std::string& s, bool prefer_external, bool allow_undefined) const;
121 
122  private:
123  bool prefer_external_{true};
124  bool allow_undefined_{false};
125  std::map<std::string, std::string> defines_;
126 };
127 
128 inline std::string interpolate_vars(const std::string& s, const Environment* env = nullptr) {
129  if (env != nullptr) {
130  return env->interpolate(s);
131  } else {
132  return Environment().interpolate(s);
133  }
134 }
135 
136 } // namespace fable
137 
138 #endif // FABLE_ENVIRONMENT_HPP_
Definition: conf.hpp:70
Definition: environment.hpp:37
std::string require(const std::string &key) const
Definition: environment.hpp:97
std::string interpolate(const std::string &s) const
Definition: environment.hpp:117
std::string evaluate(const std::string &s) const
Definition: environment.hpp:106
std::string get_or(const std::string &key, const std::string &alternative) const
Definition: environment.hpp:81