initial commit
This commit is contained in:
commit
44aa8d060a
7 changed files with 1027 additions and 0 deletions
76
include/soi-pretty.hpp
Normal file
76
include/soi-pretty.hpp
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// 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_H_PRETTY
|
||||
#define SOI_H_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_H_PRETTY
|
||||
Loading…
Add table
Add a link
Reference in a new issue