draconisplusplus/include/rfl/replace.hpp

45 lines
1.5 KiB
C++
Raw Normal View History

2024-05-31 22:59:00 -04:00
#ifndef RFL_REPLACE_HPP_
#define RFL_REPLACE_HPP_
#include <type_traits>
#include "from_named_tuple.hpp"
namespace rfl {
2024-06-08 14:10:59 -04:00
/// 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) {
2024-05-31 22:59:00 -04:00
if constexpr (internal::is_named_tuple_v<T>) {
2024-06-08 14:10:59 -04:00
return std::forward<T>(_t).replace(
to_named_tuple(std::forward<RField>(_field)),
to_named_tuple(std::forward<OtherRFields>(_other_fields))...);
2024-05-31 22:59:00 -04:00
} else {
2024-06-08 14:10:59 -04:00
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))...));
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
/// 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) {
2024-05-31 22:59:00 -04:00
if constexpr (internal::is_named_tuple_v<T>) {
2024-06-08 14:10:59 -04:00
return _t.replace(
to_named_tuple(std::forward<RField>(_field)),
to_named_tuple(std::forward<OtherRFields>(_other_fields))...);
2024-05-31 22:59:00 -04:00
} else {
2024-06-08 14:10:59 -04:00
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))...));
2024-05-31 22:59:00 -04:00
}
2024-06-08 14:10:59 -04:00
}
2024-05-31 22:59:00 -04:00
2024-06-08 14:10:59 -04:00
} // namespace rfl
2024-05-31 22:59:00 -04:00
#endif