mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Minor changes to the Validator
This commit is contained in:
+89
-88
@@ -16,130 +16,131 @@ namespace rfl {
|
||||
/// Used to define a field in the NamedTuple.
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
struct Field {
|
||||
/// The underlying type.
|
||||
using Type = _Type;
|
||||
/// The underlying type.
|
||||
using Type = _Type;
|
||||
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
/// The name of the field.
|
||||
using Name = rfl::Literal<_name>;
|
||||
|
||||
Field(const Type& _value) : value_(_value) {}
|
||||
Field(const Type& _value) : value_(_value) {}
|
||||
|
||||
Field(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
Field(Type&& _value) noexcept : value_(std::move(_value)) {}
|
||||
|
||||
Field(Field<_name, _Type>&& _field) noexcept = default;
|
||||
Field(Field<_name, _Type>&& _field) noexcept = default;
|
||||
|
||||
Field(const Field<_name, Type>& _field) = default;
|
||||
Field(const Field<_name, Type>& _field) = default;
|
||||
|
||||
template <class T>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.get()) {}
|
||||
template <class T>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class T>
|
||||
Field(Field<_name, T>&& _field) : value_(_field.get()) {}
|
||||
template <class T>
|
||||
Field(Field<_name, T>&& _field) : value_(_field.get()) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const T& _value) : value_(_value) {}
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const T& _value) : value_(_value) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(T&& _value) noexcept : value_(std::forward<T>(_value)) {}
|
||||
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.value()) {}
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
Field(const Field<_name, T>& _field) : value_(_field.value()) {}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
Field(const Default& _default) : value_(Type()) {}
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
Field(const Default& _default) : value_(Type()) {}
|
||||
|
||||
~Field() = default;
|
||||
~Field() = default;
|
||||
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
/// The name of the field, for internal use.
|
||||
constexpr static const internal::StringLiteral name_ = _name;
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
/// Returns the underlying object.
|
||||
const Type& get() const { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
/// Returns the underlying object.
|
||||
Type& operator()() { return value_; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& operator()() const { return value_; }
|
||||
/// Returns the underlying object.
|
||||
const Type& operator()() const { return value_; }
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const Type& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(Type&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(Type&& _value) noexcept {
|
||||
value_ = std::move(_value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
template <class T, typename std::enable_if<std::is_convertible_v<T, Type>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = _value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) = default;
|
||||
/// Assigns the underlying object to its default value.
|
||||
template <class T = Type,
|
||||
typename std::enable_if<std::is_default_constructible_v<T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const Default& _default) {
|
||||
value_ = Type();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) = default;
|
||||
/// Assigns the underlying object.
|
||||
Field<_name, _Type>& operator=(const Field<_name, _Type>& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
auto& operator=(const Field<_name, T>& _field) {
|
||||
value_ = _field.get();
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
Field<_name, _Type>& operator=(Field<_name, _Type>&& _field) = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
auto& operator=(Field<_name, T>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
auto& operator=(const Field<_name, T>& _field) {
|
||||
value_ = _field.get();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(const Type& _value) { value_ = _value; }
|
||||
/// Assigns the underlying object.
|
||||
template <class T>
|
||||
auto& operator=(Field<_name, T>&& _field) {
|
||||
value_ = std::forward<T>(_field.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
void set(Type&& _value) { value_ = std::move(_value); }
|
||||
/// Assigns the underlying object.
|
||||
void set(const Type& _value) { value_ = _value; }
|
||||
|
||||
/// Returns the underlying object.
|
||||
Type& value() { return value_; }
|
||||
/// Assigns the underlying object.
|
||||
void set(Type&& _value) { value_ = std::move(_value); }
|
||||
|
||||
/// Returns the underlying object.
|
||||
const Type& value() const { return value_; }
|
||||
/// Returns the underlying object.
|
||||
Type& value() { return value_; }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
/// Returns the underlying object.
|
||||
const Type& value() const { return value_; }
|
||||
|
||||
/// The underlying value.
|
||||
Type value_;
|
||||
};
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto make_field(_Type&& _value) {
|
||||
return Field<_name, _Type>(std::forward<_Type>(_value));
|
||||
return Field<_name, _Type>(std::forward<_Type>(_value));
|
||||
}
|
||||
|
||||
template <internal::StringLiteral _name, class _Type>
|
||||
inline auto make_field(const _Type& _value) {
|
||||
return Field<_name, _Type>(_value);
|
||||
return Field<_name, _Type>(_value);
|
||||
}
|
||||
|
||||
} // namespace rfl
|
||||
|
||||
@@ -19,6 +19,15 @@ struct Validator {
|
||||
public:
|
||||
using ReflectionType = T;
|
||||
|
||||
/// Exception-free validation.
|
||||
static Result<Validator<T, V>> from_value(const T& _value) noexcept {
|
||||
try {
|
||||
return Validator<T, V>(_value);
|
||||
} catch (std::exception& e) {
|
||||
return Error(e.what());
|
||||
}
|
||||
}
|
||||
|
||||
Validator() : value_(V::validate(T()).value()) {}
|
||||
|
||||
Validator(const T& _value) : value_(V::validate(_value).value()) {}
|
||||
@@ -27,21 +36,38 @@ struct Validator {
|
||||
bool>::type = true>
|
||||
Validator(const U& _value) : value_(V::validate(T(_value)).value()) {}
|
||||
|
||||
~Validator() = default;
|
||||
|
||||
/// Assigns the underlying object.
|
||||
auto& operator=(const T& _value) {
|
||||
value_ = V::validate(_value).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
Validator(U&& _value)
|
||||
: value_(V::validate(T(std::forward<U>(_value))).value()) {}
|
||||
auto& operator=(U&& _value) {
|
||||
value_ = V::validate(T(std::forward<T>(_value))).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
~Validator() = default;
|
||||
/// Assigns the underlying object.
|
||||
template <class U, typename std::enable_if<std::is_convertible_v<U, T>,
|
||||
bool>::type = true>
|
||||
auto& operator=(const U& _value) {
|
||||
value_ = V::validate(T(_value)).value();
|
||||
return *this;
|
||||
}
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T& value() { return value_; }
|
||||
|
||||
/// Exposes the underlying value.
|
||||
T value() const { return value_; }
|
||||
const T& value() const { return value_; }
|
||||
|
||||
/// Necessary for the serialization to work.
|
||||
T reflection() const { return value_; }
|
||||
const T& reflection() const { return value_; }
|
||||
|
||||
private:
|
||||
/// The underlying value.
|
||||
|
||||
Reference in New Issue
Block a user