Belle II Software  release-05-02-19
KeyValuePrinter.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2017 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Christian Pulvermacher *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <iomanip>
14 #include <sstream>
15 #include <string>
16 #include <vector>
17 #include <map>
18 
19 namespace Belle2 {
48  class KeyValuePrinter {
49  public:
55  explicit KeyValuePrinter(bool use_json, unsigned key_max_length = 10):
56  m_json(use_json),
57  m_maxpad(key_max_length + 2),
58  m_delim("")
59  { }
60  ~KeyValuePrinter() { }
61 
63  std::string string() const
64  {
65  if (m_json)
66  return "{\n" + m_stream.str() + "\n}\n";
67  else
68  return m_stream.str();
69  }
71  template <class T> void put(const std::string& key, const T& value)
72  {
73  if (m_json) {
74  m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
75  } else {
76  m_stream << std::left << std::setw(m_maxpad) << key + ": " << escape(value) << "\n";
77  }
78  m_delim = ",\n";
79  }
81  template <class T> void put(const std::string& key, const std::map<std::string, T>& value)
82  {
83  if (m_json) {
84  m_stream << m_delim << "\t" << escape(key) << ": " << escape(value) << "";
85  } else {
86  m_stream << "\n" << key << "\n";
87  for (char unused : key) {
88  (void)unused;
89  m_stream << '-';
90  }
91  m_stream << "\n" << escape(value) << "\n";
92  }
93  m_delim = ",\n";
94  }
95  private:
96  bool m_json;
97  unsigned m_maxpad;
98  std::string m_delim;
99  std::stringstream m_stream;
102  std::string escape(const std::string& value) const;
104  std::string escape(const char* value) const { return escape(std::string(value)); }
106  template <class T> std::string escape(const T& value) const { return std::to_string(value); }
108  template <class T> std::string escape(const std::vector<T>& value) const
109  {
110  std::string delim = "";
111  std::string s = "[";
112  for (auto el : value) {
113  s += delim + escape(el);
114  delim = ", ";
115  }
116  s += "]";
117  return s;
118  }
120  template <class T> std::string escape(const std::map<std::string, T>& value) const
121  {
122  //well, it's valid json, but not pretty
123  KeyValuePrinter printer(m_json, m_maxpad - 2);
124  for (auto pair : value)
125  printer.put(pair.first, pair.second);
126  return printer.string();
127  }
128  };
130 }
Belle2::KeyValuePrinter::escape
std::string escape(const std::string &value) const
escape string.
Definition: KeyValuePrinter.cc:16
Belle2::KeyValuePrinter
create human-readable or JSON output for key value pairs.
Definition: KeyValuePrinter.h:56
Belle2::KeyValuePrinter::m_delim
std::string m_delim
for JSON: comma to print after prev entry.
Definition: KeyValuePrinter.h:106
Belle2::KeyValuePrinter::m_maxpad
unsigned m_maxpad
for human-readable output: how much padding after key?
Definition: KeyValuePrinter.h:105
Belle2::KeyValuePrinter::KeyValuePrinter
KeyValuePrinter(bool use_json, unsigned key_max_length=10)
Ctor.
Definition: KeyValuePrinter.h:63
Belle2::KeyValuePrinter::put
void put(const std::string &key, const T &value)
Add one key-value pair.
Definition: KeyValuePrinter.h:79
Belle2::KeyValuePrinter::string
std::string string() const
Return completed string.
Definition: KeyValuePrinter.h:71
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::KeyValuePrinter::m_json
bool m_json
create JSON output?
Definition: KeyValuePrinter.h:104
Belle2::KeyValuePrinter::m_stream
std::stringstream m_stream
output being built.
Definition: KeyValuePrinter.h:107