├── .clang-format ├── example ├── CMakeLists.txt ├── main.cpp └── test.cpp ├── CMakeLists.txt ├── .gitignore ├── .travis.yml ├── appveyor.yml ├── include └── pretty_print │ ├── pretty_print.hpp │ └── internal │ └── detail_pretty_print.hpp ├── README.md └── LICENSE /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | ColumnLimit: '120' 3 | IndentWidth: '4' 4 | TabWidth: '4' 5 | AccessModifierOffset: '-4' 6 | NamespaceIndentation: All 7 | MaxEmptyLinesToKeep: '2' 8 | UseTab: Never 9 | 10 | -------------------------------------------------------------------------------- /example/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(example) 4 | set(CMAKE_CXX_STANDARD 17) 5 | set(SOURCE_FILES main.cpp test.cpp) 6 | 7 | add_executable(${PROJECT_NAME} ${SOURCE_FILES}) 8 | 9 | target_include_directories(${PROJECT_NAME} PUBLIC 10 | "${PROJECT_SOURCE_DIR}/../include" 11 | ) 12 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(pretty_print) 4 | 5 | add_library(${PROJECT_NAME} INTERFACE) 6 | target_include_directories(${PROJECT_NAME} INTERFACE include/) 7 | 8 | install(FILES include/pretty_print/pretty_print.hpp DESTINATION include/pretty_print) 9 | install(FILES include/pretty_print/internal/detail_pretty_print.hpp DESTINATION include/pretty_print/internal) 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # vs 2 | out/ 3 | .vs 4 | 5 | # qtcreator 6 | CMakeLists.txt.user 7 | 8 | # Prerequisites 9 | *.d 10 | 11 | # Compiled Object files 12 | *.slo 13 | *.lo 14 | *.o 15 | *.obj 16 | 17 | # Precompiled Headers 18 | *.gch 19 | *.pch 20 | 21 | # Compiled Dynamic libraries 22 | *.so 23 | *.dylib 24 | *.dll 25 | 26 | # Fortran module files 27 | *.mod 28 | *.smod 29 | 30 | # Compiled Static libraries 31 | *.lai 32 | *.la 33 | *.a 34 | *.lib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | dist: xenial 3 | 4 | # compilers to add to build matrix 5 | 6 | matrix: 7 | include: 8 | - os: linux 9 | addons: 10 | apt: 11 | sources: 12 | - ubuntu-toolchain-r-test 13 | packages: 14 | - g++-9 15 | env: 16 | - MATRIX_EVAL="CC=gcc-9 CXX=g++-9" 17 | - os: linux 18 | addons: 19 | apt: 20 | sources: 21 | - ubuntu-toolchain-r-test 22 | packages: 23 | - g++-8 24 | env: 25 | - MATRIX_EVAL="CC=gcc-8 && CXX=g++-8" 26 | - os: linux 27 | addons: 28 | apt: 29 | sources: 30 | - ubuntu-toolchain-r-test 31 | packages: 32 | - g++-7 33 | env: 34 | - MATRIX_EVAL="CC=gcc-7 && CXX=g++-7" 35 | 36 | before_install: 37 | - eval "${MATRIX_EVAL}" 38 | # scripts to run before build 39 | before_script: 40 | - mkdir build 41 | - cd build 42 | - cmake ../example 43 | 44 | script: 45 | - cmake --build . 46 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: '{build}' 2 | 3 | os: 4 | - Visual Studio 2019 5 | - Visual Studio 2017 6 | 7 | environment: 8 | matrix: 9 | - APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2019" 10 | GENERATOR: "Visual Studio 16 2019" 11 | CONFIG: Debug 12 | 13 | - APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2019" 14 | GENERATOR: "Visual Studio 16 2019" 15 | CONFIG: Release 16 | 17 | - APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2017" 18 | GENERATOR: "Visual Studio 15 2017" 19 | CONFIG: Debug 20 | 21 | - APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2017" 22 | GENERATOR: "Visual Studio 15 2017" 23 | CONFIG: Release 24 | matrix: 25 | exclude: 26 | - os: Visual Studio 2019 27 | APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2017" 28 | - os: Visual Studio 2017 29 | APPVEYOR_BUILD_WORKER_IMAGE: "Visual Studio 2019" 30 | 31 | build_script: 32 | - ps: cd example 33 | - cmake "-G%GENERATOR%" -H. -B_builds -DENABLE_TESTING=1 34 | - cmake --build _builds --config "%CONFIG%" 35 | -------------------------------------------------------------------------------- /include/pretty_print/pretty_print.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include // std::stringstream 3 | #include // std::string 4 | #include // std::is_same_v 5 | #include // std::type_info::name 6 | 7 | #include "internal/detail_pretty_print.hpp" 8 | 9 | namespace pretty { 10 | 11 | /** pretty data print 12 | * @param out Stream 13 | * @param data data 14 | * @return Stream */ 15 | template 16 | constexpr Stream& print(Stream& out, const T& data) { 17 | detail::ostream::ostream_impl<0>(out, data); 18 | return out; 19 | } 20 | 21 | /** pretty data print 22 | * @param data data 23 | * @return std::string */ 24 | template 25 | std::string print(const T& data) { 26 | std::stringstream out; 27 | print(out, data); 28 | return out.str(); 29 | } 30 | 31 | /** pretty data print with type inforamation at the beginning 32 | * @param out Stream 33 | * @param data data 34 | * @return Stream */ 35 | template 36 | constexpr Stream& print_ti(Stream& out, const T& data) { 37 | out << typeid(T).name() << "@"; 38 | print(out, data); 39 | return out; 40 | } 41 | 42 | /** pretty data print with type inforamation at the beginning 43 | * @param data data 44 | * @return std::string */ 45 | template 46 | std::string print_ti(const T& data) { 47 | return std::string(typeid(T).name()).append("@").append(print(data)); 48 | } 49 | 50 | /** pretty data print 51 | * @param out Stream 52 | * @param args variadic data 53 | * @return Stream */ 54 | template 1)>> 55 | constexpr Stream& print_args(Stream& out, Args&&... args) { 56 | ((print(out, std::forward(args)) << ' '), ...); 57 | return out; 58 | } 59 | 60 | /** pretty data print 61 | * @param args variadic data 62 | * @return std::string */ 63 | template 1)>> 64 | std::string print_args(Args&&... args) { 65 | std::string result; 66 | ((result.append(print(std::forward(args))).append(" ")), ...); 67 | return result; 68 | } 69 | 70 | /** pretty data print line 71 | * @param out Stream 72 | * @param args variadic data 73 | * @return Stream */ 74 | template 1)>> 75 | void print_line(Stream& out, Args&&... args) { 76 | ((print(out, std::forward(args)) << ' '), ...); 77 | out << '\n'; 78 | } 79 | 80 | } // namespace pretty 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/ciberst/pretty_print.svg?branch=master)](https://travis-ci.org/ciberst/pretty_print) 2 | [![Build status](https://ci.appveyor.com/api/projects/status/ognl9dahv7vkhwnf?svg=true)](https://ci.appveyor.com/project/ciberst/pretty-print) 3 | # pretty_print 4 | pretty_print is a cross-platform library for a pretty print of various data. 5 | 6 | pretty_print - это кроссплатформенная библиотека для красивой печати различных данных. 7 | Вам не нужно больше писать циклы, чтоб распечатать тот или иной std-контейнер, массив данных. Вы с помощью данной библиотеки можете распечатать std::tuple, std::variant. Смешанные типы тоже поддерживаются! 8 | 9 | ## Example 10 | 11 | ```cpp 12 | #include 13 | #include 14 | #include 15 | int main() { 16 | std::vector a = {1, 2, 3, 4}; 17 | pretty::print(std::cout, a) << std::endl; 18 | return 0; 19 | } 20 | ``` 21 | 22 | ## Output 23 | 24 | ``` 25 | [1, 2, 3, 4] 26 | ``` 27 | 28 | ## Требования к компилятору 29 | * Поддержка C++17 30 | 31 | 32 | ## Обзор основных возможностей библиотеки 33 | 34 | ### std::vector 35 | ```cpp 36 | std::vector a = {1, 2, 3, 4}; 37 | pretty::print(std::cout, a) << std::endl; 38 | ``` 39 | Output 40 | ``` 41 | [1, 2, 3, 4] 42 | ``` 43 | ### std::map 44 | 45 | ```cpp 46 | std::map map = {{1, 2}, {2, 3}, {3, 4}}; 47 | pretty::print(std::cout, map) << std::endl; 48 | ``` 49 | Output 50 | ``` 51 | {1: 2, 2: 3, 3: 4} 52 | ``` 53 | ### std::pair 54 | 55 | ```cpp 56 | using namespace std::string_literals; 57 | auto pair = std::make_pair("123"s, 12); 58 | pretty::print(std::cout, pair) << std::endl; 59 | ``` 60 | Output 61 | ``` 62 | "123": 12 63 | ``` 64 | 65 | ### std::optional 66 | ```cpp 67 | std::optional opt{"string"s}; 68 | pretty::print(std::cout, opt) << std::endl; 69 | ``` 70 | Output 71 | ``` 72 | "string" 73 | ``` 74 | 75 | ### std::variant 76 | ```cpp 77 | std::variant variant; 78 | variant = "123"; 79 | pretty::print(std::cout, variant) << std::endl; 80 | ``` 81 | Output 82 | ``` 83 | "123" 84 | ``` 85 | 86 | ### std::tuple 87 | ```cpp 88 | auto tuple = std::make_tuple("1", 2, 3, 4.5); 89 | pretty::print(std::cout, tuple) << std::endl; 90 | ``` 91 | Output 92 | ``` 93 | ("1", 2, 3, 4.5) 94 | ``` 95 | ### C-array and C-string 96 | ```cpp 97 | int c_arr[] = {1, 2, 3, 4, 5, 6}; 98 | int data2[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; 99 | pretty::print(std::cout, c_arr) << std::endl; 100 | pretty::print(std::cout, "hello") << std::endl; 101 | pretty::print(std::cout, data2) << std::endl; 102 | ``` 103 | Output 104 | ``` 105 | [1, 2, 3, 4, 5, 6] 106 | "hello" 107 | [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] 108 | ``` 109 | 110 | ### Enum 111 | 112 | ```cpp 113 | enum class color { red, green, blue }; 114 | color data = color::green; 115 | pretty::print(std::cout, data) << std::endl; 116 | ``` 117 | Output 118 | ``` 119 | 1 120 | ``` 121 | 122 | ### hardcore example :-) 123 | ```cpp 124 | using namespace std::string_literals; 125 | std::unordered_map>> mapmap = { 126 | {"test"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 127 | {"hello"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 128 | {"world"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, {}}}}}; 129 | pretty::print(std::cout, mapmap) << std::endl; 130 | ``` 131 | Output 132 | ``` 133 | {"test": {"1": 2, "2": 3, "3": 4}, "world": {"1": 2, "2": 3, "3": null}, "hello": {"1": 2, "2": 3, "3": 4}} 134 | ``` 135 | -------------------------------------------------------------------------------- /example/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #if __has_include() 3 | #include 4 | #endif 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | struct user_data { 16 | int a = 42; 17 | std::string str = "hello"; 18 | }; 19 | 20 | enum class size { large, small, medium }; 21 | 22 | 23 | std::ostream& operator<<(std::ostream& out, size val) { 24 | switch (val) { 25 | case size::large: 26 | out << "large"; 27 | break; 28 | case size::medium: 29 | out << "medium"; 30 | break; 31 | case size::small: 32 | out << "small"; 33 | break; 34 | } 35 | return out; 36 | } 37 | 38 | 39 | template 40 | T& operator,(T& val, const user_data& empty) { 41 | (void)empty; 42 | return val; 43 | } 44 | template 45 | const T& operator,(const T& val, const user_data& empty) { 46 | (void)empty; 47 | return val; 48 | } 49 | 50 | std::ostream& operator<<(std::ostream& out, const user_data& data) { 51 | out << data.str << ", " << data.a; 52 | return out; 53 | } 54 | 55 | void print_vector() { 56 | std::vector data = {1, 2, 3, 4}; 57 | pretty::print(std::cout, data) << std::endl; 58 | } 59 | 60 | void print_map() { 61 | std::map data = {{1, 2}, {2, 3}, {3, 4}}; 62 | pretty::print(std::cout, data) << std::endl; 63 | } 64 | 65 | void print_pair() { 66 | using namespace std::string_literals; 67 | auto data = std::make_pair("123"s, 12); 68 | pretty::print(std::cout, data) << std::endl; 69 | } 70 | 71 | void print_optional() { 72 | using namespace std::string_literals; 73 | std::optional data{"string"s}; 74 | pretty::print(std::cout, data) << std::endl; 75 | } 76 | 77 | void print_variant() { 78 | std::variant data; 79 | data = "123"; 80 | pretty::print(std::cout, data) << std::endl; 81 | } 82 | 83 | void print_tuple() { 84 | auto data = std::make_tuple("1", 2, 3, 4.5); 85 | pretty::print(std::cout, data) << std::endl; 86 | } 87 | 88 | void print_c_array() { 89 | int data[] = {1, 2, 3, 4, 5, 6}; 90 | int data2[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; 91 | pretty::print(std::cout, data) << std::endl; 92 | pretty::print(std::cout, "hello") << std::endl; 93 | pretty::print(std::cout, data2) << std::endl; 94 | } 95 | 96 | void print_hardcore() { 97 | using namespace std::string_literals; 98 | std::unordered_map>> data = { 99 | {"test"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 100 | {"hello"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 101 | {"world"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, {}}}}}; 102 | pretty::print(std::cout, data) << std::endl; 103 | } 104 | 105 | void print_user_data() { 106 | user_data data; 107 | pretty::print(std::cout, data) << std::endl; 108 | } 109 | 110 | #if __has_include() 111 | void print_filesystem_path() { 112 | std::filesystem::path data{"/home/user/data"}; 113 | pretty::print(std::cout, data) << std::endl; 114 | } 115 | #endif 116 | 117 | void print_enum() { 118 | enum class color { red, green, blue }; 119 | color data = color::green; 120 | pretty::print(std::cout, data) << std::endl; 121 | 122 | size data2 = size::large; 123 | pretty::print(std::cout, data2) << std::endl; 124 | } 125 | 126 | 127 | extern void run_test(); 128 | 129 | int main() { 130 | print_vector(); 131 | print_map(); 132 | print_pair(); 133 | print_optional(); 134 | print_variant(); 135 | print_tuple(); 136 | print_c_array(); 137 | print_hardcore(); 138 | print_user_data(); 139 | #if __has_include() 140 | print_filesystem_path(); 141 | #endif 142 | print_enum(); 143 | run_test(); 144 | return 0; 145 | } 146 | -------------------------------------------------------------------------------- /example/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #if __has_include() 3 | #include 4 | #endif 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | using namespace std::string_literals; 13 | 14 | template 15 | void is_equal_assert(const T& lhs, const T& rhs, int line) { 16 | if (lhs != rhs) { 17 | std::cerr << "===" << std::endl 18 | << "ASSERT! Line: " << line << std::endl 19 | << "Expect: " << lhs << std::endl 20 | << "Value : " << rhs << std::endl; 21 | assert(false); 22 | } 23 | } 24 | #define ASSERT_EQUAL(x, y) is_equal_assert(x, y, __LINE__) 25 | namespace pretty_test { 26 | 27 | struct user_data { 28 | int a = 42; 29 | std::string str = "hello"; 30 | }; 31 | 32 | enum class size { large, small, medium }; 33 | 34 | 35 | std::ostream& operator<<(std::ostream& out, size val) { 36 | switch (val) { 37 | case size::large: 38 | out << "large"; 39 | break; 40 | case size::medium: 41 | out << "medium"; 42 | break; 43 | case size::small: 44 | out << "small"; 45 | break; 46 | } 47 | return out; 48 | } 49 | 50 | 51 | template 52 | T& operator,(T& val, const user_data& empty) { 53 | (void)empty; 54 | return val; 55 | } 56 | template 57 | const T& operator,(const T& val, const user_data& empty) { 58 | (void)empty; 59 | return val; 60 | } 61 | 62 | std::ostream& operator<<(std::ostream& out, const user_data& data) { 63 | out << data.str << ", " << data.a; 64 | return out; 65 | } 66 | 67 | void test_vector() { 68 | std::vector data = {1, 2, 3, 4}; 69 | std::stringstream ss; 70 | pretty::print(ss, data); 71 | ASSERT_EQUAL("[1, 2, 3, 4]"s, ss.str()); 72 | ASSERT_EQUAL("[1, 2, 3, 4]"s, pretty::print(data)); 73 | } 74 | 75 | void test_map() { 76 | std::map data = {{1, 2}, {2, 3}, {3, 4}}; 77 | std::stringstream ss; 78 | pretty::print(ss, data); 79 | ASSERT_EQUAL("{1: 2, 2: 3, 3: 4}"s, ss.str()); 80 | ASSERT_EQUAL("{1: 2, 2: 3, 3: 4}"s, pretty::print(data)); 81 | } 82 | 83 | void test_pair() { 84 | auto data = std::make_pair("123"s, 12); 85 | std::stringstream ss; 86 | pretty::print(ss, data); 87 | ASSERT_EQUAL(R"("123": 12)"s, ss.str()); 88 | ASSERT_EQUAL(R"("123": 12)"s, pretty::print(data)); 89 | } 90 | 91 | void test_optional() { 92 | std::optional data{"string"s}; 93 | std::stringstream ss; 94 | pretty::print(ss, data); 95 | ASSERT_EQUAL(R"("string")"s, ss.str()); 96 | ASSERT_EQUAL(R"("string")"s, pretty::print(data)); 97 | } 98 | 99 | void test_variant() { 100 | std::variant data; 101 | data = "123"; 102 | std::stringstream ss; 103 | pretty::print(ss, data); 104 | ASSERT_EQUAL(R"("123")"s, ss.str()); 105 | ASSERT_EQUAL(R"("123")"s, pretty::print(data)); 106 | } 107 | 108 | void test_tuple() { 109 | auto data = std::make_tuple("1", 2, 3, 4.5); 110 | std::stringstream ss; 111 | 112 | pretty::print(ss, data); 113 | ASSERT_EQUAL(R"data(("1", 2, 3, 4.5))data"s, ss.str()); 114 | ASSERT_EQUAL(R"data(("1", 2, 3, 4.5))data"s, pretty::print(data)); 115 | } 116 | 117 | void test_c_array() { 118 | int data[] = {1, 2, 3, 4, 5, 6}; 119 | int data2[2][2][2] = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}; 120 | std::stringstream ss; 121 | 122 | pretty::print(ss, data); 123 | ASSERT_EQUAL(R"([1, 2, 3, 4, 5, 6])"s, ss.str()); 124 | ASSERT_EQUAL(R"([1, 2, 3, 4, 5, 6])"s, pretty::print(data)); 125 | ss = std::stringstream(); 126 | pretty::print(ss, "hello"); 127 | ASSERT_EQUAL(R"("hello")"s, ss.str()); 128 | ASSERT_EQUAL(R"("hello")"s, pretty::print("hello")); 129 | ss = std::stringstream(); 130 | pretty::print(ss, data2); 131 | ASSERT_EQUAL(R"([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])"s, ss.str()); 132 | ASSERT_EQUAL(R"([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])"s, pretty::print(data2)); 133 | } 134 | 135 | void test_hardcore() { 136 | std::map>> data = { 137 | {"test"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 138 | {"hello"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, 4}}}, 139 | {"world"s, {{"1"s, 2}, {"2"s, 3}, {"3"s, {}}}}}; 140 | std::stringstream ss; 141 | 142 | pretty::print(ss, data); 143 | ASSERT_EQUAL( 144 | R"({"hello": {"1": 2, "2": 3, "3": 4}, "test": {"1": 2, "2": 3, "3": 4}, "world": {"1": 2, "2": 3, "3": null}})"s, 145 | ss.str()); 146 | ASSERT_EQUAL( 147 | R"({"hello": {"1": 2, "2": 3, "3": 4}, "test": {"1": 2, "2": 3, "3": 4}, "world": {"1": 2, "2": 3, "3": null}})"s, 148 | pretty::print(data)); 149 | } 150 | 151 | void test_user_data() { 152 | user_data data; 153 | std::stringstream ss; 154 | 155 | pretty::print(ss, data); 156 | ASSERT_EQUAL(R"(hello, 42)"s, ss.str()); 157 | ASSERT_EQUAL(R"(hello, 42)"s, pretty::print(data)); 158 | } 159 | 160 | #if __has_include() 161 | void test_filesystem_path() { 162 | std::filesystem::path data{"/home/user/data"}; 163 | std::stringstream ss; 164 | 165 | pretty::print(ss, data); 166 | ASSERT_EQUAL(R"("/home/user/data")"s, ss.str()); 167 | ASSERT_EQUAL(R"("/home/user/data")"s, pretty::print(data)); 168 | } 169 | #endif 170 | void test_enum() { 171 | enum class color { red, green, blue }; 172 | color data = color::green; 173 | std::stringstream ss; 174 | 175 | pretty::print(ss, data); 176 | ASSERT_EQUAL(R"(1)"s, ss.str()); 177 | ASSERT_EQUAL(R"(1)"s, pretty::print(data)); 178 | ss = std::stringstream(); 179 | size data2 = size::large; 180 | pretty::print(ss, data2); 181 | ASSERT_EQUAL(R"(large)"s, ss.str()); 182 | ASSERT_EQUAL(R"(large)"s, pretty::print(data2)); 183 | } 184 | } // namespace pretty_test 185 | 186 | 187 | void run_test() { 188 | using namespace pretty_test; 189 | test_vector(); 190 | test_map(); 191 | test_pair(); 192 | test_optional(); 193 | test_variant(); 194 | test_tuple(); 195 | test_c_array(); 196 | test_hardcore(); 197 | test_user_data(); 198 | #if __has_include() 199 | test_filesystem_path(); 200 | #endif 201 | test_enum(); 202 | } 203 | -------------------------------------------------------------------------------- /include/pretty_print/internal/detail_pretty_print.hpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include // std::size_t 3 | #include // std::quoted 4 | #include // std::string 5 | #include // std::tuple 6 | #include // std::declval, std::void_t, 7 | // std::false_type, std::true_type 8 | #include // std::pair, std::tuple 9 | // std::forward 10 | 11 | #if __has_include() 12 | #include // std::variant 13 | #endif 14 | #if __has_include() 15 | #include // std::optional 16 | #endif 17 | 18 | namespace pretty::detail { 19 | 20 | template 21 | struct is_iterable : std::false_type {}; 22 | template 23 | struct is_iterable().begin()), decltype(std::declval().end())>> 24 | : std::true_type {}; 25 | 26 | template 27 | inline constexpr bool is_iterable_v = is_iterable::value || std::is_array_v; 28 | 29 | template 30 | struct has_ostream_operator : std::false_type {}; 31 | template 32 | struct has_ostream_operator() << std::declval())>> 33 | : std::true_type {}; 34 | 35 | template 36 | inline constexpr bool has_ostream_operator_v = has_ostream_operator::value; 37 | 38 | template 39 | struct is_same_any_of { 40 | private: 41 | constexpr static bool check() { return (std::is_same_v || (std::is_same_v || ...)); } 42 | 43 | public: 44 | static constexpr bool value = check(); 45 | }; 46 | template 47 | inline constexpr bool is_same_any_of_v = is_same_any_of::value; 48 | 49 | 50 | template 51 | inline constexpr bool is_char_type_v = 52 | is_same_any_of_v; 53 | 54 | template 55 | struct is_c_string : std::false_type {}; 56 | template 57 | struct is_c_string : std::conditional_t, std::true_type, std::false_type> {}; 58 | template 59 | struct is_c_string : std::conditional_t, std::true_type, std::false_type> {}; 60 | 61 | template 62 | inline constexpr bool is_c_string_v = is_c_string::value; 63 | 64 | template 65 | struct is_map : std::false_type {}; 66 | template 67 | struct is_map()[std::declval()])>> 69 | : std::true_type {}; 70 | template 71 | inline constexpr bool is_map_v = is_map::value; 72 | 73 | template >> 74 | auto quoted_helper(const T (&s)[N]) noexcept { 75 | return std::quoted(s); 76 | } 77 | 78 | inline auto quoted_helper(const char* c) noexcept { return std::quoted(c); } 79 | 80 | inline auto quoted_helper(const std::string& s) noexcept { return std::quoted(s); } 81 | 82 | inline auto quoted_helper(std::string& s) noexcept { return std::quoted(s); } 83 | 84 | template 85 | auto quoted_helper(std::basic_string_view s) noexcept { 86 | return std::quoted(s); 87 | } 88 | 89 | template 90 | decltype(auto) quoted_helper(T&& v) noexcept { 91 | return std::forward(v); 92 | } 93 | 94 | struct ostream { // struct ostream 95 | template 96 | static Stream& ostream_impl(Stream& out, const T& data); 97 | template >>> 99 | static Stream& ostream_impl(Stream& out, const std::pair& data); 100 | template >>> 102 | static Stream& ostream_impl(Stream& out, const std::tuple& data); 103 | #if __has_include() 104 | template >>> 106 | static Stream& ostream_impl(Stream& out, const std::optional& data); 107 | #endif 108 | #if __has_include() 109 | template 110 | static Stream& ostream_impl(Stream& out, const std::variant& data); 111 | #endif 112 | }; // struct ostream 113 | 114 | template 115 | void append(Stream& out, T&& data) { 116 | out << std::forward(data); 117 | } 118 | 119 | template 120 | void print_tuple_impl(Stream& out, const Tuple& value, std::index_sequence) { 121 | ((void)(append(out, (Is == 0 ? "" : ", ")), (void)ostream::ostream_impl(out, std::get(value))), 122 | ...); 123 | } 124 | 125 | template 126 | Stream& ostream::ostream_impl(Stream& out, const T& data) { 127 | if constexpr (detail::is_iterable_v && !detail::is_c_string_v && 128 | ((!detail::has_ostream_operator_v) || std::is_array_v)) { 129 | std::string delimiter; 130 | if constexpr (is_map_v) { 131 | append(out, '{'); 132 | } else { 133 | append(out, '['); 134 | } 135 | 136 | for (const auto& el : data) { 137 | append(out, delimiter); 138 | ostream_impl(out, detail::quoted_helper(el)); 139 | delimiter = ", "; 140 | } 141 | 142 | if constexpr (is_map_v) { 143 | append(out, '}'); 144 | } else { 145 | append(out, ']'); 146 | } 147 | } else if constexpr (detail::has_ostream_operator_v) { 148 | append(out, detail::quoted_helper(data)); 149 | } else if constexpr (std::is_enum_v) { 150 | append(out, static_cast>(data)); 151 | } else { 152 | static_assert(detail::has_ostream_operator_v && !std::is_enum_v, 153 | "not support [ostream& operator<<(ostream& out, const T& data)]"); 154 | } 155 | 156 | return out; 157 | } 158 | 159 | template 160 | Stream& ostream::ostream_impl(Stream& out, const std::pair& data) { 161 | if constexpr (detail::has_ostream_operator_v>) { 162 | append(out, data); 163 | } else { 164 | ///*if (!!Nested) */ out << '{'; 165 | ostream_impl(out, detail::quoted_helper(data.first)); 166 | append(out, ": "); 167 | ostream_impl(out, detail::quoted_helper(data.second)); 168 | ///*if (!!Nested)*/ out << '}'; 169 | } 170 | return out; 171 | } 172 | 173 | template 174 | Stream& ostream::ostream_impl(Stream& out, const std::tuple& data) { 175 | append(out, "("); 176 | detail::print_tuple_impl(out, data, std::index_sequence_for{}); 177 | append(out, ")"); 178 | return out; 179 | } 180 | 181 | #if __has_include() 182 | template 183 | Stream& ostream::ostream_impl(Stream& out, const std::optional& data) { 184 | if (data) { 185 | ostream_impl(out, detail::quoted_helper(data.value())); 186 | } else { 187 | append(out, "null"); 188 | } 189 | return out; 190 | } 191 | #endif 192 | 193 | #if __has_include() 194 | template 195 | Stream& ostream::ostream_impl(Stream& out, const std::variant& data) { 196 | if (data.index() != std::variant_npos) { 197 | std::visit([&out](const auto& t) { ostream_impl(out, t); }, data); 198 | return out; 199 | } 200 | append(out, "VARIANT_NPOS"); 201 | return out; 202 | } 203 | #endif 204 | 205 | 206 | /// static assert test 207 | static_assert(is_same_any_of_v, "test failed"); 208 | 209 | #if __has_include() 210 | static_assert(!is_iterable_v>, "test failed"); 211 | #endif 212 | static_assert(is_iterable_v, "test failed"); 213 | 214 | static_assert(!has_ostream_operator_v>, "test failed"); 215 | static_assert(has_ostream_operator_v, "test failed"); 216 | 217 | static_assert(is_char_type_v, "test failed"); 218 | static_assert(is_char_type_v, "test failed"); 219 | static_assert(is_char_type_v, "test failed"); 220 | static_assert(is_char_type_v, "test failed"); 221 | static_assert(is_char_type_v, "test failed"); 222 | static_assert(is_char_type_v, "test failed"); 223 | static_assert(is_char_type_v, "test failed"); 224 | static_assert(!is_char_type_v, "test failed"); 225 | static_assert(!is_char_type_v, "test failed"); 226 | 227 | } // namespace pretty::detail 228 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------