implement read() and print() functions for easier I/O
This commit is contained in:
parent
a994e7d10d
commit
11b6ae9729
7 changed files with 67 additions and 38 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <bitset>
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <complex>
|
||||
#include <cstddef>
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
|
||||
namespace soi {
|
||||
|
||||
namespace prettyprint {
|
||||
|
||||
template <typename T, typename TChar, typename TCharTraits>
|
||||
std::basic_ostream<TChar, TCharTraits> &
|
||||
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x);
|
||||
|
|
@ -110,13 +112,13 @@ struct pretty_print_container_helper {
|
|||
|
||||
if (it != the_end) {
|
||||
for (;;) {
|
||||
::soi::print(stream, *it);
|
||||
::soi::prettyprint::print(stream, *it);
|
||||
|
||||
if (++it == the_end)
|
||||
break;
|
||||
|
||||
if (delimiters_type::values.delimiter != NULL)
|
||||
::soi::print(stream, delimiters_type::values.delimiter);
|
||||
::soi::prettyprint::print(stream, delimiters_type::values.delimiter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -126,12 +128,12 @@ struct pretty_print_container_helper {
|
|||
|
||||
inline void operator()(ostream_type &stream) const {
|
||||
if (delimiters_type::values.prefix != NULL)
|
||||
::soi::print(stream, delimiters_type::values.prefix);
|
||||
::soi::prettyprint::print(stream, delimiters_type::values.prefix);
|
||||
|
||||
pretty_printer<T>::pretty_print_body(container_, stream);
|
||||
|
||||
if (delimiters_type::values.postfix != NULL)
|
||||
::soi::print(stream, delimiters_type::values.postfix);
|
||||
::soi::prettyprint::print(stream, delimiters_type::values.postfix);
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -151,16 +153,16 @@ struct pretty_print_container_helper<
|
|||
|
||||
static void pretty_print_body(const std::pair<T1, T2> &c,
|
||||
ostream_type &stream) {
|
||||
::soi::print(stream, c.first);
|
||||
::soi::prettyprint::print(stream, c.first);
|
||||
if (pretty_print_container_helper<T, TChar, TCharTraits,
|
||||
TDelimiters>::delimiters_type::values
|
||||
.delimiter != NULL)
|
||||
::soi::print(
|
||||
::soi::prettyprint::print(
|
||||
stream,
|
||||
pretty_print_container_helper<T, TChar, TCharTraits,
|
||||
TDelimiters>::delimiters_type::values
|
||||
.delimiter);
|
||||
::soi::print(stream, c.second);
|
||||
::soi::prettyprint::print(stream, c.second);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -189,7 +191,7 @@ struct pretty_print_container_helper<
|
|||
tuple_pretty_print(const element_type &c, ostream_type &stream,
|
||||
typename std::conditional<sizeof...(Args) != 0, Int<0>,
|
||||
std::nullptr_t>::type) {
|
||||
::soi::print(stream, std::get<0>(c));
|
||||
::soi::prettyprint::print(stream, std::get<0>(c));
|
||||
tuple_pretty_print(c, stream, Int<1>());
|
||||
}
|
||||
|
||||
|
|
@ -199,13 +201,13 @@ struct pretty_print_container_helper<
|
|||
if (pretty_print_container_helper<T, TChar, TCharTraits,
|
||||
TDelimiters>::delimiters_type::values
|
||||
.delimiter != NULL)
|
||||
::soi::print(
|
||||
::soi::prettyprint::print(
|
||||
stream,
|
||||
pretty_print_container_helper<T, TChar, TCharTraits,
|
||||
TDelimiters>::delimiters_type::values
|
||||
.delimiter);
|
||||
|
||||
::soi::print(stream, std::get<N>(c));
|
||||
::soi::prettyprint::print(stream, std::get<N>(c));
|
||||
|
||||
tuple_pretty_print(c, stream, Int<N + 1>());
|
||||
}
|
||||
|
|
@ -375,7 +377,7 @@ const delimiters_values<wchar_t>
|
|||
// Type-erasing helper class for easy use of custom delimiters.
|
||||
// Requires TCharTraits = std::char_traits<TChar> and TChar = char or wchar_t,
|
||||
// and MyDelims needs to be defined for TChar. Usage: "cout <<
|
||||
// pretty_pretty_print::custom_delims<MyDelims>(x)".
|
||||
// pretty_prettyprint::custom_delims<MyDelims>(x)".
|
||||
|
||||
struct custom_delims_base {
|
||||
virtual ~custom_delims_base() {}
|
||||
|
|
@ -455,22 +457,20 @@ private:
|
|||
const size_type n;
|
||||
};
|
||||
|
||||
} // namespace soi
|
||||
|
||||
/*
|
||||
// Global accessor functions for the convenience wrappers
|
||||
|
||||
template<typename T>
|
||||
inline pretty_pretty_print::array_wrapper_n<T> pretty_pretty_print_array(const T
|
||||
inline pretty_prettyprint::array_wrapper_n<T> pretty_pretty_print_array(const T
|
||||
* const a, size_t n)
|
||||
{
|
||||
return pretty_pretty_print::array_wrapper_n<T>(a, n);
|
||||
return pretty_prettyprint::array_wrapper_n<T>(a, n);
|
||||
}
|
||||
|
||||
template <typename T> pretty_pretty_print::bucket_pretty_print_wrapper<T>
|
||||
template <typename T> pretty_prettyprint::bucket_pretty_print_wrapper<T>
|
||||
bucket_pretty_print(const T & m, typename T::size_type n)
|
||||
{
|
||||
return pretty_pretty_print::bucket_pretty_print_wrapper<T>(m, n);
|
||||
return pretty_prettyprint::bucket_pretty_print_wrapper<T>(m, n);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -479,18 +479,18 @@ return pretty_pretty_print::bucket_pretty_print_wrapper<T>(m, n);
|
|||
|
||||
*/
|
||||
|
||||
namespace soi {
|
||||
// Pretty_Prints a container to the stream using default delimiters
|
||||
|
||||
template <typename T, typename TChar, typename TCharTraits>
|
||||
inline typename std::enable_if<::soi::is_container<T>::value,
|
||||
inline typename std::enable_if<::soi::prettyprint::is_container<T>::value,
|
||||
std::basic_ostream<TChar, TCharTraits> &>::type
|
||||
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream,
|
||||
const T &container) {
|
||||
return ::soi::print(
|
||||
return ::soi::prettyprint::print(
|
||||
stream,
|
||||
::soi::pretty_print_container_helper<T, TChar, TCharTraits>(container));
|
||||
::soi::prettyprint::pretty_print_container_helper<T, TChar, TCharTraits>(container));
|
||||
}
|
||||
|
||||
}// namespace prettyprint
|
||||
|
||||
} // namespace soi
|
||||
|
||||
#endif // SOI_PRETTY_PRETTY_PRINT
|
||||
|
|
|
|||
|
|
@ -172,7 +172,7 @@ T &&dbg_print(T &&value, std::string const& type, char const *file, int line,
|
|||
const T &ref = value;
|
||||
std::stringstream
|
||||
value_buffer; // avoid nesting of dbg macros within print functions
|
||||
soi::print(value_buffer, ref);
|
||||
soi::prettyprint::print(value_buffer, ref);
|
||||
|
||||
std::cerr << ANSI_DEBUG << "[" << file << ":" << line << " (" << function_name
|
||||
<< ")] " << ANSI_RESET << ANSI_EXPRESSION
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ realloc(void *ptr, std::size_t new_size);
|
|||
[[deprecated("malloc/free is evil. Use a vector.")]] void *
|
||||
free(void *ptr, std::size_t new_size);
|
||||
|
||||
[[deprecated("new is evil. Use a vector.")]] void *
|
||||
operator new(std::size_t sz);
|
||||
[[deprecated("delete is evil. Use a vector.")]] void
|
||||
operator delete(void *ptr) noexcept;
|
||||
//[[deprecated("new is evil. Use a vector.")]] void *
|
||||
//operator new(std::size_t sz);
|
||||
//[[deprecated("delete is evil. Use a vector.")]] void
|
||||
//operator delete(void *ptr) noexcept;
|
||||
|
|
|
|||
|
|
@ -13,6 +13,8 @@
|
|||
|
||||
namespace soi {
|
||||
|
||||
namespace prettyprint {
|
||||
|
||||
template <typename TChar, typename TCharTraits>
|
||||
std::basic_ostream<TChar, TCharTraits> &
|
||||
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const bool &x) {
|
||||
|
|
@ -84,7 +86,7 @@ pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, char c) {
|
|||
}
|
||||
|
||||
template <typename T, typename TChar, typename TCharTraits>
|
||||
inline typename std::enable_if<!::soi::is_container<T>::value,
|
||||
inline typename std::enable_if<!::soi::prettyprint::is_container<T>::value,
|
||||
std::basic_ostream<TChar, TCharTraits> &>::type
|
||||
pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
|
||||
return stream << x;
|
||||
|
|
@ -93,8 +95,11 @@ pretty_print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
|
|||
template <typename T, typename TChar, typename TCharTraits>
|
||||
std::basic_ostream<TChar, TCharTraits> &
|
||||
print(std::basic_ostream<TChar, TCharTraits> &stream, const T &x) {
|
||||
return pretty_print(stream, x);
|
||||
return ::soi::prettyprint::pretty_print(stream, x);
|
||||
}
|
||||
|
||||
} // namespace prettyprint
|
||||
|
||||
} // namespace soi
|
||||
|
||||
#endif // SOI_PRETTY
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue