|
| 1 | +#ifndef RFL_DEPRECATED_HPP_ |
| 2 | +#define RFL_DEPRECATED_HPP_ |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <string_view> |
| 6 | +#include <tuple> |
| 7 | +#include <type_traits> |
| 8 | +#include <utility> |
| 9 | + |
| 10 | +#include "Literal.hpp" |
| 11 | +#include "default.hpp" |
| 12 | +#include "internal/StringLiteral.hpp" |
| 13 | + |
| 14 | +namespace rfl { |
| 15 | + |
| 16 | +/// Used to mark a field as deprecated in the JSON schema, replacing |
| 17 | +/// rfl::Description for deprecated fields. Includes both a deprecation message |
| 18 | +/// and a description: |
| 19 | +/// rfl::Deprecated<"use X instead", "description of field", T> |
| 20 | +template <internal::StringLiteral _deprecation_message, |
| 21 | + internal::StringLiteral _description, class T> |
| 22 | +struct Deprecated { |
| 23 | + /// The underlying type. |
| 24 | + using Type = T; |
| 25 | + |
| 26 | + /// The deprecation message. |
| 27 | + using DeprecationMessage = rfl::Literal<_deprecation_message>; |
| 28 | + |
| 29 | + /// The description of the field (same role as Description::Content). |
| 30 | + using Content = rfl::Literal<_description>; |
| 31 | + |
| 32 | + using ReflectionType = Type; |
| 33 | + |
| 34 | + Deprecated() : value_(Type()) {} |
| 35 | + |
| 36 | + Deprecated(const Type& _value) : value_(_value) {} |
| 37 | + |
| 38 | + Deprecated(Type&& _value) noexcept : value_(std::move(_value)) {} |
| 39 | + |
| 40 | + Deprecated(Deprecated&& _field) noexcept = default; |
| 41 | + |
| 42 | + Deprecated(const Deprecated& _field) = default; |
| 43 | + |
| 44 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 45 | + bool>::type = true> |
| 46 | + Deprecated(const Deprecated<_deprecation_message, _description, U>& _field) |
| 47 | + : value_(_field.get()) {} |
| 48 | + |
| 49 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 50 | + bool>::type = true> |
| 51 | + Deprecated(Deprecated<_deprecation_message, _description, U>&& _field) |
| 52 | + : value_(std::move(_field.value_)) {} |
| 53 | + |
| 54 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 55 | + bool>::type = true> |
| 56 | + Deprecated(const U& _value) : value_(_value) {} |
| 57 | + |
| 58 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 59 | + bool>::type = true> |
| 60 | + Deprecated(U&& _value) noexcept : value_(std::forward<U>(_value)) {} |
| 61 | + |
| 62 | + /// Assigns the underlying object to its default value. |
| 63 | + template <class U = Type, |
| 64 | + typename std::enable_if<std::is_default_constructible_v<U>, |
| 65 | + bool>::type = true> |
| 66 | + Deprecated(const Default&) : value_(Type()) {} |
| 67 | + |
| 68 | + ~Deprecated() = default; |
| 69 | + |
| 70 | + /// The deprecation message, for internal use. |
| 71 | + constexpr static const internal::StringLiteral deprecation_message_ = |
| 72 | + _deprecation_message; |
| 73 | + |
| 74 | + /// The description, for internal use. |
| 75 | + constexpr static const internal::StringLiteral description_ = _description; |
| 76 | + |
| 77 | + /// Returns the underlying object. |
| 78 | + const Type& get() const { return value_; } |
| 79 | + |
| 80 | + /// Returns the underlying object. |
| 81 | + Type& operator()() { return value_; } |
| 82 | + |
| 83 | + /// Returns the underlying object. |
| 84 | + const Type& operator()() const { return value_; } |
| 85 | + |
| 86 | + /// Assigns the underlying object. |
| 87 | + auto& operator=(const Type& _value) { |
| 88 | + value_ = _value; |
| 89 | + return *this; |
| 90 | + } |
| 91 | + |
| 92 | + /// Assigns the underlying object. |
| 93 | + auto& operator=(Type&& _value) noexcept { |
| 94 | + value_ = std::move(_value); |
| 95 | + return *this; |
| 96 | + } |
| 97 | + |
| 98 | + /// Assigns the underlying object. |
| 99 | + template <class U, typename std::enable_if<std::is_convertible_v<U, Type>, |
| 100 | + bool>::type = true> |
| 101 | + auto& operator=(const U& _value) { |
| 102 | + value_ = _value; |
| 103 | + return *this; |
| 104 | + } |
| 105 | + |
| 106 | + /// Assigns the underlying object to its default value. |
| 107 | + template <class U = Type, |
| 108 | + typename std::enable_if<std::is_default_constructible_v<U>, |
| 109 | + bool>::type = true> |
| 110 | + auto& operator=(const Default&) { |
| 111 | + value_ = Type(); |
| 112 | + return *this; |
| 113 | + } |
| 114 | + |
| 115 | + /// Assigns the underlying object. |
| 116 | + Deprecated& operator=(const Deprecated& _field) = default; |
| 117 | + |
| 118 | + /// Assigns the underlying object. |
| 119 | + Deprecated& operator=(Deprecated&& _field) = default; |
| 120 | + |
| 121 | + /// Assigns the underlying object. |
| 122 | + template <class U> |
| 123 | + auto& operator=( |
| 124 | + const Deprecated<_deprecation_message, _description, U>& _field) { |
| 125 | + value_ = _field.get(); |
| 126 | + return *this; |
| 127 | + } |
| 128 | + |
| 129 | + /// Assigns the underlying object. |
| 130 | + template <class U> |
| 131 | + auto& operator=(Deprecated<_deprecation_message, _description, U>&& _field) { |
| 132 | + value_ = std::move(_field.value_); |
| 133 | + return *this; |
| 134 | + } |
| 135 | + |
| 136 | + /// Returns the underlying object - necessary for the reflection to work. |
| 137 | + const Type& reflection() const { return value_; } |
| 138 | + |
| 139 | + /// Assigns the underlying object. |
| 140 | + void set(const Type& _value) { value_ = _value; } |
| 141 | + |
| 142 | + /// Assigns the underlying object. |
| 143 | + void set(Type&& _value) { value_ = std::move(_value); } |
| 144 | + |
| 145 | + /// Returns the underlying object. |
| 146 | + Type& value() { return value_; } |
| 147 | + |
| 148 | + /// Returns the underlying object. |
| 149 | + const Type& value() const { return value_; } |
| 150 | + |
| 151 | + /// The underlying value. |
| 152 | + Type value_; |
| 153 | +}; |
| 154 | + |
| 155 | +} // namespace rfl |
| 156 | + |
| 157 | +#endif |
0 commit comments