|
1 | 1 | #pragma once
|
2 | 2 |
|
| 3 | +#include <array> |
| 4 | +#include <deque> |
| 5 | +#include <forward_list> |
| 6 | +#include <initializer_list> |
| 7 | +#include <list> |
| 8 | +#include <map> |
| 9 | +#include <set> |
3 | 10 | #include <string>
|
4 | 11 | #include <sstream>
|
| 12 | +#include <unordered_map> |
| 13 | +#include <unordered_set> |
| 14 | +#include <utility> |
5 | 15 | #include <vector>
|
6 | 16 |
|
7 | 17 | namespace cil {
|
8 | 18 |
|
| 19 | +// ============================================================================= |
| 20 | +// declaration |
| 21 | +// ----------------------------------------------------------------------------- |
| 22 | + |
| 23 | +template <typename T> |
| 24 | +std::string tostr(const T& t); |
| 25 | + |
| 26 | +template <typename T, typename U> |
| 27 | +std::string tostr(const std::pair<T, U>& t); |
| 28 | + |
| 29 | +template <typename T, size_t N> |
| 30 | +std::string tostr(const std::array<T, N>& t); |
| 31 | + |
| 32 | +template <typename T> |
| 33 | +std::string tostr(const std::deque<T>& t); |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +std::string tostr(const std::forward_list<T>& t); |
| 37 | + |
| 38 | +template <typename T> |
| 39 | +std::string tostr(const std::initializer_list<T>& t); |
| 40 | + |
| 41 | +template <typename T> |
| 42 | +std::string tostr(const std::list<T>& t); |
| 43 | + |
| 44 | +template <typename T, typename U> |
| 45 | +std::string tostr(const std::map<T, U>& t); |
| 46 | + |
| 47 | +template <typename T> |
| 48 | +std::string tostr(const std::set<T>& t); |
| 49 | + |
| 50 | +template <typename T, typename U> |
| 51 | +std::string tostr(const std::unordered_map<T, U>& t); |
| 52 | + |
| 53 | +template <typename T> |
| 54 | +std::string tostr(const std::unordered_set<T>& t); |
| 55 | + |
| 56 | +template <typename T> |
| 57 | +std::string tostr(const std::vector<T>& t); |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | +template <typename T, typename ...Args> |
| 62 | +void push_to_str_vec(std::vector<std::string>& strs, const T& t, const Args&... args); |
| 63 | + |
| 64 | +template <typename T, typename ...Args> |
| 65 | +std::vector<std::string> tostrs(const T& t, const Args&... args); |
| 66 | + |
| 67 | +template <typename ...Args> |
| 68 | +std::string tostr_with_delim(const std::string& delim, const Args&... args); |
| 69 | + |
| 70 | +template <typename ...Args> |
| 71 | +std::string tostr(const Args&... args); |
| 72 | + |
| 73 | + |
| 74 | +// ============================================================================= |
| 75 | +// implementations |
| 76 | +// ----------------------------------------------------------------------------- |
| 77 | + |
| 78 | +template <typename ForwardIterator> |
| 79 | +std::string __item_single_container_to_str( |
| 80 | + ForwardIterator b, ForwardIterator e, |
| 81 | + const std::string& l, const std::string& r, const std::string& d) { |
| 82 | + std::string ret{l}; |
| 83 | + ForwardIterator it = b; |
| 84 | + while (it != e) { |
| 85 | + if (it != b) ret += d; |
| 86 | + ret += tostr(*it); |
| 87 | + ++it; |
| 88 | + } |
| 89 | + ret += r; |
| 90 | + return ret; |
| 91 | +} |
| 92 | + |
| 93 | +template <typename ForwardIterator> |
| 94 | +std::string __item_pair_container_to_str( |
| 95 | + ForwardIterator b, ForwardIterator e, |
| 96 | + const std::string& l, const std::string& r, const std::string& d) { |
| 97 | + std::string ret{l}; |
| 98 | + ForwardIterator it = b; |
| 99 | + while (it != e) { |
| 100 | + if (it != b) ret += d; |
| 101 | + ret += tostr(it->first) + ": " + tostr(it->second); |
| 102 | + ++it; |
| 103 | + } |
| 104 | + ret += r; |
| 105 | + return ret; |
| 106 | +} |
| 107 | + |
9 | 108 | template <typename T>
|
10 | 109 | std::string tostr(const T& t) {
|
11 | 110 | std::ostringstream os;
|
12 | 111 | os << t;
|
13 | 112 | return os.str();
|
14 | 113 | }
|
15 | 114 |
|
| 115 | +template <typename T, typename U> |
| 116 | +std::string tostr(const std::pair<T, U>& t) { |
| 117 | + return "(" + tostr(t.first) + ", " + tostr(t.second) + ")"; |
| 118 | +}; |
| 119 | + |
| 120 | +template <typename T, size_t N> |
| 121 | +std::string tostr(const std::array<T, N>& t) { |
| 122 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 123 | +}; |
| 124 | + |
| 125 | +template <typename T> |
| 126 | +std::string tostr(const std::deque<T>& t) { |
| 127 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 128 | +}; |
| 129 | + |
| 130 | +template <typename T> |
| 131 | +std::string tostr(const std::forward_list<T>& t) { |
| 132 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 133 | +}; |
| 134 | + |
| 135 | +template <typename T> |
| 136 | +std::string tostr(const std::initializer_list<T>& t) { |
| 137 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 138 | +}; |
| 139 | + |
| 140 | +template <typename T> |
| 141 | +std::string tostr(const std::list<T>& t) { |
| 142 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 143 | +}; |
| 144 | + |
| 145 | +template <typename T, typename U> |
| 146 | +std::string tostr(const std::map<T, U>& t) { |
| 147 | + return __item_pair_container_to_str(t.begin(), t.end(), "{", "}", ", "); |
| 148 | +} |
| 149 | + |
| 150 | +template <typename T> |
| 151 | +std::string tostr(const std::set<T>& t) { |
| 152 | + return __item_single_container_to_str(t.begin(), t.end(), "{", "}", ", "); |
| 153 | +} |
| 154 | + |
| 155 | +template <typename T, typename U> |
| 156 | +std::string tostr(const std::unordered_map<T, U>& t) { |
| 157 | + return __item_pair_container_to_str(t.begin(), t.end(), "{", "}", ", "); |
| 158 | +} |
| 159 | + |
| 160 | +template <typename T> |
| 161 | +std::string tostr(const std::unordered_set<T>& t) { |
| 162 | + return __item_single_container_to_str(t.begin(), t.end(), "{", "}", ", "); |
| 163 | +} |
| 164 | + |
| 165 | +template <typename T> |
| 166 | +std::string tostr(const std::vector<T>& t) { |
| 167 | + return __item_single_container_to_str(t.begin(), t.end(), "[", "]", ", "); |
| 168 | +} |
| 169 | + |
| 170 | + |
16 | 171 | template <typename T>
|
17 | 172 | void push_to_str_vec(std::vector<std::string>& strs, const T& t) {
|
18 | 173 | strs.push_back(tostr(t));
|
@@ -44,7 +199,8 @@ std::string tostr_with_delim(const std::string& delim, const Args&... args) {
|
44 | 199 |
|
45 | 200 | template <typename ...Args>
|
46 | 201 | std::string tostr(const Args&... args) {
|
47 |
| - return tostr_with_delim(" ", args...); |
| 202 | + return tostr_with_delim(", ", args...); |
48 | 203 | };
|
49 | 204 |
|
| 205 | + |
50 | 206 | } // namespace cil
|
0 commit comments