diff --git a/thrift/compiler/generate/t_mstch_cpp2_generator.cc b/thrift/compiler/generate/t_mstch_cpp2_generator.cc index 60257edf7fc..f9745cf3308 100644 --- a/thrift/compiler/generate/t_mstch_cpp2_generator.cc +++ b/thrift/compiler/generate/t_mstch_cpp2_generator.cc @@ -2038,6 +2038,9 @@ void t_mstch_cpp2_generator::generate_visitation(const t_program* program) { generators_->program_generator_->generate(program, generators_, cache_); } + render_to_file( + cache_->programs_[id], "module_visitation.h", name + "_visitation.h"); + render_to_file( cache_->programs_[id], "module_for_each_field.h", diff --git a/thrift/compiler/generate/templates/cpp2/module_visitation.h.mustache b/thrift/compiler/generate/templates/cpp2/module_visitation.h.mustache new file mode 100644 index 00000000000..05373a3d89a --- /dev/null +++ b/thrift/compiler/generate/templates/cpp2/module_visitation.h.mustache @@ -0,0 +1,23 @@ +<%! + + Copyright (c) Facebook, Inc. and its affiliates. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +%><% > Autogen%> +#pragma once +<%#program:thrift_includes%> +#include "<%program:include_prefix%><%program:name%>_visitation.h" +<%/program:thrift_includes%> +#include "<%program:include_prefix%><%program:name%>_for_each_field.h" +#include "<%program:include_prefix%><%program:name%>_visit_union.h" diff --git a/thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visitation.h b/thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visitation.h new file mode 100644 index 00000000000..f2d55366c2f --- /dev/null +++ b/thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visitation.h @@ -0,0 +1,11 @@ +/** + * Autogenerated by Thrift + * + * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING + * @generated + */ +#pragma once +#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/reflection_dep_B_visitation.h" +#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/reflection_dep_C_visitation.h" +#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/module_for_each_field.h" +#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visit_union.h" diff --git a/thrift/test/visitation_transitivity_test.cpp b/thrift/test/visitation_transitivity_test.cpp new file mode 100644 index 00000000000..348c1c0582b --- /dev/null +++ b/thrift/test/visitation_transitivity_test.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include // @manual=:reflection_if-cpp2-visitation + +#include +#include + +using namespace apache::thrift; +using namespace std; + +namespace test_cpp2 { +namespace cpp_reflection { +TEST(dep_C_struct, test_transitivity) { + dep_C_struct s; + s.i_c_ref() = 10; + s.d_ref()->i_d_ref() = 20; + for_each_field(s, [](auto&&, auto&& ref) { + folly::overload( + [](int32_t i) { EXPECT_EQ(i, 10); }, + [](dep_D_struct d) { + for_each_field(d, [](auto&&, auto&& ref) { EXPECT_EQ(*ref, 20); }); + })(*ref); + }); +} +TEST(union1, test_union) { + union1 s; + s.us_ref() = "foo"; + visit_union(s, [](auto&&, auto&& value) { + folly::overload( + [](string& s) { EXPECT_EQ(s, "foo"); }, + [](auto&&) { EXPECT_TRUE(false); })(value); + }); +} +} // namespace cpp_reflection +} // namespace test_cpp2