#ifndef BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED #define BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED // Copyright 2018-2024 Emil Dotchevski and Reverge Studios, Inc. // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) #include #include #include #include #include #include namespace boost { namespace leaf { template struct show_in_diagnostics: std::true_type { }; namespace detail { template struct is_printable: std::false_type { }; template struct is_printable()<(), void())>: show_in_diagnostics { }; //////////////////////////////////////// template struct has_printable_member_value: std::false_type { }; template struct has_printable_member_value()<().value, void())>: show_in_diagnostics { }; //////////////////////////////////////// template void print_name(std::basic_ostream & os, char const * & prefix, char const * delimiter) { static_assert(show_in_diagnostics::value, "show_in_diagnostics violation"); BOOST_LEAF_ASSERT(delimiter); char const * p = prefix; prefix = nullptr; os << (p ? p : delimiter) << parse(); } template bool print_impl(std::basic_ostream & os, char const * & prefix, char const * delimiter, char const * mid, PrintableInfo const & x) { print_name(os, prefix, delimiter); if( mid ) os << mid << x; return true; } template bool print_impl(std::basic_ostream & os, char const * & prefix, char const * delimiter, char const * mid, PrintableInfo const * x) { print_name(os, prefix, delimiter); if( mid ) { os << mid; if( x ) os << x; else os << ""; } return true; } //////////////////////////////////////// template < class Wrapper, bool ShowInDiagnostics = show_in_diagnostics::value, bool WrapperPrintable = is_printable::value, bool ValuePrintable = has_printable_member_value::value, bool IsException = std::is_base_of::value, bool IsEnum = std::is_enum::value> struct diagnostic; template struct diagnostic { template static bool print(std::basic_ostream &, char const * &, char const *, Wrapper const & x) noexcept { return false; } }; template struct diagnostic { template static bool print(std::basic_ostream & os, char const * & prefix, char const * delimiter, Wrapper const & x) { return print_impl(os, prefix, delimiter, ": ", x); } }; template struct diagnostic { template static bool print(std::basic_ostream & os, char const * & prefix, char const * delimiter, Wrapper const & x) { return print_impl(os, prefix, delimiter, ": ", x.value); } }; template struct diagnostic { template static bool print(std::basic_ostream & os, char const * & prefix, char const * delimiter, Exception const & ex) { if( print_impl(os, prefix, delimiter, ": \"", static_cast(ex).what()) ) { os << '"'; return true; } return false; } }; template struct diagnostic { template static bool print(std::basic_ostream & os, char const * & prefix, char const * delimiter, Wrapper const &) { return print_impl(os, prefix, delimiter, nullptr, 0); } }; template struct diagnostic { template static bool print(std::basic_ostream & os, char const * & prefix, char const * delimiter, Enum const & enum_) { return print_impl(os, prefix, delimiter, ": ", static_cast::type>(enum_)); } }; } } } #endif // BOOST_LEAF_DETAIL_PRINT_HPP_INCLUDED