16 #include <string_view>
17 #include <unordered_map>
19 #include <rapidsmpf/config_defaults.hpp>
20 #include <rapidsmpf/error.hpp>
21 #include <rapidsmpf/utils/string.hpp>
60 : value_as_string_{std::move(value_as_string)} {}
74 requires(!std::is_convertible_v<T, std::string_view>)
75 : value_{std::make_any<T>(std::move(value))} {}
94 return value_as_string_;
106 !value_.has_value(),
"value already set", std::invalid_argument
108 value_ = std::move(value);
113 std::string value_as_string_{};
128 std::unordered_map<std::string, OptionValue>
options;
153 Options(std::unordered_map<std::string, OptionValue> options = {});
163 Options(std::unordered_map<std::string, std::string> options_as_strings);
191 std::unordered_map<std::string, std::string> options_as_strings
218 template <
typename T>
220 requires(!std::is_convertible_v<T, std::string_view>)
222 std::lock_guard<std::mutex> lock(shared_->mutex);
223 auto [_, inserted] = shared_->options.try_emplace(
248 template <
typename T>
250 auto& shared = *shared_;
251 std::lock_guard<std::mutex> lock(shared.mutex);
252 auto& option = shared.options[key];
253 if (!option.get_value().has_value()) {
257 std::string
const& stored = option.get_value_as_string();
259 std::string
const& effective =
260 (stored.empty() && it !=
DEFAULTS.end()) ? it->second : stored;
261 option.set_value(std::make_any<T>(factory(effective)));
264 return std::any_cast<T const&>(option.get_value());
265 }
catch (
const std::bad_any_cast&) {
267 "accessing option with incompatible template type", std::invalid_argument
284 [[nodiscard]] std::unordered_map<std::string, std::string>
get_strings()
const;
322 [[nodiscard]] std::vector<std::uint8_t>
serialize()
const;
338 std::shared_ptr<detail::SharedOptions> shared_;
370 std::unordered_map<std::string, std::string>& output,
371 std::string
const& key_regex =
"RAPIDSMPF_(.*)"
391 std::string
const& key_regex =
"RAPIDSMPF_(.*)"
Configuration option value.
OptionValue(std::string value_as_string)
Constructs OptionValue from a string representation.
std::any const & get_value() const
Retrieves the stored value.
std::string const & get_value_as_string() const
Retrieves the string representation of the value.
OptionValue()=default
Default constructor.
void set_value(std::any value)
Sets the value if it has not been set already.
OptionValue(T value) requires(!std
Constructs OptionValue from a typed value.
Manages configuration options for RapidsMPF operations.
bool insert_if_absent(std::string const &key, T value) requires(!std
Inserts an option only if it is not already present.
static Options deserialize(std::vector< std::uint8_t > const &buffer)
Deserializes a binary buffer into an Options object.
T const & get(std::string const &key, OptionFactory< T > factory)
Retrieves a configuration option by key.
std::vector< std::uint8_t > serialize() const
Serializes the options into a binary buffer.
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.
bool insert_if_absent(std::string const &key, std::string_view option_as_string)
Inserts an option only if it is not already present.
const std::unordered_map< std::string, std::string > DEFAULTS
String-form default values for config options.
std::function< T(std::string const &)> OptionFactory
Type alias for a factory function that constructs options from strings.
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.
std::string to_lower(std::string_view text)
Converts the specified string to lowercase.
std::string trim(std::string_view text)
Trims whitespace from both ends of the specified string.
Internal shared collection for the Options class.
std::mutex mutex
Shared mutex, must be use to guard options.
std::unordered_map< std::string, OptionValue > options
Shared options.