Skip to content

Commit fc8f28a

Browse files
iahsfacebook-github-bot
authored andcommitted
Simplify object::visit
Summary: Ensures that if future cases are added and not handled here we will fail at compile time rather than runtime. Reviewed By: vitaut Differential Revision: D77317771 fbshipit-source-id: b8178592ce28416929f3e71b858219c05de29f66
1 parent a6441e3 commit fc8f28a

File tree

1 file changed

+9
-23
lines changed
  • third-party/thrift/src/thrift/compiler/whisker

1 file changed

+9
-23
lines changed

third-party/thrift/src/thrift/compiler/whisker/object.h

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -699,29 +699,15 @@ class object final : private detail::object_base {
699699
resolved = resolved->as_proxy().get();
700700
}
701701

702-
auto overloaded = detail::overload(std::forward<Visitors>(visitors)...);
703-
// This macro could be implemented using variant_match + overload, but that
704-
// would require two levels of indirection. That leads to some really ugly
705-
// template-related compiler errors.
706-
#define WHISKER_OBJECT_TRY_VISIT(type) \
707-
do { \
708-
if (const auto* v = std::get_if<type>(resolved)) { \
709-
return overloaded(*v); \
710-
} \
711-
} while (false)
712-
WHISKER_OBJECT_TRY_VISIT(null);
713-
WHISKER_OBJECT_TRY_VISIT(i64);
714-
WHISKER_OBJECT_TRY_VISIT(f64);
715-
WHISKER_OBJECT_TRY_VISIT(string);
716-
WHISKER_OBJECT_TRY_VISIT(boolean);
717-
WHISKER_OBJECT_TRY_VISIT(native_function::ptr);
718-
WHISKER_OBJECT_TRY_VISIT(native_handle<>);
719-
WHISKER_OBJECT_TRY_VISIT(map::ptr);
720-
WHISKER_OBJECT_TRY_VISIT(array::ptr);
721-
#undef WHISKER_OBJECT_TRY_VISIT
722-
assert(!static_cast<bool>(
723-
"There is a missed variant alternative in object::visit() implementation"));
724-
throw std::bad_variant_access();
702+
return detail::variant_match(
703+
resolved->as_variant(),
704+
[](managed_ptr<object>) -> decltype(detail::overload(
705+
std::forward<Visitors>(visitors)...)(
706+
i64{})) {
707+
// Unreachable: unwrapped above.
708+
std::terminate();
709+
},
710+
std::forward<Visitors>(visitors)...);
725711
}
726712

727713
const i64& as_i64() const { return as<i64>(); }

0 commit comments

Comments
 (0)