Skip to content

Commit

Permalink
add module-visitation.h to include all visitation headers in a tran…
Browse files Browse the repository at this point in the history
…sitive 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
  • Loading branch information
Mizuchi authored and facebook-github-bot committed Aug 24, 2020
1 parent f38c234 commit 5d84040
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions thrift/compiler/generate/t_mstch_cpp2_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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"
49 changes: 49 additions & 0 deletions thrift/test/visitation_transitivity_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <thrift/test/gen-cpp2/reflection_visitation.h> // @manual=:reflection_if-cpp2-visitation

#include <folly/Overload.h>
#include <folly/portability/GTest.h>

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

0 comments on commit 5d84040

Please sign in to comment.