mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Use perfect forwarding on rfl::visit for rfl::TaggedUnion (#252)
This commit is contained in:
@@ -86,26 +86,16 @@ struct TaggedUnion {
|
||||
|
||||
/// Applies function _f to all underlying alternatives.
|
||||
template <class F>
|
||||
auto visit(F& _f) {
|
||||
return variant_.visit(_f);
|
||||
auto visit(F&& _f)
|
||||
-> decltype(std::declval<VariantType>().visit(std::declval<F&&>())) {
|
||||
return variant_.visit(std::forward<F>(_f));
|
||||
}
|
||||
|
||||
/// Applies function _f to all underlying alternatives.
|
||||
template <class F>
|
||||
auto visit(F& _f) const {
|
||||
return variant_.visit(_f);
|
||||
}
|
||||
|
||||
/// Applies function _f to all underlying alternatives.
|
||||
template <class F>
|
||||
auto visit(const F& _f) {
|
||||
return variant_.visit(_f);
|
||||
}
|
||||
|
||||
/// Applies function _f to all underlying alternatives.
|
||||
template <class F>
|
||||
auto visit(const F& _f) const {
|
||||
return variant_.visit(_f);
|
||||
auto visit(F&& _f) const
|
||||
-> decltype(std::declval<VariantType>().visit(std::declval<F&&>())) {
|
||||
return variant_.visit(std::forward<F>(_f));
|
||||
}
|
||||
|
||||
/// The underlying variant - a TaggedUnion is a thin wrapper
|
||||
|
||||
@@ -27,10 +27,8 @@ inline auto visit(F&& _f, V&& _v)
|
||||
return std::forward<V>(_v).visit(std::forward<F>(_f));
|
||||
}
|
||||
|
||||
template <class F, TaggedUnionBased T, internal::StringLiteral _discriminator,
|
||||
class... AlternativeTypes>
|
||||
inline auto visit(
|
||||
F&& _f, TaggedUnion<_discriminator, AlternativeTypes...>&& _tagged_union)
|
||||
template <class F, TaggedUnionBased T>
|
||||
inline auto visit(F&& _f, T&& _tagged_union)
|
||||
-> decltype(std::declval<T&&>().variant().visit(std::declval<F&&>())) {
|
||||
return std::forward<T>(_tagged_union).variant().visit(std::forward<F>(_f));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
|
||||
#include "rfl/TaggedUnion.hpp"
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
namespace test_rfl_tagged_union_visit_move_only {
|
||||
|
||||
struct Rectangle {
|
||||
double height;
|
||||
std::unique_ptr<double> width;
|
||||
};
|
||||
|
||||
struct Square {
|
||||
std::unique_ptr<double> width;
|
||||
};
|
||||
|
||||
struct Circle {
|
||||
double const radius;
|
||||
};
|
||||
|
||||
using Shapes = rfl::TaggedUnion<"Shapes", Rectangle, Square>;
|
||||
|
||||
TEST(json, test_rfl_tagged_union_visit_move_only) {
|
||||
const Shapes r =
|
||||
Rectangle{.height = 10, .width = std::make_unique<double>(5.0)};
|
||||
const auto get_width = [](const auto& _s) -> const std::unique_ptr<double>& {
|
||||
return _s.width;
|
||||
};
|
||||
EXPECT_EQ(*rfl::visit(get_width, r), 5.0);
|
||||
}
|
||||
|
||||
TEST(json, test_rfl_tagged_union_visit_return_move_only_const) {
|
||||
auto const circle = Circle{.radius = 10};
|
||||
auto const tagged_union = rfl::TaggedUnion<"Circle", Circle>{circle};
|
||||
const auto get_radius = [](const auto& object) -> const double& {
|
||||
return object.radius;
|
||||
};
|
||||
auto const result = rfl::visit(get_radius, tagged_union);
|
||||
EXPECT_EQ(result, circle.radius);
|
||||
}
|
||||
|
||||
TEST(json, test_rfl_tagged_union_visit_return_move_only) {
|
||||
auto circle = Circle{.radius = 10};
|
||||
auto tagged_union = rfl::TaggedUnion<"Circle", Circle>{circle};
|
||||
auto const get_radius = [](auto& object) -> double const& {
|
||||
return object.radius;
|
||||
};
|
||||
auto result = rfl::visit(get_radius, tagged_union);
|
||||
EXPECT_EQ(result, circle.radius);
|
||||
}
|
||||
} // namespace test_rfl_tagged_union_visit_move_only
|
||||
@@ -2,8 +2,6 @@
|
||||
#include <memory>
|
||||
#include <rfl.hpp>
|
||||
#include <rfl/json.hpp>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "write_and_read.hpp"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user