Sprout/testspr/print.hpp

341 lines
8.7 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2017 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
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)
=============================================================================*/
2011-12-23 11:59:33 +00:00
#ifndef TESTSPR_PRINT_HPP
#define TESTSPR_PRINT_HPP
#include <algorithm>
#include <iterator>
#include <bitset>
2014-02-19 23:04:36 +00:00
#include <utility>
2017-09-29 14:17:17 +00:00
#include <string>
#include <sstream>
2014-02-19 23:04:36 +00:00
#include <iomanip>
2017-09-29 14:17:17 +00:00
#include <iostream>
2014-07-22 00:33:13 +00:00
#include <sprout/config.hpp>
2017-09-19 11:01:44 +00:00
#include <sprout/preprocessor/config.hpp>
#include <sprout/preprocessor/stringize.hpp>
2018-05-29 10:30:30 +00:00
#include <sprout/utility/swallow.hpp>
#include <sprout/container.hpp>
2014-02-19 23:04:36 +00:00
#include <sprout/detail/io/ios_state.hpp>
#include <testspr/typeinfo.hpp>
2011-12-23 11:59:33 +00:00
namespace testspr {
2017-09-29 22:23:16 +00:00
namespace detail {
inline SPROUT_NON_CONSTEXPR std::ostream&
2018-05-29 10:30:30 +00:00
output(std::ostream& os) {
2017-09-29 22:23:16 +00:00
return os;
}
template<typename Head, typename... Tail>
inline SPROUT_NON_CONSTEXPR std::ostream&
2018-05-29 10:30:30 +00:00
output(std::ostream& os, Head const& head, Tail const&... tail) {
return testspr::detail::output(os << head, tail...);
2017-09-29 22:23:16 +00:00
}
} // namespace detail
2018-05-29 10:30:30 +00:00
2017-09-29 22:23:16 +00:00
//
// to_string
//
template<typename Head, typename... Tail>
inline SPROUT_NON_CONSTEXPR std::string
to_string(Head const& head, Tail const&... tail) {
std::ostringstream os;
2018-05-29 10:30:30 +00:00
testspr::detail::output(os, head, tail...);
2017-09-29 22:23:16 +00:00
return os.str();
}
2018-05-29 10:30:30 +00:00
//
// endl
//
class endl_t {};
template<typename Elem, typename Traits>
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, testspr::endl_t) {
return lhs << std::endl;
}
namespace {
SPROUT_STATIC_CONSTEXPR endl_t endl = {};
} // anonymous-namespace
//
// ends
//
class ends_t {};
template<typename Elem, typename Traits>
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, testspr::ends_t) {
return lhs << std::ends;
}
namespace {
SPROUT_STATIC_CONSTEXPR ends_t ends = {};
} // anonymous-namespace
//
// flush
//
class flush_t {};
template<typename Elem, typename Traits>
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, testspr::flush_t) {
return lhs << std::flush;
}
namespace {
SPROUT_STATIC_CONSTEXPR flush_t flush = {};
} // anonymous-namespace
//
// ios_all_saver
//
class ios_all_saver {
private:
sprout::detail::io::ios_all_saver saver_;
public:
SPROUT_NON_CONSTEXPR ios_all_saver()
: saver_(std::cout)
{}
SPROUT_NON_CONSTEXPR ios_all_saver(std::ostream& os)
: saver_(os)
{}
};
2011-12-23 11:59:33 +00:00
//
// print
//
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print() {}
2014-02-19 23:04:36 +00:00
template<typename Head, typename... Tail>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print(Head const& head, Tail const&... tail) {
2014-02-19 23:04:36 +00:00
std::cout << head;
testspr::print(tail...);
}
//
// print_ln
//
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_ln() {
2018-05-29 10:30:30 +00:00
testspr::print(testspr::endl);
}
2014-02-19 23:04:36 +00:00
template<typename... Args>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_ln(Args const&... args) {
2018-05-29 10:30:30 +00:00
testspr::ios_all_saver saver;
testspr::print(args..., testspr::endl);
2014-02-19 23:04:36 +00:00
}
//
2018-05-29 10:30:30 +00:00
// print_tokens_d
2014-02-19 23:04:36 +00:00
//
2018-05-29 10:30:30 +00:00
template<typename T>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
2018-05-29 10:30:30 +00:00
print_tokens_d(T const&) {
2014-02-19 23:04:36 +00:00
testspr::print_ln();
}
2018-05-29 10:30:30 +00:00
template<typename T, typename Head, typename... Tail>
inline SPROUT_NON_CONSTEXPR void
print_tokens_d(T const& delim, Head const& head, Tail const&... tail) {
testspr::ios_all_saver saver;
testspr::print(head);
sprout::swallow({(testspr::print(delim, tail), 0)...});
testspr::print_ln();
}
//
// print_tokens
//
template<typename... Args>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
2018-05-29 10:30:30 +00:00
print_tokens(Args const&... args) {
print_tokens_d(' ', args...);
2014-02-19 23:04:36 +00:00
}
//
2018-05-29 10:30:30 +00:00
// print_quotes_d
2014-02-19 23:04:36 +00:00
//
2018-05-29 10:30:30 +00:00
template<typename T>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
2018-05-29 10:30:30 +00:00
print_quotes_d(T const&) {
2014-02-19 23:04:36 +00:00
testspr::print_ln();
}
2018-05-29 10:30:30 +00:00
template<typename T, typename Head, typename... Tail>
inline SPROUT_NON_CONSTEXPR void
print_quotes_d(T const& delim, Head const& head, Tail const&... tail) {
testspr::ios_all_saver saver;
testspr::print('\"', head, '\"');
sprout::swallow({(testspr::print(delim, '\"', tail, '\"'), 0)...});
testspr::print_ln();
}
//
// print_quotes
//
template<typename... Args>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
2018-05-29 10:30:30 +00:00
print_quotes(Args const&... args) {
print_quotes_d(' ', args...);
2014-02-19 23:04:36 +00:00
}
2018-05-29 10:30:30 +00:00
//
// print_range_d
//
template<typename T, typename InputIterator>
inline SPROUT_NON_CONSTEXPR void
print_range_d(T const& delim, InputIterator first, InputIterator last) {
testspr::ios_all_saver saver;
if (first != last) {
testspr::print(*first);
}
++first;
for (; first != last; ++first) {
testspr::print(delim, *first);
}
testspr::print_ln();
}
template<typename T, typename InputRange>
inline SPROUT_NON_CONSTEXPR void
print_range_d(T const& delim, InputRange const& range) {
testspr::print_range_d(delim, sprout::begin(range), sprout::end(range));
}
2014-02-19 23:04:36 +00:00
//
// print_range
//
template<typename InputIterator>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_range(InputIterator first, InputIterator last) {
2018-05-29 10:30:30 +00:00
print_range_d(' ', first, last);
2014-02-19 23:04:36 +00:00
}
template<typename InputRange>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_range(InputRange const& range) {
2018-05-29 10:30:30 +00:00
print_range_d(' ', range);
}
//
// print_bits
//
template<typename T>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_bits(T const& t) {
testspr::print_ln(std::bitset<sizeof(T) * 8>(t).template to_string<char>());
2011-12-23 11:59:33 +00:00
}
//
// print_typename
//
template<typename T>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_typename() {
testspr::print_ln(testspr::typename_of<T>());
}
2017-10-18 05:42:38 +00:00
template<typename T>
inline SPROUT_NON_CONSTEXPR void
print_typename(T&& t) {
testspr::print_ln(testspr::typename_of(std::forward<T>(t)));
}
//
// print_type
//
template<typename T>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_type() {
2017-07-26 06:22:14 +00:00
testspr::print_ln(testspr::qualified_typename_of<T>());
}
2011-12-23 11:59:33 +00:00
//
// print_hl
//
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR void
print_hl() {
testspr::print_ln("--------------------------------------------------------------------------------");
2011-12-23 11:59:33 +00:00
}
2017-09-29 14:17:17 +00:00
template<typename T>
inline SPROUT_NON_CONSTEXPR void
print_hl(char c) {
for (std::string::size_type i = 0, last = 80; i != last; ++i) {
2018-05-29 10:30:30 +00:00
testspr::print(c);
2017-09-29 14:17:17 +00:00
}
2018-05-29 10:30:30 +00:00
testspr::print_ln();
2017-09-29 14:17:17 +00:00
}
template<typename T>
inline SPROUT_NON_CONSTEXPR void
print_hl(T const& t) {
2017-09-29 22:23:16 +00:00
std::string s(testspr::to_string(t));
for (std::string::size_type i = 0, n = 80 / s.size(); i != n; ++i) {
2018-05-29 10:30:30 +00:00
testspr::print(s);
2017-09-29 14:17:17 +00:00
}
2017-09-29 22:23:16 +00:00
for (std::string::const_iterator it = s.begin(), last = s.begin() + 80 % s.size(); it != last; ++it) {
2018-05-29 10:30:30 +00:00
testspr::print(*it);
2017-09-29 22:23:16 +00:00
}
2018-05-29 10:30:30 +00:00
testspr::print_ln();
2017-09-29 14:17:17 +00:00
}
2014-02-19 23:04:36 +00:00
//
// manip_holder
//
template<typename T>
class manip_holder {
public:
typedef T value_type;
private:
value_type m_;
public:
2014-07-22 00:33:13 +00:00
SPROUT_CONSTEXPR manip_holder(value_type const& m)
2014-02-19 23:04:36 +00:00
: m_(m)
{}
2014-07-22 00:33:13 +00:00
SPROUT_CONSTEXPR value_type const& get() const {
2014-02-19 23:04:36 +00:00
return m_;
}
};
template<typename T, typename Elem, typename Traits>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
operator<<(std::basic_ostream<Elem, Traits>& lhs, testspr::manip_holder<T> const& rhs) {
2014-02-19 23:04:36 +00:00
return lhs << rhs.get();
}
template<typename T, typename Elem, typename Traits>
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
operator>>(std::basic_istream<Elem, Traits>& lhs, testspr::manip_holder<T> const& rhs) {
2014-02-19 23:04:36 +00:00
return lhs >> rhs.get();
}
//
// manip
//
template<typename T>
2018-05-29 10:30:30 +00:00
inline SPROUT_NON_CONSTEXPR T&
manip(T& t) {
return t;
2014-02-19 23:04:36 +00:00
}
2018-05-29 10:30:30 +00:00
template<typename T>
inline SPROUT_NON_CONSTEXPR T const&
manip(T const& t) {
return t;
}
inline SPROUT_NON_CONSTEXPR testspr::manip_holder<std::ostream& (*)(std::ostream&)>
manip(std::ostream& (*pf)(std::ostream&)) {
2014-02-19 23:04:36 +00:00
return pf;
}
template<typename Elem, typename Traits>
2018-05-29 10:30:30 +00:00
inline SPROUT_NON_CONSTEXPR testspr::manip_holder<std::ios& (*)(std::ios&)>
manip(std::ios& (*pf)(std::ios&)) {
2014-02-19 23:04:36 +00:00
return pf;
}
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR testspr::manip_holder<std::ios_base& (*)(std::ios_base&)>
2014-02-19 23:04:36 +00:00
manip(std::ios_base& (*pf)(std::ios_base&)) {
return pf;
}
2011-12-23 11:59:33 +00:00
} // namespace testspr
2017-09-19 11:01:44 +00:00
//
// TESTSPR_PRINT_EXPR
//
#if SPROUT_PP_VARIADICS
# define TESTSPR_PRINT_EXPR(...) \
::testspr::print_ln(SPROUT_PP_STRINGIZE((__VA_ARGS__)), " => ", __VA_ARGS__)
#else
# define TESTSPR_PRINT_EXPR(EXPR) \
::testspr::print_ln(SPROUT_PP_STRINGIZE((EXPR)), " => ", EXPR)
#endif
2011-12-23 11:59:33 +00:00
#endif // #ifndef TESTSPR_PRINT_HPP