14 #include <unordered_map>
16 #include <rapidsmpf/error.hpp>
55 : value_as_string_{std::move(value_as_string)} {}
74 return value_as_string_;
86 !value_.has_value(),
"value already set", std::invalid_argument
88 value_ = std::move(value);
93 std::string value_as_string_{};
108 std::unordered_map<std::string, OptionValue>
options;
133 Options(std::unordered_map<std::string, OptionValue> options = {});
143 Options(std::unordered_map<std::string, std::string> options_as_strings);
171 std::unordered_map<std::string, std::string> options_as_strings
193 template <
typename T>
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())));
202 return std::any_cast<const T&>(option.get_value());
203 }
catch (
const std::bad_any_cast&) {
205 "accessing option with incompatible template type", std::invalid_argument
219 [[nodiscard]] std::unordered_map<std::string, std::string>
get_strings()
const;
250 [[nodiscard]] std::vector<std::uint8_t>
serialize()
const;
266 std::shared_ptr<detail::SharedOptions> shared_;
298 std::unordered_map<std::string, std::string>& output,
299 std::string
const& key_regex =
"RAPIDSMPF_(.*)"
319 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.
Manages configuration options for RapidsMPF operations.
T const & get(const std::string &key, OptionFactory< T > factory)
Retrieves a configuration option by key.
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.
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.
std::mutex mutex
Shared mutex, must be use to guard options.
std::unordered_map< std::string, OptionValue > options
Shared options.