33#include <fmt/format.h>
45 std::variant<bool, std::intmax_t, std::uintmax_t, long double, std::string>;
59 [[nodiscard]]
static auto get_type() ->
const std::type_info& {
69 [[nodiscard]]
static auto to_string(
const std::shared_ptr<void>& data)
72 fmt::format(
"{}", *
static_cast<const T*
>(data.get())));
81 [[nodiscard]]
static auto to_double(
const std::shared_ptr<void>& data)
83 if constexpr (std::is_convertible_v<T, double>) {
84 return static_cast<double>(*
static_cast<const T*
>(data.get()));
87 fmt::format(
"Cannot convert {} to double.",
typeid(T).name()));
97 [[nodiscard]]
static auto to_variant(
const std::shared_ptr<void>& data)
99 if constexpr (std::is_same_v<T, bool>) {
100 return *
static_cast<const bool*
>(data.get());
101 }
else if constexpr (std::is_integral_v<T> && std::is_signed_v<T>) {
102 return static_cast<std::intmax_t
>(
103 *
static_cast<const T*
>(data.get()));
104 }
else if constexpr (std::is_integral_v<T> && std::is_unsigned_v<T>) {
105 return static_cast<std::uintmax_t
>(
106 *
static_cast<const T*
>(data.get()));
107 }
else if constexpr (std::is_floating_point_v<T>) {
108 return static_cast<long double>(*
static_cast<const T*
>(data.get()));
109 }
else if constexpr (std::is_same_v<T, std::string>) {
110 return *
static_cast<const std::string*
>(data.get());
112 return fmt::format(
"{}", *
static_cast<const T*
>(data.get()));
124 return std::hash<std::type_index>{}(std::type_index(
typeid(T))) ^
125 std::hash<T>{}(*
static_cast<const T*
>(data.get()));
136 [[nodiscard]]
static auto equal(
const std::shared_ptr<void>& data1,
137 const std::shared_ptr<void>& data2) ->
bool {
138 return *
static_cast<const T*
>(data1.get()) ==
139 *
static_cast<const T*
>(data2.get());
161 template <typename T, typename... Args>
163 data_ = std::make_shared<T>(std::forward<Args>(args)...);
176 void clear() noexcept { data_.reset(); }
184 template <
typename T>
189 return get_type_() ==
typeid(T);
198 template <
typename T>
199 [[nodiscard]]
auto as() const -> const T& {
202 "Tried to get value from empty ParameterValue object.");
206 "Wrong type of parameter value (expected: {}, actual: {}).",
207 typeid(T).name(), get_type_().name()));
209 return *
static_cast<const T*
>(data_.get());
221 return to_string_(data_);
232 "Tried to convert an empty ParameterValue object to double.");
234 return to_double_(data_);
245 "Tried to convert an empty ParameterValue object to a variant "
248 return to_variant_(data_);
260 return calculate_hash_(data_);
271 if (!data_ && !rhs.data_) {
274 if (!data_ || !rhs.data_) {
277 if (get_type_() != rhs.get_type_()) {
280 return equal_(data_, rhs.data_);
291 return !(*
this == rhs);
296 using GetTypeSignature =
const std::type_info&();
302 using ToDoubleSignature = double(
const std::shared_ptr<void>&);
306 const std::shared_ptr<void>&);
309 using CalculateHashSignature = std::size_t(
const std::shared_ptr<void>&);
312 using EqualSignature = bool(
313 const std::shared_ptr<void>&,
const std::shared_ptr<void>&);
316 std::shared_ptr<void> data_{};
319 GetTypeSignature* get_type_{
nullptr};
322 ToStringSignature* to_string_{
nullptr};
325 ToDoubleSignature* to_double_{
nullptr};
328 ToVariantSignature* to_variant_{
nullptr};
331 CalculateHashSignature* calculate_hash_{
nullptr};
334 EqualSignature* equal_{
nullptr};
356 return val.calculate_hash();
Class of exceptions in this library.
Class of values of parameters.
auto as() const -> const T &
Get value.
void clear() noexcept
Clear data.
auto calculate_hash() const -> std::size_t
Calculate hash value.
auto operator!=(const ParameterValue &rhs) const -> bool
Check whether two values are not equal.
auto operator==(const ParameterValue &rhs) const -> bool
Check whether two values are equal.
ParameterValue() noexcept=default
Constructor.
auto to_variant() const -> ParameterValueVariant
Convert to a variant object.
auto to_string() const -> util::Utf8String
Format to string.
auto emplace(Args &&... args) -> ParameterValue &
Initialize data.
auto is_type_of() const -> bool
Check data type.
auto to_double() const -> double
Convert to double.
Class of UTF-8 encoded string.
auto operator()(const stat_bench::param::ParameterValue &val) const -> std::size_t
Hash function.
Namespace of parameters of benchmarks.
std::variant< bool, std::intmax_t, std::uintmax_t, long double, std::string > ParameterValueVariant
Type of variant of parameter values.
Namespace of utility functions and classes.
Namespace of stat_bench source codes.
Definition of StatBenchException class.
Class of traits of parameter values.
static auto to_double(const std::shared_ptr< void > &data) -> double
Convert to double.
static auto equal(const std::shared_ptr< void > &data1, const std::shared_ptr< void > &data2) -> bool
Check whether two values are equal.
static auto calculate_hash(const std::shared_ptr< void > &data) -> std::size_t
Calculate hash value.
static auto get_type() -> const std::type_info &
Get the type.
static auto to_string(const std::shared_ptr< void > &data) -> util::Utf8String
Format to string.
static auto to_variant(const std::shared_ptr< void > &data) -> ParameterValueVariant
Convert to a variant object.
Definition of Utf8String class.