soi-header/include/bits/soi-pretty.hpp

77 lines
2.4 KiB
C++

// Copyright Johannes Kapfhammer 2019.
// 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)
//
// pretty printing with c++
//
#ifndef SOI_PRETTY
#define SOI_PRETTY
#include "prettyprint.hpp"
namespace soi_h {
template<typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if< ::pretty_print::is_container<T>::value,
std::basic_ostream<TChar, TCharTraits> &>::type
pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, const T & container) {
return stream << ::pretty_print::print_container_helper<T, TChar, TCharTraits>(container);
}
template<typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, const bool& x) {
return stream << (x ? "true" : "false");
}
namespace detail {
template<typename TChar, typename TCharTraits>
void escape_char(std::basic_ostream<TChar, TCharTraits> & stream, char c) {
switch (c) {
case '\a': stream << "\\a"; break;
case '\b': stream << "\\b"; break;
case '\t': stream << "\\t"; break;
case '\n': stream << "\\n"; break;
case '\v': stream << "\\v"; break;
case '\f': stream << "\\f"; break;
case '\r': stream << "\\r"; break;
case '\e': stream << "\\e"; break;
case '\"': stream << "\\\""; break;
case '\'': stream << "\\'"; break;
case '\?': stream << "\\?"; break;
case '\\': stream << "\\\\"; break;
default: stream << c;
}
}
}
template<typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, const std::string& x) {
stream << "\"";
for (char c : x)
detail::escape_char(stream, c);
return stream << "\"";
}
template<typename TChar, typename TCharTraits>
std::basic_ostream<TChar, TCharTraits> &
pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, char c) {
stream << "'";
detail::escape_char(stream, c);
return stream << "'";
}
template<typename T, typename TChar, typename TCharTraits>
inline typename std::enable_if< !::pretty_print::is_container<T>::value,
std::basic_ostream<TChar, TCharTraits> &>::type
pretty_print(std::basic_ostream<TChar, TCharTraits> & stream, const T& x) {
return stream << x;
}
}
#endif // SOI_PRETTY