Revert "Disable nil_value() for enum and remove NilValue<Enum>."

This reverts commit 58e32e173b76450ead43f095aee1f8650aae2fad.
This commit is contained in:
Lukas Matena 2024-03-19 14:47:32 +01:00
parent 3e98afa71d
commit db7a89b183

View File

@ -324,6 +324,7 @@ typedef const ConfigOption* ConfigOptionConstPtr;
template<class T, class En = void> struct NilValueTempl template<class T, class En = void> struct NilValueTempl
{ {
using NilType = T; using NilType = T;
static_assert(always_false<T>::value, "Type has no well defined nil value");
}; };
template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_integral_v<T>, void>> { template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_integral_v<T>, void>> {
@ -333,6 +334,14 @@ template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_integral_v<T>
template<> struct NilValueTempl<bool> : public NilValueTempl<int>{}; template<> struct NilValueTempl<bool> : public NilValueTempl<int>{};
// For enums the nil is the max value of the underlying type.
template<class T>
struct NilValueTempl<T, std::enable_if_t<std::is_enum_v<T>, void>>
{
using NilType = T;
static constexpr auto value = static_cast<T>(std::numeric_limits<std::underlying_type_t<T>>::max());
};
template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_floating_point_v<T>, void>> { template<class T> struct NilValueTempl<T, std::enable_if_t<std::is_floating_point_v<T>, void>> {
using NilType = T; using NilType = T;
static constexpr auto value = std::numeric_limits<T>::quiet_NaN(); static constexpr auto value = std::numeric_limits<T>::quiet_NaN();
@ -366,7 +375,7 @@ public:
T value; T value;
explicit ConfigOptionSingle(T value) : value(std::move(value)) {} explicit ConfigOptionSingle(T value) : value(std::move(value)) {}
operator T() const { return this->value; } operator T() const { return this->value; }
void set(const ConfigOption *rhs) override void set(const ConfigOption *rhs) override
{ {
if (rhs->type() != this->type()) if (rhs->type() != this->type())
@ -431,8 +440,7 @@ public:
bool nullable() const override { return NULLABLE; } bool nullable() const override { return NULLABLE; }
template<bool IsNullable = NULLABLE, typename std::enable_if_t<IsNullable, bool> = true> static constexpr NilType<T> nil_value() { return NilValue<T>(); }
static constexpr NilType<T> nil_value() { return NilType<T>(); }
// A scalar is nil, or all values of a vector are nil. // A scalar is nil, or all values of a vector are nil.
bool is_nil() const override bool is_nil() const override
@ -709,7 +717,7 @@ public:
std::istringstream iss(str); std::istringstream iss(str);
if (str == "nil") { if (str == "nil") {
if constexpr (NULLABLE) if (NULLABLE)
this->value = this->nil_value(); this->value = this->nil_value();
else else
throw ConfigurationError("Deserializing nil into a non-nullable object"); throw ConfigurationError("Deserializing nil into a non-nullable object");
@ -882,8 +890,7 @@ public:
std::string serialize() const override std::string serialize() const override
{ {
std::ostringstream ss; std::ostringstream ss;
if (this->value == this->nil_value()) {
if (this->value == std::numeric_limits<int>::max()) {
if (NULLABLE) if (NULLABLE)
ss << "nil"; ss << "nil";
else else
@ -900,7 +907,7 @@ public:
std::istringstream iss(str); std::istringstream iss(str);
if (str == "nil") { if (str == "nil") {
if constexpr (NULLABLE) if (NULLABLE)
this->value = this->nil_value(); this->value = this->nil_value();
else else
throw ConfigurationError("Deserializing nil into a non-nullable object"); throw ConfigurationError("Deserializing nil into a non-nullable object");