|
| 1 | +#ifndef RFL_DEFAULTVAL_HPP_ |
| 2 | +#define RFL_DEFAULTVAL_HPP_ |
| 3 | + |
| 4 | +#include <type_traits> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +#include "default.hpp" |
| 8 | + |
| 9 | +namespace rfl { |
| 10 | + |
| 11 | +template <class T> |
| 12 | +struct DefaultVal { |
| 13 | + public: |
| 14 | + using Type = std::remove_cvref_t<T>; |
| 15 | + |
| 16 | + DefaultVal() : value_(Type()) {} |
| 17 | + |
| 18 | + DefaultVal(const Type& _value) : value_(_value) {} |
| 19 | + |
| 20 | + DefaultVal(Type&& _value) noexcept : value_(std::move(_value)) {} |
| 21 | + |
| 22 | + DefaultVal(DefaultVal&& _field) noexcept = default; |
| 23 | + |
| 24 | + DefaultVal(const DefaultVal& _field) = default; |
| 25 | + |
| 26 | + template <class U> |
| 27 | + DefaultVal(const DefaultVal<U>& _field) : value_(_field.get()) {} |
| 28 | + |
| 29 | + template <class U> |
| 30 | + DefaultVal(DefaultVal<U>&& _field) noexcept( |
| 31 | + noexcept(Type(std::move(_field.value())))) |
| 32 | + : value_(std::move(_field.value())) {} |
| 33 | + |
| 34 | + template <class U> |
| 35 | + requires(std::is_convertible_v<U, Type>) |
| 36 | + DefaultVal(const U& _value) : value_(_value) {} |
| 37 | + |
| 38 | + template <class U> |
| 39 | + requires(std::is_convertible_v<U, Type>) |
| 40 | + DefaultVal(U&& _value) noexcept : value_(std::forward<U>(_value)) {} |
| 41 | + |
| 42 | + template <class U> |
| 43 | + requires(std::is_convertible_v<U, Type>) |
| 44 | + DefaultVal(const DefaultVal<U>& _field) : value_(_field.value()) {} |
| 45 | + |
| 46 | + /// Assigns the underlying object to its default value. |
| 47 | + template <class U = Type> |
| 48 | + requires(std::is_default_constructible_v<U>) |
| 49 | + DefaultVal(const Default&) : value_(Type()) {} |
| 50 | + |
| 51 | + ~DefaultVal() = default; |
| 52 | + |
| 53 | + /// Returns the underlying object. |
| 54 | + const Type& get() const { return value_; } |
| 55 | + |
| 56 | + /// Returns the underlying object. |
| 57 | + Type& operator()() { return value_; } |
| 58 | + |
| 59 | + /// Returns the underlying object. |
| 60 | + const Type& operator()() const { return value_; } |
| 61 | + |
| 62 | + /// Assigns the underlying object. |
| 63 | + auto& operator=(const Type& _value) { |
| 64 | + value_ = _value; |
| 65 | + return *this; |
| 66 | + } |
| 67 | + |
| 68 | + /// Assigns the underlying object. |
| 69 | + auto& operator=(Type&& _value) noexcept { |
| 70 | + value_ = std::move(_value); |
| 71 | + return *this; |
| 72 | + } |
| 73 | + |
| 74 | + /// Assigns the underlying object. |
| 75 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 76 | + bool>::type = true> |
| 77 | + auto& operator=(const U& _value) { |
| 78 | + value_ = _value; |
| 79 | + return *this; |
| 80 | + } |
| 81 | + |
| 82 | + /// Assigns the underlying object to its default value. |
| 83 | + template <class U = Type, |
| 84 | + typename std::enable_if<std::is_default_constructible_v<U>, |
| 85 | + bool>::type = true> |
| 86 | + auto& operator=(const Default&) { |
| 87 | + value_ = Type(); |
| 88 | + return *this; |
| 89 | + } |
| 90 | + |
| 91 | + /// Assigns the underlying object. |
| 92 | + DefaultVal& operator=(const DefaultVal& _field) = default; |
| 93 | + |
| 94 | + /// Assigns the underlying object. |
| 95 | + DefaultVal& operator=(DefaultVal&& _field) = default; |
| 96 | + |
| 97 | + /// Assigns the underlying object. |
| 98 | + template <class U> |
| 99 | + auto& operator=(const DefaultVal<U>& _field) { |
| 100 | + value_ = _field.get(); |
| 101 | + return *this; |
| 102 | + } |
| 103 | + |
| 104 | + /// Assigns the underlying object. |
| 105 | + template <class U> |
| 106 | + auto& operator=(DefaultVal<U>&& _field) { |
| 107 | + value_ = std::forward<U>(_field.value_); |
| 108 | + return *this; |
| 109 | + } |
| 110 | + |
| 111 | + /// Assigns the underlying object. |
| 112 | + void set(const Type& _value) { value_ = _value; } |
| 113 | + |
| 114 | + /// Assigns the underlying object. |
| 115 | + void set(Type&& _value) { value_ = std::move(_value); } |
| 116 | + |
| 117 | + /// Returns the underlying object. |
| 118 | + Type& value() { return value_; } |
| 119 | + |
| 120 | + /// Returns the underlying object. |
| 121 | + const Type& value() const { return value_; } |
| 122 | + |
| 123 | + /// The underlying value. |
| 124 | + Type value_; |
| 125 | +}; |
| 126 | + |
| 127 | +} // namespace rfl |
| 128 | + |
| 129 | +#endif |
0 commit comments