From 10e239f933d94cfcbb4e592b26714867e2427309 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Tue, 29 May 2018 19:30:30 +0900 Subject: [PATCH] add sprout::swallow --- sprout/tuple/tuple/tuple_decl.hpp | 6 +- sprout/utility/operation_ext.hpp | 1 + sprout/utility/swallow.hpp | 23 ++++ testspr/print.hpp | 184 ++++++++++++++++++++++-------- 4 files changed, 166 insertions(+), 48 deletions(-) create mode 100644 sprout/utility/swallow.hpp diff --git a/sprout/tuple/tuple/tuple_decl.hpp b/sprout/tuple/tuple/tuple_decl.hpp index bd9b591d..59194d49 100644 --- a/sprout/tuple/tuple/tuple_decl.hpp +++ b/sprout/tuple/tuple/tuple_decl.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include #include @@ -286,7 +286,7 @@ namespace sprout { template SPROUT_CXX14_CONSTEXPR void swap_impl(tuple& other, sprout::index_tuple) { - sprout::eat((sprout::swap(base_type::template get(*this), base_type::template get(other)), 0)...); + sprout::swallow({(sprout::swap(base_type::template get(*this), base_type::template get(other)), 0)...}); } template SPROUT_CXX14_CONSTEXPR void @@ -297,7 +297,7 @@ namespace sprout { SPROUT_CXX14_CONSTEXPR void assign_impl(Tuple&& t, sprout::index_tuple) { typedef typename std::decay::type type; - sprout::eat((sprout::eat(base_type::template get(*this) = sprout::move(type::template get(SPROUT_FORWARD(Tuple, t)))), 0)...); + sprout::swallow({(base_type::template get(*this) = sprout::move(type::template get(SPROUT_FORWARD(Tuple, t))), 0)...}); } template SPROUT_CONSTEXPR std::tuple diff --git a/sprout/utility/operation_ext.hpp b/sprout/utility/operation_ext.hpp index f0535672..a6cc5632 100644 --- a/sprout/utility/operation_ext.hpp +++ b/sprout/utility/operation_ext.hpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include diff --git a/sprout/utility/swallow.hpp b/sprout/utility/swallow.hpp new file mode 100644 index 00000000..0546b240 --- /dev/null +++ b/sprout/utility/swallow.hpp @@ -0,0 +1,23 @@ +/*============================================================================= + Copyright (c) 2011-2017 Bolero MURAKAMI + 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) +=============================================================================*/ +#ifndef SPROUT_UTILITY_SWALLOW_HPP +#define SPROUT_UTILITY_SWALLOW_HPP + +#include +#include + +namespace sprout { + // + // swallow + // + template + inline SPROUT_CXX14_CONSTEXPR void + swallow(std::initializer_list) SPROUT_NOEXCEPT {} +} // namespace sprout + +#endif // #ifndef SPROUT_UTILITY_SWALLOW_HPP diff --git a/testspr/print.hpp b/testspr/print.hpp index afe82378..f5f171d9 100644 --- a/testspr/print.hpp +++ b/testspr/print.hpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -26,15 +27,16 @@ namespace testspr { namespace detail { inline SPROUT_NON_CONSTEXPR std::ostream& - to_string_impl(std::ostream& os) { + output(std::ostream& os) { return os; } template inline SPROUT_NON_CONSTEXPR std::ostream& - to_string_impl(std::ostream& os, Head const& head, Tail const&... tail) { - return testspr::detail::to_string_impl(os << head, tail...); + output(std::ostream& os, Head const& head, Tail const&... tail) { + return testspr::detail::output(os << head, tail...); } } // namespace detail + // // to_string // @@ -42,10 +44,62 @@ namespace testspr { inline SPROUT_NON_CONSTEXPR std::string to_string(Head const& head, Tail const&... tail) { std::ostringstream os; - testspr::detail::to_string_impl(os, head, tail...); + testspr::detail::output(os, head, tail...); return os.str(); } + // + // endl + // + class endl_t {}; + template + inline SPROUT_NON_CONSTEXPR std::basic_ostream& + operator<<(std::basic_ostream& lhs, testspr::endl_t) { + return lhs << std::endl; + } + namespace { + SPROUT_STATIC_CONSTEXPR endl_t endl = {}; + } // anonymous-namespace + // + // ends + // + class ends_t {}; + template + inline SPROUT_NON_CONSTEXPR std::basic_ostream& + operator<<(std::basic_ostream& lhs, testspr::ends_t) { + return lhs << std::ends; + } + namespace { + SPROUT_STATIC_CONSTEXPR ends_t ends = {}; + } // anonymous-namespace + // + // flush + // + class flush_t {}; + template + inline SPROUT_NON_CONSTEXPR std::basic_ostream& + operator<<(std::basic_ostream& 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) + {} + }; + // // print // @@ -63,62 +117,98 @@ namespace testspr { // inline SPROUT_NON_CONSTEXPR void print_ln() { - std::cout << std::endl; + testspr::print(testspr::endl); } template inline SPROUT_NON_CONSTEXPR void print_ln(Args const&... args) { - sprout::detail::io::ios_all_saver saver(std::cout); - testspr::print(args...); - std::cout << std::endl; + testspr::ios_all_saver saver; + testspr::print(args..., testspr::endl); } + // + // print_tokens_d + // + template + inline SPROUT_NON_CONSTEXPR void + print_tokens_d(T const&) { + testspr::print_ln(); + } + template + 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 inline SPROUT_NON_CONSTEXPR void - print_tokens() { - testspr::print_ln(); - } - template - inline SPROUT_NON_CONSTEXPR void - print_tokens(Head const& head, Tail const&... tail) { - sprout::detail::io::ios_all_saver saver(std::cout); - testspr::print(head, ' '); - testspr::print_tokens(tail...); + print_tokens(Args const&... args) { + print_tokens_d(' ', args...); } + // + // print_quotes_d + // + template + inline SPROUT_NON_CONSTEXPR void + print_quotes_d(T const&) { + testspr::print_ln(); + } + template + 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 inline SPROUT_NON_CONSTEXPR void - print_quotes() { - testspr::print_ln(); - } - template - inline SPROUT_NON_CONSTEXPR void - print_quotes(Head const& head, Tail const&... tail) { - sprout::detail::io::ios_all_saver saver(std::cout); - testspr::print('\"', head, "\" "); - testspr::print_quotes(tail...); + print_quotes(Args const&... args) { + print_quotes_d(' ', args...); } + // + // print_range_d + // + template + 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 + 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)); + } // // print_range // template inline SPROUT_NON_CONSTEXPR void print_range(InputIterator first, InputIterator last) { - sprout::detail::io::ios_all_saver saver(std::cout); - for (; first != last; ++first) { - std::cout << *first << ' '; - } - std::cout << std::endl; + print_range_d(' ', first, last); } template inline SPROUT_NON_CONSTEXPR void print_range(InputRange const& range) { - testspr::print_range(sprout::begin(range), sprout::end(range)); + print_range_d(' ', range); } // @@ -164,21 +254,21 @@ namespace testspr { inline SPROUT_NON_CONSTEXPR void print_hl(char c) { for (std::string::size_type i = 0, last = 80; i != last; ++i) { - std::cout << c; + testspr::print(c); } - std::cout << std::endl; + testspr::print_ln(); } template inline SPROUT_NON_CONSTEXPR void print_hl(T const& t) { std::string s(testspr::to_string(t)); for (std::string::size_type i = 0, n = 80 / s.size(); i != n; ++i) { - std::cout << s; + testspr::print(s); } for (std::string::const_iterator it = s.begin(), last = s.begin() + 80 % s.size(); it != last; ++it) { - std::cout << *it; + testspr::print(*it); } - std::cout << std::endl; + testspr::print_ln(); } // @@ -212,18 +302,22 @@ namespace testspr { // manip // template - inline SPROUT_NON_CONSTEXPR T&& - manip(T&& t) { - return std::forward(t); + inline SPROUT_NON_CONSTEXPR T& + manip(T& t) { + return t; } - template - inline SPROUT_NON_CONSTEXPR testspr::manip_holder& (*)(std::basic_ostream&)> - manip(std::basic_ostream& (*pf)(std::basic_ostream&)) { + template + inline SPROUT_NON_CONSTEXPR T const& + manip(T const& t) { + return t; + } + inline SPROUT_NON_CONSTEXPR testspr::manip_holder + manip(std::ostream& (*pf)(std::ostream&)) { return pf; } template - inline SPROUT_NON_CONSTEXPR testspr::manip_holder& (*)(std::basic_ios&)> - manip(std::basic_ios& (*pf)(std::basic_ios&)) { + inline SPROUT_NON_CONSTEXPR testspr::manip_holder + manip(std::ios& (*pf)(std::ios&)) { return pf; } inline SPROUT_NON_CONSTEXPR testspr::manip_holder