@@ -85,27 +85,29 @@ class NamedTuple {
85
85
// / Move constructor.
86
86
template <class ... OtherFieldTypes>
87
87
NamedTuple (NamedTuple<OtherFieldTypes...>&& _other)
88
- : NamedTuple(retrieve_fields(_other.fields(), seq_)) {}
88
+ : NamedTuple(retrieve_fields(
89
+ std::forward<NamedTuple<OtherFieldTypes...>>(_other).fields(),
90
+ seq_)) {}
89
91
90
92
~NamedTuple () = default ;
91
93
92
94
// / Returns a new named tuple with additional fields.
93
95
template <internal::StringLiteral _name, class FType , class ... Tail>
94
- auto add (Field<_name, FType>&& _head, Tail&&... _tail) {
96
+ auto add (Field<_name, FType>&& _head, Tail&&... _tail) && {
95
97
using Head = Field<_name, FType>;
96
98
if constexpr (sizeof ...(Tail) > 0 ) {
97
99
return NamedTuple<FieldTypes..., std::remove_cvref_t <Head>>(
98
- make_fields (seq_, std::forward<Head>(_head)))
100
+ std::move (* this ). make_fields (seq_, std::forward<Head>(_head)))
99
101
.add (std::forward<Tail>(_tail)...);
100
102
} else {
101
103
return NamedTuple<FieldTypes..., std::remove_cvref_t <Head>>(
102
- make_fields (seq_, std::forward<Head>(_head)));
104
+ std::move (* this ). make_fields (seq_, std::forward<Head>(_head)));
103
105
}
104
106
}
105
107
106
108
// / Returns a new named tuple with additional fields.
107
109
template <internal::StringLiteral _name, class FType , class ... Tail>
108
- auto add (Field<_name, FType> _head, const Tail&... _tail) const {
110
+ auto add (Field<_name, FType> _head, const Tail&... _tail) const & {
109
111
using Head = Field<_name, FType>;
110
112
if constexpr (sizeof ...(Tail) > 0 ) {
111
113
return NamedTuple<FieldTypes..., std::remove_cvref_t <Head>>(
@@ -120,19 +122,21 @@ class NamedTuple {
120
122
// / Template specialization for rfl::Tuple, so we can pass fields from other
121
123
// / named tuples.
122
124
template <class ... TupContent, class ... Tail>
123
- auto add (rfl::Tuple<TupContent...>&& _tuple, Tail&&... _tail) {
125
+ auto add (rfl::Tuple<TupContent...>&& _tuple, Tail&&... _tail) && {
124
126
if constexpr (sizeof ...(Tail) > 0 ) {
125
- return add_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple))
127
+ return std::move (*this )
128
+ .add_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple))
126
129
.add (std::forward<Tail>(_tail)...);
127
130
} else {
128
- return add_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple));
131
+ return std::move (*this ).add_tuple (
132
+ std::forward<rfl::Tuple<TupContent...>>(_tuple));
129
133
}
130
134
}
131
135
132
136
// / Template specialization for rfl::Tuple, so we can pass fields from other
133
137
// / named tuples.
134
138
template <class ... TupContent, class ... Tail>
135
- auto add (rfl::Tuple<TupContent...> _tuple, const Tail&... _tail) const {
139
+ auto add (rfl::Tuple<TupContent...> _tuple, const Tail&... _tail) const & {
136
140
if constexpr (sizeof ...(Tail) > 0 ) {
137
141
return add_tuple (std::move (_tuple)).add (_tail...);
138
142
} else {
@@ -143,36 +147,39 @@ class NamedTuple {
143
147
// / Template specialization for NamedTuple, so we can pass fields from other
144
148
// / named tuples.
145
149
template <class ... TupContent, class ... Tail>
146
- auto add (NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
147
- return add (std::forward<rfl::Tuple<TupContent...>>(_named_tuple.fields ()),
148
- std::forward<Tail>(_tail)...);
150
+ auto add (NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) && {
151
+ return std::move (*this ).add (
152
+ std::forward<rfl::Tuple<TupContent...>>(
153
+ std::forward<NamedTuple<TupContent...>>(_named_tuple).fields ()),
154
+ std::forward<Tail>(_tail)...);
149
155
}
150
156
151
157
// / Template specialization for NamedTuple, so we can pass fields from other
152
158
// / named tuples.
153
159
template <class ... TupContent, class ... Tail>
154
- auto add (NamedTuple<TupContent...> _named_tuple, const Tail&... _tail) const {
160
+ auto add (NamedTuple<TupContent...> _named_tuple,
161
+ const Tail&... _tail) const & {
155
162
return add (_named_tuple.fields (), _tail...);
156
163
}
157
164
158
165
// / Creates a new named tuple by applying the supplied function to
159
166
// / field. The function is expected to return a named tuple itself.
160
167
template <typename F>
161
- auto and_then (const F& _f) {
168
+ auto and_then (const F& _f) && {
162
169
const auto transform_field = [&_f](auto ... _fields) {
163
170
return rfl::tuple_cat (_f (std::move (_fields)).fields ()...);
164
171
};
165
172
const auto to_nt = []<class ... NewFields>(rfl::Tuple<NewFields...>&& _tup) {
166
173
return NamedTuple<NewFields...>(_tup);
167
174
};
168
- auto new_fields = rfl::apply (transform_field, std::move (fields () ));
175
+ auto new_fields = rfl::apply (transform_field, std::move (* this ). fields ());
169
176
return to_nt (std::move (new_fields));
170
177
}
171
178
172
179
// / Creates a new named tuple by applying the supplied function to
173
180
// / field. The function is expected to return a named tuple itself.
174
181
template <typename F>
175
- auto and_then (const F& _f) const {
182
+ auto and_then (const F& _f) const & {
176
183
const auto transform_field = [&_f](auto ... _fields) {
177
184
return rfl::tuple_cat (_f (std::move (_fields)).fields ()...);
178
185
};
@@ -193,10 +200,10 @@ class NamedTuple {
193
200
}
194
201
195
202
// / Returns a tuple containing the fields.
196
- Fields fields () { return make_fields (seq_); }
203
+ Fields fields () && { return std::move (* this ). make_fields (seq_); }
197
204
198
205
// / Returns a tuple containing the fields.
199
- Fields fields () const { return make_fields (seq_); }
206
+ Fields fields () const & { return make_fields (seq_); }
200
207
201
208
// / Gets a field by index.
202
209
template <int _index>
@@ -276,13 +283,15 @@ class NamedTuple {
276
283
// / Replaces one or several fields, returning a new version
277
284
// / with the non-replaced fields left unchanged.
278
285
template <internal::StringLiteral _name, class FType , class ... OtherRFields>
279
- auto replace (Field<_name, FType>&& _field, OtherRFields&&... _other_fields) {
286
+ auto replace (Field<_name, FType>&& _field,
287
+ OtherRFields&&... _other_fields) && {
280
288
using RField = Field<_name, FType>;
281
289
constexpr auto num_other_fields = sizeof ...(OtherRFields);
282
290
if constexpr (num_other_fields == 0 ) {
283
- return replace_value<RField>(_field.value_ );
291
+ return std::move (* this ). template replace_value <RField>(_field.value_ );
284
292
} else {
285
- return replace_value<RField>(_field.value_ )
293
+ return std::move (*this )
294
+ .template replace_value <RField>(_field.value_ )
286
295
.replace (std::forward<OtherRFields>(_other_fields)...);
287
296
}
288
297
}
@@ -291,7 +300,7 @@ class NamedTuple {
291
300
// / with the non-replaced fields left unchanged.
292
301
template <internal::StringLiteral _name, class FType , class ... OtherRFields>
293
302
auto replace (Field<_name, FType> _field,
294
- const OtherRFields&... _other_fields) const {
303
+ const OtherRFields&... _other_fields) const & {
295
304
using RField = Field<_name, FType>;
296
305
constexpr auto num_other_fields = sizeof ...(OtherRFields);
297
306
if constexpr (num_other_fields == 0 ) {
@@ -305,19 +314,21 @@ class NamedTuple {
305
314
// / Template specialization for rfl::Tuple, so we can pass fields from other
306
315
// / named tuples.
307
316
template <class ... TupContent, class ... Tail>
308
- auto replace (rfl::Tuple<TupContent...>&& _tuple, Tail&&... _tail) {
317
+ auto replace (rfl::Tuple<TupContent...>&& _tuple, Tail&&... _tail) && {
309
318
if constexpr (sizeof ...(Tail) > 0 ) {
310
- return replace_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple))
319
+ return std::move (*this )
320
+ .replace_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple))
311
321
.replace (std::forward<Tail>(_tail)...);
312
322
} else {
313
- return replace_tuple (std::forward<rfl::Tuple<TupContent...>>(_tuple));
323
+ return std::move (*this ).replace_tuple (
324
+ std::forward<rfl::Tuple<TupContent...>>(_tuple));
314
325
}
315
326
}
316
327
317
328
// / Template specialization for rfl::Tuple, so we can pass fields from other
318
329
// / named tuples.
319
330
template <class ... TupContent, class ... Tail>
320
- auto replace (rfl::Tuple<TupContent...> _tuple, const Tail&... _tail) const {
331
+ auto replace (rfl::Tuple<TupContent...> _tuple, const Tail&... _tail) const & {
321
332
if constexpr (sizeof ...(Tail) > 0 ) {
322
333
return replace_tuple (std::move (_tuple)).replace (_tail...);
323
334
} else {
@@ -328,8 +339,8 @@ class NamedTuple {
328
339
// / Template specialization for NamedTuple, so we can pass fields from other
329
340
// / named tuples.
330
341
template <class ... TupContent, class ... Tail>
331
- auto replace (NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) {
332
- return replace (
342
+ auto replace (NamedTuple<TupContent...>&& _named_tuple, Tail&&... _tail) && {
343
+ return std::move (* this ). replace (
333
344
std::forward<NamedTuple<TupContent...>>(_named_tuple).fields (),
334
345
std::forward<Tail>(_tail)...);
335
346
}
@@ -338,7 +349,7 @@ class NamedTuple {
338
349
// / named tuples.
339
350
template <class ... TupContent, class ... Tail>
340
351
auto replace (NamedTuple<TupContent...> _named_tuple,
341
- const Tail&... _tail) const {
352
+ const Tail&... _tail) const & {
342
353
return replace (_named_tuple.fields (), _tail...);
343
354
}
344
355
@@ -348,21 +359,21 @@ class NamedTuple {
348
359
// / Creates a new named tuple by applying the supplied function to every
349
360
// / field.
350
361
template <typename F>
351
- auto transform (const F& _f) {
362
+ auto transform (const F& _f) && {
352
363
const auto transform_field = [&_f](auto ... fields) {
353
364
return rfl::make_tuple (_f (std::move (fields))...);
354
365
};
355
366
const auto to_nt = []<class ... NewFields>(rfl::Tuple<NewFields...>&& _tup) {
356
367
return NamedTuple<NewFields...>(_tup);
357
368
};
358
- auto new_fields = rfl::apply (transform_field, std::move (fields () ));
369
+ auto new_fields = rfl::apply (transform_field, std::move (* this ). fields ());
359
370
return to_nt (std::move (new_fields));
360
371
}
361
372
362
373
// / Creates a new named tuple by applying the supplied function to every
363
374
// / field.
364
375
template <typename F>
365
- auto transform (const F& _f) const {
376
+ auto transform (const F& _f) const & {
366
377
const auto transform_field = [&_f](auto ... fields) {
367
378
return rfl::make_tuple (_f (std::move (fields))...);
368
379
};
@@ -383,17 +394,17 @@ class NamedTuple {
383
394
// / Adds the elements of a tuple to a newly created named tuple,
384
395
// / and other elements to a newly created named tuple.
385
396
template <class ... TupContent>
386
- constexpr auto add_tuple (rfl::Tuple<TupContent...>&& _tuple) {
397
+ constexpr auto add_tuple (rfl::Tuple<TupContent...>&& _tuple) && {
387
398
const auto a = [this ](auto &&... _fields) {
388
- return this -> add (std::forward<TupContent>(_fields)...);
399
+ return std::move (* this ). add (std::forward<TupContent>(_fields)...);
389
400
};
390
401
return rfl::apply (a, std::forward<rfl::Tuple<TupContent...>>(_tuple));
391
402
}
392
403
393
404
// / Adds the elements of a tuple to a newly created named tuple,
394
405
// / and other elements to a newly created named tuple.
395
406
template <class ... TupContent>
396
- constexpr auto add_tuple (rfl::Tuple<TupContent...>&& _tuple) const {
407
+ constexpr auto add_tuple (rfl::Tuple<TupContent...>&& _tuple) const & {
397
408
const auto a = [this ](auto &&... _fields) {
398
409
return this ->add (std::forward<TupContent>(_fields)...);
399
410
};
@@ -435,11 +446,10 @@ class NamedTuple {
435
446
// / Generates the fields.
436
447
template <int ... _is, class ... AdditionalArgs>
437
448
auto make_fields (std::integer_sequence<int , _is...>,
438
- AdditionalArgs&&... _args) {
449
+ AdditionalArgs&&... _args) && {
439
450
const auto wrap = [this ]<int _i>(Index<_i>) {
440
451
using FieldType = internal::nth_element_t <_i, FieldTypes...>;
441
- using T = std::remove_cvref_t <typename FieldType::Type>;
442
- return FieldType (std::forward<T>(rfl::get<_i>(values_)));
452
+ return FieldType (std::move (rfl::get<_i>(values_)));
443
453
};
444
454
return rfl::make_tuple (wrap (Index<_is>{})...,
445
455
std::forward<AdditionalArgs>(_args)...);
@@ -448,7 +458,7 @@ class NamedTuple {
448
458
// / Generates the fields.
449
459
template <int ... _is, class ... AdditionalArgs>
450
460
auto make_fields (std::integer_sequence<int , _is...>,
451
- AdditionalArgs... _args) const {
461
+ AdditionalArgs... _args) const & {
452
462
const auto wrap = [this ]<int _i>(Index<_i>) {
453
463
using FieldType = internal::nth_element_t <_i, FieldTypes...>;
454
464
return FieldType (rfl::get<_i>(values_));
@@ -474,16 +484,16 @@ class NamedTuple {
474
484
475
485
// / Replaced the field signified by the field type.
476
486
template <class Field , class T >
477
- NamedTuple<FieldTypes...> replace_value (T&& _val) {
487
+ NamedTuple<FieldTypes...> replace_value (T&& _val) && {
478
488
using FieldType = std::remove_cvref_t <Field>;
479
489
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
480
- return make_replaced<index>(std::forward<Values>(values_ ),
481
- std::forward<T>(_val), seq_);
490
+ return make_replaced<index>(std::move (values_), std:: forward<T>(_val ),
491
+ seq_);
482
492
}
483
493
484
494
// / Replaced the field signified by the field type.
485
495
template <class Field , class T >
486
- NamedTuple<FieldTypes...> replace_value (T&& _val) const {
496
+ NamedTuple<FieldTypes...> replace_value (T&& _val) const & {
487
497
using FieldType = std::remove_cvref_t <Field>;
488
498
constexpr auto index = internal::find_index<FieldType::name_, Fields>();
489
499
auto values = values_;
@@ -493,17 +503,17 @@ class NamedTuple {
493
503
// / Adds the elements of a tuple to a newly created named tuple,
494
504
// / and other elements to a newly created named tuple.
495
505
template <class ... TupContent>
496
- auto replace_tuple (rfl::Tuple<TupContent...>&& _tuple) {
506
+ auto replace_tuple (rfl::Tuple<TupContent...>&& _tuple) && {
497
507
const auto r = [this ](auto &&... _fields) {
498
- return this -> replace (std::forward<TupContent>(_fields)...);
508
+ return std::move (* this ). replace (std::forward<TupContent>(_fields)...);
499
509
};
500
510
return rfl::apply (r, std::forward<rfl::Tuple<TupContent...>>(_tuple));
501
511
}
502
512
503
513
// / Adds the elements of a tuple to a newly created named tuple,
504
514
// / and other elements to a newly created named tuple.
505
515
template <class ... TupContent>
506
- auto replace_tuple (rfl::Tuple<TupContent...>&& _tuple) const {
516
+ auto replace_tuple (rfl::Tuple<TupContent...>&& _tuple) const & {
507
517
const auto r = [this ](auto &&... _fields) {
508
518
return this ->replace (std::forward<TupContent>(_fields)...);
509
519
};
0 commit comments