config.hpp
1 
6 #pragma once
7 
8 #include <any>
9 #include <functional>
10 #include <memory>
11 #include <mutex>
12 #include <stdexcept>
13 #include <string>
14 #include <unordered_map>
15 
16 #include <rapidsmpf/error.hpp>
17 
18 namespace rapidsmpf::config {
19 
31 template <typename T>
32 using OptionFactory = std::function<T(std::string const&)>;
33 
40 class OptionValue {
41  public:
47  OptionValue() = default;
48 
54  OptionValue(std::string value_as_string)
55  : value_as_string_{std::move(value_as_string)} {}
56 
62  [[nodiscard]] std::any const& get_value() const {
63  return value_;
64  }
65 
73  [[nodiscard]] std::string const& get_value_as_string() const {
74  return value_as_string_;
75  }
76 
84  void set_value(std::any value) {
85  RAPIDSMPF_EXPECTS(
86  !value_.has_value(), "value already set", std::invalid_argument
87  );
88  value_ = std::move(value);
89  }
90 
91  private:
92  std::any value_{};
93  std::string value_as_string_{};
94  // TODO: add a collective policy.
95 };
96 
97 namespace detail {
98 
107  mutable std::mutex mutex;
108  std::unordered_map<std::string, OptionValue> options;
109 };
110 } // namespace detail
111 
124 class Options {
125  public:
133  Options(std::unordered_map<std::string, OptionValue> options = {});
134 
135 
143  Options(std::unordered_map<std::string, std::string> options_as_strings);
144 
158  bool insert_if_absent(std::string const& key, std::string option_as_string);
159 
170  std::size_t insert_if_absent(
171  std::unordered_map<std::string, std::string> options_as_strings
172  );
173 
193  template <typename T>
194  T const& get(const std::string& key, OptionFactory<T> factory) {
195  auto& shared = *shared_;
196  std::lock_guard<std::mutex> lock(shared.mutex);
197  auto& option = shared.options[key];
198  if (!option.get_value().has_value()) {
199  option.set_value(std::make_any<T>(factory(option.get_value_as_string())));
200  }
201  try {
202  return std::any_cast<const T&>(option.get_value());
203  } catch (const std::bad_any_cast&) {
204  RAPIDSMPF_FAIL(
205  "accessing option with incompatible template type", std::invalid_argument
206  );
207  }
208  }
209 
219  [[nodiscard]] std::unordered_map<std::string, std::string> get_strings() const;
220 
250  [[nodiscard]] std::vector<std::uint8_t> serialize() const;
251 
263  [[nodiscard]] static Options deserialize(std::vector<std::uint8_t> const& buffer);
264 
265  private:
266  std::shared_ptr<detail::SharedOptions> shared_;
267 };
268 
298  std::unordered_map<std::string, std::string>& output,
299  std::string const& key_regex = "RAPIDSMPF_(.*)"
300 );
301 
318 std::unordered_map<std::string, std::string> get_environment_variables(
319  std::string const& key_regex = "RAPIDSMPF_(.*)"
320 );
321 
322 } // namespace rapidsmpf::config
Configuration option value.
Definition: config.hpp:40
OptionValue(std::string value_as_string)
Constructs OptionValue from a string representation.
Definition: config.hpp:54
std::any const & get_value() const
Retrieves the stored value.
Definition: config.hpp:62
std::string const & get_value_as_string() const
Retrieves the string representation of the value.
Definition: config.hpp:73
OptionValue()=default
Default constructor.
void set_value(std::any value)
Sets the value if it has not been set already.
Definition: config.hpp:84
Manages configuration options for RapidsMPF operations.
Definition: config.hpp:124
T const & get(const std::string &key, OptionFactory< T > factory)
Retrieves a configuration option by key.
Definition: config.hpp:194
static Options deserialize(std::vector< std::uint8_t > const &buffer)
Deserializes a binary buffer into an Options object.
std::vector< std::uint8_t > serialize() const
Serializes the options into a binary buffer.
bool insert_if_absent(std::string const &key, std::string option_as_string)
Inserts an option only if it is not already present.
Options(std::unordered_map< std::string, OptionValue > options={})
Constructs an Options instance from option values.
std::unordered_map< std::string, std::string > get_strings() const
Retrieves all option values as strings.
Options(std::unordered_map< std::string, std::string > options_as_strings)
Constructs an Options instance from option values as strings.
std::size_t insert_if_absent(std::unordered_map< std::string, std::string > options_as_strings)
Inserts multiple options if they are not already present.
std::function< T(std::string const &)> OptionFactory
Type alias for a factory function that constructs options from strings.
Definition: config.hpp:32
void get_environment_variables(std::unordered_map< std::string, std::string > &output, std::string const &key_regex="RAPIDSMPF_(.*)")
Populates a map with environment variables matching a given regular expression.
Internal shared collection for the Options class.
Definition: config.hpp:106
std::mutex mutex
Shared mutex, must be use to guard options.
Definition: config.hpp:107
std::unordered_map< std::string, OptionValue > options
Shared options.
Definition: config.hpp:108