 Timestamp:
 09/29/08 21:28:49 (7 years ago)
 Location:
 lang/cplusplus/debugprint
 Files:

 2 modified
Legend:
 Unmodified
 Added
 Removed

lang/cplusplus/debugprint/debugprint.hpp
r20209 r20221 1 #ifndef NISHIO_DEBUGPRINT_H 2 #define NISHIO_DEBUGPRINT_H 1 #ifndef NISHIO_DEBUGPRINT_HPP 2 #define NISHIO_DEBUGPRINT_HPP 3 3 4 4 #include <iostream> 5 #include <sstream> 6 #include <utility> 7 #include <vector> 8 #include <map> 9 #include <algorithm> 5 #include "debugprint_common.hpp" 10 6 11 7 namespace debugprint{ 12 13 // make string representation 14 template<class T> 15 std::string repr(const T& value) { 16 std::ostringstream result; 17 result << value; 18 return result.str(); 19 } 20 ; 21 22 // repr pair 23 template<class K, class V> 24 std::string repr(const std::pair<K, V>& x) { 25 std::ostringstream result; 26 result << "(" << x.first << ", " << x.second << ")"; 27 return result.str(); 28 } 29 ; 30 31 // repr vector 32 template<class V> 33 std::string repr(const std::vector<V>& v) { 34 std::ostringstream result; 35 if (v.empty()) { 36 return "{}"; 37 } 38 result << "{" << *(v.begin()); 39 typedef typename std::vector<V>::const_iterator Iter; 40 for (Iter it = ++v.begin(), end = v.end(); it != end; ++it) { 41 result << ", " << *it; 42 } 43 result << "}"; 44 return result.str(); 45 } 46 ; 47 48 template<typename K, typename V> 49 std::string repr(const std::map<K, V> m) { 50 std::ostringstream result; 51 if(m.empty()){ 52 return "{}"; 53 } 54 typedef typename std::map<K, V>::const_iterator Iter; 55 Iter it = m.begin(); 56 result << "{" << it>first << ": " << it>second; 57 ++it; 58 for (Iter end = m.end(); it != end; ++it) { 59 if (it != m.begin()) 60 result << ", " << it>first << ": " << it>second; 61 } 62 result << "}"; 63 return result.str(); 64 } 65 ; 66 67 #define P(x) (std::string(#x) + ": " + repr(x)) 8 #define P(x) (std::string(#x) + ": " + common::repr(x)) 68 9 // debug print 69 10 #define DP(x) std::cout << P(x) << std::endl; … … 72 13 // message(indented) 73 14 #define MSG(x) std::cout << #x << std::endl; 74 75 15 } 76 16 
lang/cplusplus/debugprint/indented_debugprint.hpp
r20220 r20221 1 #ifndef NISHIO_ DEBUGPRINT_H2 #define NISHIO_ DEBUGPRINT_H1 #ifndef NISHIO_INDENTED_DEBUGPRINT_HPP 2 #define NISHIO_INDENTED_DEBUGPRINT_HPP 3 3 4 4 #include <iostream> 5 #include <sstream> 6 #include <utility> 7 #include <vector> 8 #include <map> 9 #include <algorithm> 10 5 #include "debugprint_common.hpp" 11 6 namespace debugprint{ 12 13 // make string representation14 template<class T>15 std::string repr(const T& value) {16 std::ostringstream result;17 result << value;18 return result.str();19 }20 ;21 22 // repr pair23 template<class K, class V>24 std::string repr(const std::pair<K, V>& x) {25 std::ostringstream result;26 result << "(" << x.first << ", " << x.second << ")";27 return result.str();28 }29 ;30 31 // repr vector32 template<class V>33 std::string repr(const std::vector<V>& v) {34 std::ostringstream result;35 if (v.empty()) {36 return "{}";37 }38 result << "{" << *(v.begin());39 typedef typename std::vector<V>::const_iterator Iter;40 for (Iter it = ++v.begin(), end = v.end(); it != end; ++it) {41 result << ", " << *it;42 }43 result << "}";44 return result.str();45 }46 ;47 48 template<typename K, typename V>49 std::string repr(const std::map<K, V> m) {50 std::ostringstream result;51 if(m.empty()){52 return "{}";53 }54 typedef typename std::map<K, V>::const_iterator Iter;55 Iter it = m.begin();56 result << "{" << it>first << ": " << it>second;57 ++it;58 for (Iter end = m.end(); it != end; ++it) {59 if (it != m.begin())60 result << ", " << it>first << ": " << it>second;61 }62 result << "}";63 return result.str();64 }65 ;66 7 67 8 struct Indenter{ … … 86 27 Indenter indent; 87 28 88 #define P(x) (std::string(#x) + ": " + repr(x))29 #define P(x) (std::string(#x) + ": " + common::repr(x)) 89 30 // debug print 90 31 #define DP(x) indent.indent() << P(x) << std::endl; indent.head_of_line = true;