From 11b6ae9729bd3f9b72a94cc6158d1583a435be3f Mon Sep 17 00:00:00 2001 From: Johannes Kapfhammer Date: Thu, 10 Oct 2019 14:05:52 +0200 Subject: [PATCH] implement read() and print() functions for easier I/O --- example.cpp | 35 ++++++++++++++++++++------ include/bits/include-all.hpp | 1 + include/bits/prettyprint.hpp | 46 +++++++++++++++++----------------- include/bits/soi-dbg.hpp | 2 +- include/bits/soi-deprecate.hpp | 8 +++--- include/bits/soi-pretty.hpp | 9 +++++-- include/soi | 4 ++- 7 files changed, 67 insertions(+), 38 deletions(-) diff --git a/example.cpp b/example.cpp index b561fc6..41e8b7b 100644 --- a/example.cpp +++ b/example.cpp @@ -1,4 +1,4 @@ -// -*- compile-command: "g++ -Iinclude -D_GLIBCXX_DEBUG -fsanitize=address,undefined -g3 -ggdb3 -std=c++17 example.cpp -o example && SOI_COLOR=1 SOI_EOFCHECK=1 ./example <<< ''" -*- +// -*- compile-command: "g++ -Iinclude -D_GLIBCXX_DEBUG -fsanitize=address,undefined -g3 -ggdb3 -std=c++17 example.cpp -o example && SOI_COLOR=1 SOI_EOFCHECK=1 ./example <<< '4 5 blub 3 1 4'" -*- #include inline std::string methodName(const std::string& prettyFunction) { @@ -9,26 +9,35 @@ inline std::string methodName(const std::string& prettyFunction) { } struct A{ - void operator()() const { + void operator()() const { dbg(methodName(__PRETTY_FUNCTION__)); - dbg("here"); - } + dbg("here"); + } void bar(int a, int b, string c) const { dbg("imbar"); } void foo(int a=3, int b=5, string c="hi") const { - dbg(methodName(__PRETTY_FUNCTION__)); dbg("bar"); - } + } }; signed main() { - dbg(methodName(__PRETTY_FUNCTION__)); A a; a(); a.foo(); a.bar(3,4,"hi"); + int i = read_int(); + int j = read_int(); + print(i, j); + + print("read string",read()); + print('h','e','l','l','o'); + print("false",'=',false); + print("true",'=',true); + + print(read_vector(3)); + int l=3, r=5; dbg(tuple{l, r}); dbg(); @@ -48,4 +57,16 @@ signed main() { dbg(map{{3,"three"},{1,"one"}}); dbg(tuple{unordered_map{{3,"three"},{1,"one"}}}); dbg(unordered_set{3,1,4}); + + print("v-- empty line"); + print(); + print("^-- empty line"); + print("tuple",make_tuple(3, 1, 4, 1)); + print(vector{3,1,4,1,5,9,2,6}); + print(vector>{{3,1},{4,1}}); + print(vector>>{{3,{"hi"}},{4,{"hello", "world"}}}); + print(set{3,1,4}); + print(map{{3,"three"},{1,"one"}}); + print(tuple{unordered_map{{3,"three"},{1,"one"}}}); + print(unordered_set{3,1,4}); } diff --git a/include/bits/include-all.hpp b/include/bits/include-all.hpp index b160e83..86d5c1d 100644 --- a/include/bits/include-all.hpp +++ b/include/bits/include-all.hpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/include/bits/prettyprint.hpp b/include/bits/prettyprint.hpp index 862ee3d..1a27b45 100644 --- a/include/bits/prettyprint.hpp +++ b/include/bits/prettyprint.hpp @@ -23,6 +23,8 @@ namespace soi { +namespace prettyprint { + template std::basic_ostream & print(std::basic_ostream &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::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 &c, ostream_type &stream) { - ::soi::print(stream, c.first); + ::soi::prettyprint::print(stream, c.first); if (pretty_print_container_helper::delimiters_type::values .delimiter != NULL) - ::soi::print( + ::soi::prettyprint::print( stream, pretty_print_container_helper::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, 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::delimiters_type::values .delimiter != NULL) - ::soi::print( + ::soi::prettyprint::print( stream, pretty_print_container_helper::delimiters_type::values .delimiter); - ::soi::print(stream, std::get(c)); + ::soi::prettyprint::print(stream, std::get(c)); tuple_pretty_print(c, stream, Int()); } @@ -375,7 +377,7 @@ const delimiters_values // Type-erasing helper class for easy use of custom delimiters. // Requires TCharTraits = std::char_traits and TChar = char or wchar_t, // and MyDelims needs to be defined for TChar. Usage: "cout << -// pretty_pretty_print::custom_delims(x)". +// pretty_prettyprint::custom_delims(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 -inline pretty_pretty_print::array_wrapper_n pretty_pretty_print_array(const T +inline pretty_prettyprint::array_wrapper_n pretty_pretty_print_array(const T * const a, size_t n) { -return pretty_pretty_print::array_wrapper_n(a, n); +return pretty_prettyprint::array_wrapper_n(a, n); } -template pretty_pretty_print::bucket_pretty_print_wrapper +template pretty_prettyprint::bucket_pretty_print_wrapper bucket_pretty_print(const T & m, typename T::size_type n) { -return pretty_pretty_print::bucket_pretty_print_wrapper(m, n); +return pretty_prettyprint::bucket_pretty_print_wrapper(m, n); } @@ -479,18 +479,18 @@ return pretty_pretty_print::bucket_pretty_print_wrapper(m, n); */ -namespace soi { -// Pretty_Prints a container to the stream using default delimiters - template -inline typename std::enable_if<::soi::is_container::value, +inline typename std::enable_if<::soi::prettyprint::is_container::value, std::basic_ostream &>::type pretty_print(std::basic_ostream &stream, const T &container) { - return ::soi::print( + return ::soi::prettyprint::print( stream, - ::soi::pretty_print_container_helper(container)); + ::soi::prettyprint::pretty_print_container_helper(container)); } + +}// namespace prettyprint + } // namespace soi #endif // SOI_PRETTY_PRETTY_PRINT diff --git a/include/bits/soi-dbg.hpp b/include/bits/soi-dbg.hpp index 2935f21..13bece5 100644 --- a/include/bits/soi-dbg.hpp +++ b/include/bits/soi-dbg.hpp @@ -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 diff --git a/include/bits/soi-deprecate.hpp b/include/bits/soi-deprecate.hpp index e6676ae..e1c9230 100644 --- a/include/bits/soi-deprecate.hpp +++ b/include/bits/soi-deprecate.hpp @@ -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; diff --git a/include/bits/soi-pretty.hpp b/include/bits/soi-pretty.hpp index 0cd802e..bc304a4 100644 --- a/include/bits/soi-pretty.hpp +++ b/include/bits/soi-pretty.hpp @@ -13,6 +13,8 @@ namespace soi { +namespace prettyprint { + template std::basic_ostream & pretty_print(std::basic_ostream &stream, const bool &x) { @@ -84,7 +86,7 @@ pretty_print(std::basic_ostream &stream, char c) { } template -inline typename std::enable_if::value, +inline typename std::enable_if::value, std::basic_ostream &>::type pretty_print(std::basic_ostream &stream, const T &x) { return stream << x; @@ -93,8 +95,11 @@ pretty_print(std::basic_ostream &stream, const T &x) { template std::basic_ostream & print(std::basic_ostream &stream, const T &x) { - return pretty_print(stream, x); + return ::soi::prettyprint::pretty_print(stream, x); } + +} // namespace prettyprint + } // namespace soi #endif // SOI_PRETTY diff --git a/include/soi b/include/soi index 8e3c092..6a6279f 100644 --- a/include/soi +++ b/include/soi @@ -74,9 +74,11 @@ soi_initializer soi_initializer_{false}; } // end namespace soi -#include "bits/soi-deprecate.hpp" +#include "bits/soi-io.hpp" #include "bits/include-all.hpp" +#include "bits/soi-deprecate.hpp" + #define int int64_t using namespace std;