From 5d84040f3fe307e97f936d94627c93d86d84d747 Mon Sep 17 00:00:00 2001 From: TJ Yin Date: Sun, 23 Aug 2020 18:34:48 -0700 Subject: [PATCH] add `module-visitation.h` to include all visitation headers in a transitive manner Summary: `module-visitation.h` is a convenient header that includes all visitation APIs, along with dependent thrift visitation headers transitively. This is useful for implementing general code since existing reflection is transitive. If user doesn't need transitivity, they can still include each individual header to reduce build-time. Reviewed By: yfeldblum Differential Revision: D23253246 fbshipit-source-id: ac4c07ffa71ccd849756dccaaf44b70d5d391b76 --- .../generate/t_mstch_cpp2_generator.cc | 3 ++ .../cpp2/module_visitation.h.mustache | 23 +++++++++ .../visitation/gen-cpp2/module_visitation.h | 11 +++++ thrift/test/visitation_transitivity_test.cpp | 49 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 thrift/compiler/generate/templates/cpp2/module_visitation.h.mustache create mode 100644 thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visitation.h create mode 100644 thrift/test/visitation_transitivity_test.cpp 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