root/lang/cplusplus/debugprint/debugprint_common.hpp @ 20293

Revision 20293, 1.6 kB (checked in by nishio, 5 years ago)

/lang/cplusplus/debugprint small fix

Line 
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
15namespace debugprint{
16  namespace common{
17
18// make string representation
19template<class T>
20std::string repr(const T& value) {
21  std::ostringstream result;
22  result << value;
23  return result.str();
24}
25;
26
27// repr pair
28template<class K, class V>
29std::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
37template<class V>
38std::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
53template<typename K, typename V>
54std::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
Note: See TracBrowser for help on using the browser.