14 #include <unordered_map>
16 #include <rapidsmpf/error.hpp>
17 #include <rapidsmpf/utils/string.hpp>
56 : value_as_string_{std::move(value_as_string)} {}
70 requires(!std::is_convertible_v<T, std::string_view>)
71 : value_{std::make_any<T>(std::move(value))} {}
90 return value_as_string_;
102 !value_.has_value(),
"value already set", std::invalid_argument
104 value_ = std::move(value);
109 std::string value_as_string_{};
124 std::unordered_map<std::string, OptionValue>
options;
149 Options(std::unordered_map<std::string, OptionValue> options = {});
159 Options(std::unordered_map<std::string, std::string> options_as_strings);
187 std::unordered_map<std::string, std::string> options_as_strings
214 template <
typename T>
216 requires(!std::is_convertible_v<T, std::string_view>)
218 std::lock_guard<std::mutex> lock(shared_->mutex);
219 auto [_, inserted] = shared_->options.try_emplace(
244 template <
typename T>
246 auto& shared = *shared_;
247 std::lock_guard<std::mutex> lock(shared.mutex);
248 auto& option = shared.options[key];
249 if (!option.get_value().has_value()) {
250 option.set_value(std::make_any<T>(factory(option.get_value_as_string())));
253 return std::any_cast<T const&>(option.get_value());
254 }
catch (
const std::bad_any_cast&) {
256 "accessing option with incompatible template type", std::invalid_argument
273 [[nodiscard]] std::unordered_map<std::string, std::string>
get_strings()
const;
311 [[nodiscard]] std::vector<std::uint8_t>
serialize()
const;
327 std::shared_ptr<detail::SharedOptions> shared_;
359 std::unordered_map<std::string, std::string>& output,
360 std::string
const& key_regex =
"RAPIDSMPF_(.*)"
380 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.
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.