Skip to content

Commit 5d84040

Browse files
Mizuchifacebook-github-bot
authored andcommitted
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
1 parent f38c234 commit 5d84040

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

thrift/compiler/generate/t_mstch_cpp2_generator.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,9 @@ void t_mstch_cpp2_generator::generate_visitation(const t_program* program) {
20382038
generators_->program_generator_->generate(program, generators_, cache_);
20392039
}
20402040

2041+
render_to_file(
2042+
cache_->programs_[id], "module_visitation.h", name + "_visitation.h");
2043+
20412044
render_to_file(
20422045
cache_->programs_[id],
20432046
"module_for_each_field.h",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<%!
2+
3+
Copyright (c) Facebook, Inc. and its affiliates.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
%><% > Autogen%>
18+
#pragma once
19+
<%#program:thrift_includes%>
20+
#include "<%program:include_prefix%><%program:name%>_visitation.h"
21+
<%/program:thrift_includes%>
22+
#include "<%program:include_prefix%><%program:name%>_for_each_field.h"
23+
#include "<%program:include_prefix%><%program:name%>_visit_union.h"
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Autogenerated by Thrift
3+
*
4+
* DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5+
* @generated
6+
*/
7+
#pragma once
8+
#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/reflection_dep_B_visitation.h"
9+
#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/reflection_dep_C_visitation.h"
10+
#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/module_for_each_field.h"
11+
#include "thrift/compiler/test/fixtures/visitation/gen-cpp2/module_visit_union.h"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <thrift/test/gen-cpp2/reflection_visitation.h> // @manual=:reflection_if-cpp2-visitation
18+
19+
#include <folly/Overload.h>
20+
#include <folly/portability/GTest.h>
21+
22+
using namespace apache::thrift;
23+
using namespace std;
24+
25+
namespace test_cpp2 {
26+
namespace cpp_reflection {
27+
TEST(dep_C_struct, test_transitivity) {
28+
dep_C_struct s;
29+
s.i_c_ref() = 10;
30+
s.d_ref()->i_d_ref() = 20;
31+
for_each_field(s, [](auto&&, auto&& ref) {
32+
folly::overload(
33+
[](int32_t i) { EXPECT_EQ(i, 10); },
34+
[](dep_D_struct d) {
35+
for_each_field(d, [](auto&&, auto&& ref) { EXPECT_EQ(*ref, 20); });
36+
})(*ref);
37+
});
38+
}
39+
TEST(union1, test_union) {
40+
union1 s;
41+
s.us_ref() = "foo";
42+
visit_union(s, [](auto&&, auto&& value) {
43+
folly::overload(
44+
[](string& s) { EXPECT_EQ(s, "foo"); },
45+
[](auto&&) { EXPECT_TRUE(false); })(value);
46+
});
47+
}
48+
} // namespace cpp_reflection
49+
} // namespace test_cpp2

0 commit comments

Comments
 (0)