-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathDescription.hpp
More file actions
146 lines (108 loc) · 3.81 KB
/
Copy pathDescription.hpp
File metadata and controls
146 lines (108 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#ifndef RFL_DESCRIPTION_HPP_
#define RFL_DESCRIPTION_HPP_
#include <algorithm>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <utility>
#include "Literal.hpp"
#include "default.hpp"
#include "internal/StringLiteral.hpp"
namespace rfl {
/// Used to add a description to the field - this is only relevant for the JSON
/// schema and will be ignored by the normal serialization routines.
template <internal::StringLiteral _description, class T>
struct Description {
/// The underlying type.
using Type = T;
/// The description of the field.
using Content = rfl::Literal<_description>;
using ReflectionType = Type;
Description() : value_(Type()) {}
Description(const Type& _value) : value_(_value) {}
Description(Type&& _value) noexcept : value_(std::move(_value)) {}
Description(Description&& _field) noexcept = default;
Description(const Description& _field) = default;
template <class U>
Description(const Description<_description, U>& _field)
: value_(_field.get()) {}
template <class U>
Description(Description<_description, U>&& _field) : value_(_field.get()) {}
template <class U>
requires std::is_convertible_v<U, Type>
Description(const U& _value) : value_(_value) {}
template <class U>
requires std::is_convertible_v<U, Type>
Description(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
template <class U>
requires std::is_convertible_v<U, Type>
Description(const Description<_description, U>& _field)
: value_(_field.value()) {}
/// Assigns the underlying object to its default value.
template <class U = Type>
requires std::is_default_constructible_v<U>
Description(const Default&) : value_(Type()) {}
~Description() = default;
/// The description of the field, for internal use.
constexpr static const internal::StringLiteral description_ = _description;
/// Returns the underlying object.
const Type& get() const { return value_; }
/// Returns the underlying object.
Type& operator()() { 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=(Type&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
/// Assigns the underlying object.
template <class U>
requires std::is_convertible_v<U, Type>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}
/// Assigns the underlying object to its default value.
template <class U = Type>
requires std::is_default_constructible_v<U>
auto& operator=(const Default&) {
value_ = Type();
return *this;
}
/// Assigns the underlying object.
Description& operator=(const Description& _field) = default;
/// Assigns the underlying object.
Description& operator=(Description&& _field) = default;
/// Assigns the underlying object.
template <class U>
auto& operator=(const Description<_description, U>& _field) {
value_ = _field.get();
return *this;
}
/// Assigns the underlying object.
template <class U>
auto& operator=(Description<_description, U>&& _field) {
value_ = std::forward<T>(_field.value_);
return *this;
}
/// Returns the underlying object - necessary for the reflection to work.
const Type& 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_; }
/// The underlying value.
Type value_;
};
} // namespace rfl
#endif