| 1 | /** |
|---|
| 2 | * debugprint.hpp, indented_debugprint.hppの共通部分 |
|---|
| 3 | * repr: いろんなオブジェクトを受け取って文字列化する関数 |
|---|
| 4 | * |
|---|
| 5 | */ |
|---|
| 6 | #ifndef NISHIO_DEBUGPRINT_COMMON_HPP |
|---|
| 7 | #define NISHIO_DEBUGPRINT_COMMON_HPP |
|---|
| 8 | |
|---|
| 9 | #include <sstream> |
|---|
| 10 | #include <utility> |
|---|
| 11 | #include <vector> |
|---|
| 12 | #include <map> |
|---|
| 13 | #include <algorithm> |
|---|
| 14 | |
|---|
| 15 | namespace debugprint{ |
|---|
| 16 | namespace common{ |
|---|
| 17 | |
|---|
| 18 | // make string representation |
|---|
| 19 | template<class T> |
|---|
| 20 | std::string repr(const T& value) { |
|---|
| 21 | std::ostringstream result; |
|---|
| 22 | result << value; |
|---|
| 23 | return result.str(); |
|---|
| 24 | } |
|---|
| 25 | ; |
|---|
| 26 | |
|---|
| 27 | // repr pair |
|---|
| 28 | template<class K, class V> |
|---|
| 29 | std::string repr(const std::pair<K, V>& x) { |
|---|
| 30 | std::ostringstream result; |
|---|
| 31 | result << "(" << repr(x.first) << ", " << repr(x.second) << ")"; |
|---|
| 32 | return result.str(); |
|---|
| 33 | } |
|---|
| 34 | ; |
|---|
| 35 | |
|---|
| 36 | // repr vector |
|---|
| 37 | template<class V> |
|---|
| 38 | std::string repr(const std::vector<V>& v) { |
|---|
| 39 | std::ostringstream result; |
|---|
| 40 | if (v.empty()) { |
|---|
| 41 | return "{}"; |
|---|
| 42 | } |
|---|
| 43 | result << "{" << repr(*(v.begin())); |
|---|
| 44 | typedef typename std::vector<V>::const_iterator Iter; |
|---|
| 45 | for (Iter it = ++v.begin(), end = v.end(); it != end; ++it) { |
|---|
| 46 | result << ", " << repr(*it); |
|---|
| 47 | } |
|---|
| 48 | result << "}"; |
|---|
| 49 | return result.str(); |
|---|
| 50 | } |
|---|
| 51 | ; |
|---|
| 52 | |
|---|
| 53 | template<typename K, typename V> |
|---|
| 54 | std::string repr(const std::map<K, V> m) { |
|---|
| 55 | std::ostringstream result; |
|---|
| 56 | if(m.empty()){ |
|---|
| 57 | return "{}"; |
|---|
| 58 | } |
|---|
| 59 | typedef typename std::map<K, V>::const_iterator Iter; |
|---|
| 60 | Iter it = m.begin(); |
|---|
| 61 | result << "{" << repr(it->first) << ": " << repr(it->second); |
|---|
| 62 | ++it; |
|---|
| 63 | for (Iter end = m.end(); it != end; ++it) { |
|---|
| 64 | if (it != m.begin()) |
|---|
| 65 | result << ", " << repr(it->first) << ": " << repr(it->second); |
|---|
| 66 | } |
|---|
| 67 | result << "}"; |
|---|
| 68 | return result.str(); |
|---|
| 69 | } |
|---|
| 70 | ; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | #endif |
|---|