Repository navigation
Expand file tree
/
Copy pathPositional.hpp
More file actions
198 lines (160 loc) · 6.55 KB
/
Copy pathPositional.hpp
File metadata and controls
198 lines (160 loc) · 6.55 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#ifndef RFL_POSITIONAL_HPP_
#define RFL_POSITIONAL_HPP_
#include <type_traits>
#include <utility>
#include "default.hpp"
namespace rfl {
/// Marks a field as positional for CLI argument parsing.
/// In CLI contexts, positional fields are passed without names (e.g., "program arg1 arg2").
/// For non-CLI formats (JSON, YAML, etc.), this wrapper is transparent and has no effect.
/// @tparam T The type of the positional field
template <class T>
struct Positional {
/// The underlying type.
using Type = T;
/// Default constructor - requires Type to be default constructible.
Positional() requires std::is_default_constructible_v<Type>
: value_(Type()) {}
/// Constructs from a const reference to the value.
/// @param _value The value to store
Positional(const Type& _value) : value_(_value) {}
/// Constructs from an rvalue reference to the value.
/// @param _value The value to store (will be moved)
Positional(Type&& _value) noexcept : value_(std::move(_value)) {}
/// Move constructor.
/// @param _field The Positional to move from
Positional(Positional<T>&& _field) noexcept = default;
/// Copy constructor.
/// @param _field The Positional to copy from
Positional(const Positional<T>& _field) = default;
/// Copy constructor from Positional with compatible type.
/// @tparam U Type compatible with T
/// @param _field The Positional to copy the value from
template <class U>
Positional(const Positional<U>& _field) : value_(_field.get()) {}
/// Move constructor from Positional with compatible type.
/// @tparam U Type compatible with T
/// @param _field The Positional to move the value from
template <class U>
Positional(Positional<U>&& _field) : value_(std::move(_field.value_)) {}
/// Constructs from any type convertible to Type (copy).
/// @tparam U Type convertible to T
/// @param _value The value to convert and store
template <class U>
requires std::is_convertible_v<U, Type>
Positional(const U& _value) : value_(_value) {}
/// Constructs from any type convertible to Type (move).
/// @tparam U Type convertible to T
/// @param _value The value to convert and store (will be moved)
template <class U>
requires std::is_convertible_v<U, Type>
Positional(U&& _value) noexcept : value_(std::forward<U>(_value)) {}
/// Assigns the underlying object to its default value using the Default sentinel.
/// @tparam U The type (must be default constructible)
/// @param _ The default sentinel value
template <class U = Type>
requires std::is_default_constructible_v<U>
Positional(const Default&) : value_(Type()) {}
/// Destructor.
~Positional() = default;
/// Returns the underlying value.
/// @return Const reference to the stored value
const Type& get() const noexcept { return value_; }
/// Returns the underlying value.
/// @return Reference to the stored value
Type& get() noexcept { return value_; }
/// Dereference operator - returns the underlying value.
/// @return Reference to the stored value
Type& operator*() noexcept { return value_; }
/// Dereference operator (const) - returns the underlying value.
/// @return Const reference to the stored value
const Type& operator*() const noexcept { return value_; }
/// Function call operator - returns the underlying value.
/// @return Reference to the stored value
Type& operator()() noexcept { return value_; }
/// Function call operator (const) - returns the underlying value.
/// @return Const reference to the stored value
const Type& operator()() const noexcept { return value_; }
/// Pointer to the underlying value.
/// @return Pointer to the stored value
Type* operator->() noexcept { return &value_; }
/// Pointer to the underlying value (const).
/// @return Const pointer to the stored value
const Type* operator->() const noexcept { return &value_; }
/// Assigns a new value.
/// @param _value The value to assign
/// @return Reference to this Positional
auto& operator=(const Type& _value) {
value_ = _value;
return *this;
}
/// Assigns a new value (move version).
/// @param _value The value to assign (will be moved)
/// @return Reference to this Positional
auto& operator=(Type&& _value) noexcept {
value_ = std::move(_value);
return *this;
}
/// Assigns from any type convertible to the underlying type.
/// @tparam U Type convertible to T
/// @param _value The value to convert and assign
/// @return Reference to this Positional
template <class U>
requires std::is_convertible_v<U, Type>
auto& operator=(const U& _value) {
value_ = _value;
return *this;
}
/// Assigns the value to its default using the Default sentinel.
/// @tparam U The type (must be default constructible)
/// @param The default sentinel value
/// @return Reference to this Positional
template <class U = Type>
requires std::is_default_constructible_v<U>
auto& operator=(const Default&) {
value_ = Type();
return *this;
}
/// Copy assignment operator.
/// @param _field The Positional to copy from
/// @return Reference to this Positional
Positional<T>& operator=(const Positional<T>& _field) = default;
/// Move assignment operator.
/// @param _field The Positional to move from
/// @return Reference to this Positional
Positional<T>& operator=(Positional<T>&& _field) = default;
/// Assigns from another Positional with compatible type (copy).
/// @tparam U Type compatible with T
/// @param _field The Positional to copy the value from
/// @return Reference to this Positional
template <class U>
auto& operator=(const Positional<U>& _field) {
value_ = _field.get();
return *this;
}
/// Assigns from another Positional with compatible type (move).
/// @tparam U Type compatible with T
/// @param _field The Positional to move the value from
/// @return Reference to this Positional
template <class U>
auto& operator=(Positional<U>&& _field) {
value_ = std::move(_field.value_);
return *this;
}
/// Sets the value (copy version).
/// @param _value The value to set
void set(const Type& _value) { value_ = _value; }
/// Sets the value (move version).
/// @param _value The value to set (will be moved)
void set(Type&& _value) { value_ = std::move(_value); }
/// Returns the underlying value.
/// @return Reference to the stored value
Type& value() noexcept { return value_; }
/// Returns the underlying value (const).
/// @return Const reference to the stored value
const Type& value() const noexcept { return value_; }
/// The underlying value.
Type value_;
};
} // namespace rfl
#endif