2024-05-31 22:59:00 -04:00
|
|
|
#ifndef RFL_INTERNAL_SKIP_HPP_
|
|
|
|
#define RFL_INTERNAL_SKIP_HPP_
|
|
|
|
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace rfl::internal {
|
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class T, bool _skip_serialization, bool _skip_deserialization>
|
|
|
|
class Skip {
|
|
|
|
private:
|
|
|
|
using SelfType = Skip<T, _skip_serialization, _skip_deserialization>;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
public:
|
|
|
|
static constexpr bool skip_serialization_ = _skip_serialization;
|
|
|
|
static constexpr bool skip_deserialization_ = _skip_deserialization;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// The underlying type.
|
|
|
|
using Type = T;
|
|
|
|
using ReflectionType = std::optional<T>;
|
|
|
|
|
|
|
|
Skip() : value_(Type()) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
Skip(const Type& _value) : value_(_value) {}
|
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
Skip(ReflectionType&& _value) noexcept : value_(_value ? std::move(*_value) : Type()) {}
|
2024-06-08 14:10:59 -04:00
|
|
|
|
|
|
|
Skip(const ReflectionType& _value) : value_(_value ? *_value : Type()) {}
|
|
|
|
|
|
|
|
Skip(Type&& _value) noexcept : value_(std::move(_value)) {}
|
|
|
|
|
|
|
|
Skip(SelfType&& _skip) noexcept = default;
|
|
|
|
|
|
|
|
Skip(const SelfType& _skip) = default;
|
|
|
|
|
|
|
|
template <class U, bool _skip_s, bool _skip_d>
|
|
|
|
Skip(const Skip<U, _skip_s, _skip_d>& _other) : value_(_other.get()) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
template <class U, bool _skip_s, bool _skip_d>
|
|
|
|
Skip(Skip<U, _skip_s, _skip_d>&& _other) : value_(_other.get()) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
Skip(const U& _value) : value_(_value) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-16 00:13:15 -04:00
|
|
|
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
Skip(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 15:53:06 -04:00
|
|
|
template <
|
2024-06-16 00:13:15 -04:00
|
|
|
class U,
|
|
|
|
bool _skip_s,
|
|
|
|
bool _skip_d,
|
|
|
|
typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
Skip(const Skip<U, _skip_s, _skip_d>& _skip) : value_(_skip.value()) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object to its default value.
|
2024-06-08 15:53:06 -04:00
|
|
|
template <
|
2024-06-16 00:13:15 -04:00
|
|
|
class U = Type,
|
|
|
|
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
Skip(const Default& _default) : value_(Type()) {}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
~Skip() = default;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Returns the underlying object.
|
|
|
|
Type& get() { return value_; }
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Returns the underlying object.
|
|
|
|
const Type& get() const { return value_; }
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Returns the underlying object.
|
|
|
|
Type& operator()() { return value_; }
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Returns the underlying object.
|
|
|
|
const Type& operator()() const { return value_; }
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
auto& operator=(const Type& _value) {
|
|
|
|
value_ = _value;
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
auto& operator=(Type&& _value) noexcept {
|
|
|
|
value_ = std::move(_value);
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
2024-06-16 00:13:15 -04:00
|
|
|
template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
auto& operator=(const U& _value) {
|
|
|
|
value_ = _value;
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object to its default value.
|
2024-06-08 15:53:06 -04:00
|
|
|
template <
|
2024-06-16 00:13:15 -04:00
|
|
|
class U = Type,
|
|
|
|
typename std::enable_if<std::is_default_constructible_v<U>, bool>::type = true>
|
2024-06-08 14:10:59 -04:00
|
|
|
auto& operator=(const Default& _default) {
|
|
|
|
value_ = Type();
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
SelfType& operator=(const SelfType& _other) = default;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
SelfType& operator=(SelfType&& _other) = default;
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
template <class U, bool _skip_s, bool _skip_d>
|
|
|
|
auto& operator=(const Skip<U, _skip_s, _skip_d>& _skip) {
|
|
|
|
value_ = _skip.get();
|
|
|
|
return *this;
|
|
|
|
}
|
2024-05-31 22:59:00 -04:00
|
|
|
|
2024-06-08 14:10:59 -04:00
|
|
|
/// Assigns the underlying object.
|
|
|
|
template <class U, bool _skip_s, bool _skip_d>
|
|
|
|
auto& operator=(Skip<U, _skip_s, _skip_d>&& _skip) {
|
|
|
|
value_ = std::forward<T>(_skip.value_);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the ReflectionType - necessary for the serialization to work.
|
|
|
|
ReflectionType reflection() const { return value_; }
|
|
|
|
|
|
|
|
/// Assigns the underlying object.
|
|
|
|
void set(const Type& _value) { value_ = _value; }
|
|
|
|
|
|
|
|
/// Assigns the underlying object.
|
|
|
|
void set(Type&& _value) { value_ = std::move(_value); }
|
|
|
|
|
|
|
|
/// Returns the underlying object.
|
|
|
|
Type& value() { return value_; }
|
|
|
|
|
|
|
|
/// Returns the underlying object.
|
|
|
|
const Type& value() const { return value_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
/// The underlying value
|
|
|
|
T value_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rfl::internal
|
2024-05-31 22:59:00 -04:00
|
|
|
|
|
|
|
#endif
|