/* * Copyright (c) Meta Platforms, Inc. and 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. */ #pragma once #include #include #include #include #include #include template struct printer { static_assert( !std::is_same::value, "no static reflection support for the given type" " - did you forget to include the reflection metadata?" " see thrift/lib/cpp2/reflection/reflection.h"); template static void print(T const& what) { std::cout << what; } static void print(const bool what) { std::cout << (what ? "true" : "false"); } }; template <> struct printer { template static void print(T const& what) { std::cout << '"' << what << '"'; } }; template <> struct printer { template static void print(T const& what) { std::cout << '"' << fatal::enum_to_string(what, nullptr) << '"'; } }; template struct printer> { template static void print(T const& what) { std::cout << '['; bool first = true; for (const auto& i : what) { if (first) { first = false; } else { std::cout << ','; } printer::print(i); } std::cout << ']'; } }; template struct printer> : public printer> {}; template struct printer> { template static void print(T const& what) { std::cout << '{'; bool first = true; for (const auto& i : what) { if (first) { first = false; } else { std::cout << ','; } printer::print(i.first); std::cout << ':'; printer::print(i.second); } std::cout << '}'; } }; struct struct_member_printer { template void operator()(fatal::indexed, T const& what) const { if (Index) { std::cout << ','; } const auto name = fatal::z_data(); std::cout << '"' << name << "\":"; const auto& value = typename Member::getter{}(what); printer::print(value); } }; template <> struct printer { template static void print(T const& what) { std::cout << '{'; fatal::foreach::members>( struct_member_printer(), what); std::cout << '}'; } }; struct variant_member_printer { template void operator()(fatal::indexed, T const& what) const { const auto name = fatal::enum_to_string(what.getType(), nullptr); std::cout << '"' << name << "\":"; const auto& value = Member::get(what); printer::print(value); } }; template <> struct printer { template static void print(T const& what) { std::cout << '{'; fatal::scalar_search< typename fatal::variant_traits::descriptors, fatal::get_type::id>(what.getType(), variant_member_printer(), what); std::cout << '}'; } }; template void print_as(TC, T const& what) { printer::print(what); } template void print(T const& what) { print_as(apache::thrift::reflect_type_class_of_thrift_class{}, what); }