File: replace.hpp

package info (click to toggle)
reflect-cpp 0.18.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 12,524 kB
  • sloc: cpp: 44,484; python: 131; makefile: 30; sh: 3
file content (44 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download | duplicates (2)
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
#ifndef RFL_REPLACE_HPP_
#define RFL_REPLACE_HPP_

#include <type_traits>

#include "from_named_tuple.hpp"

namespace rfl {

/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
template <class T, class RField, class... OtherRFields>
auto replace(T&& _t, RField&& _field, OtherRFields&&... _other_fields) {
    if constexpr (internal::is_named_tuple_v<T>) {
        return std::forward<T>(_t).replace(
            to_named_tuple(std::forward<RField>(_field)),
            to_named_tuple(std::forward<OtherRFields>(_other_fields))...);
    } else {
        return from_named_tuple<T>(
            to_named_tuple(std::forward<T>(_t))
                .replace(to_named_tuple(std::forward<RField>(_field)),
                         to_named_tuple(
                             std::forward<OtherRFields>(_other_fields))...));
    }
}

/// Replaces one or several fields, returning a new version
/// with the non-replaced fields left unchanged.
template <class T, class RField, class... OtherRFields>
auto replace(const T& _t, RField&& _field, OtherRFields&&... _other_fields) {
    if constexpr (internal::is_named_tuple_v<T>) {
        return _t.replace(
            to_named_tuple(std::forward<RField>(_field)),
            to_named_tuple(std::forward<OtherRFields>(_other_fields))...);
    } else {
        return from_named_tuple<T>(to_named_tuple(_t).replace(
            to_named_tuple(std::forward<RField>(_field)),
            to_named_tuple(std::forward<OtherRFields>(_other_fields))...));
    }
}

}  // namespace rfl

#endif