| 1 |
#ifndef PRINTER_H_INCLUDED |
| 2 |
#define PRINTER_H_INCLUDED |
| 3 |
|
| 4 |
#include <cassert> |
| 5 |
#include <string> |
| 6 |
#include <vector> |
| 7 |
#include <list> |
| 8 |
#include <iostream> |
| 9 |
#include <sstream> |
| 10 |
|
| 11 |
class Printer |
| 12 |
{ |
| 13 |
protected: |
| 14 |
|
| 15 |
// Precision in output of floats |
| 16 |
const std::size_t precision; |
| 17 |
const double epsilon; |
| 18 |
|
| 19 |
// Output stream |
| 20 |
std::ostream & os; |
| 21 |
|
| 22 |
// Indentation level |
| 23 |
int level; |
| 24 |
|
| 25 |
|
| 26 |
public: |
| 27 |
|
| 28 |
Printer(std::ostream & os): |
| 29 |
precision(16), |
| 30 |
epsilon(1e-16), |
| 31 |
os(os), |
| 32 |
level(0) |
| 33 |
{} |
| 34 |
|
| 35 |
|
| 36 |
/// Indent to current level |
| 37 |
void indent() |
| 38 |
{ |
| 39 |
for (int i=0; i<level; ++i) |
| 40 |
os << " "; |
| 41 |
} |
| 42 |
|
| 43 |
/// Format name with optional integers appended |
| 44 |
std::string format_name(std::string name, int i=-1, int j=-1) |
| 45 |
{ |
| 46 |
std::stringstream s; |
| 47 |
s << name; |
| 48 |
if (i >= 0) |
| 49 |
s << "_" << i; |
| 50 |
if (j >= 0) |
| 51 |
s << "_" << j; |
| 52 |
return s.str(); |
| 53 |
} |
| 54 |
|
| 55 |
/// Format '<indent>"foo": ' properly |
| 56 |
void begin_entry(std::string name, int i=-1, int j=-1) |
| 57 |
{ |
| 58 |
name = format_name(name, i, j); |
| 59 |
indent(); |
| 60 |
os << '"' << name << '"' << ": "; |
| 61 |
} |
| 62 |
|
| 63 |
/// Begin an unnamed block |
| 64 |
void begin() |
| 65 |
{ |
| 66 |
os << "{" << std::endl; |
| 67 |
++level; |
| 68 |
} |
| 69 |
|
| 70 |
/// Begin a named block entry |
| 71 |
void begin(std::string name, int i=-1, int j=-1) |
| 72 |
{ |
| 73 |
begin_entry(name, i, j); |
| 74 |
begin(); |
| 75 |
} |
| 76 |
|
| 77 |
/// End current named or unnamed block |
| 78 |
void end() |
| 79 |
{ |
| 80 |
--level; |
| 81 |
assert(level >= 0); |
| 82 |
indent(); |
| 83 |
if (level > 0) |
| 84 |
os << "}," << std::endl; |
| 85 |
else |
| 86 |
os << "}" << std::endl; |
| 87 |
} |
| 88 |
|
| 89 |
/// Format a value type properly |
| 90 |
template<typename T> |
| 91 |
void print_value(T value); |
| 92 |
|
| 93 |
/// Set a named single value entry |
| 94 |
template<typename T> |
| 95 |
void print_scalar(std::string name, T value, int i=-1, int j=-1) |
| 96 |
{ |
| 97 |
begin_entry(name, i, j); |
| 98 |
print_value(value); |
| 99 |
os << ", " << std::endl; |
| 100 |
} |
| 101 |
|
| 102 |
/// Set a named array valued entry |
| 103 |
template<typename T> |
| 104 |
void print_array(std::string name, int n, T * values, int i=-1, int j=-1) |
| 105 |
{ |
| 106 |
begin_entry(name, i, j); |
| 107 |
os << "["; |
| 108 |
if (n > 0) |
| 109 |
print_value(values[0]); |
| 110 |
for (int k=1; k<n; ++k) |
| 111 |
{ |
| 112 |
os << ", "; |
| 113 |
print_value(values[k]); |
| 114 |
} |
| 115 |
os << "], " << std::endl; |
| 116 |
} |
| 117 |
|
| 118 |
/// Set a named vector valued entry |
| 119 |
template<typename T> |
| 120 |
void print_vector(std::string name, typename std::vector<T> values, int i=-1, int j=-1) |
| 121 |
{ |
| 122 |
begin_entry(name, i, j); |
| 123 |
os << "["; |
| 124 |
typename std::vector<T>::iterator k=values.begin(); |
| 125 |
if (k!=values.end()) |
| 126 |
{ |
| 127 |
print_value(*k); |
| 128 |
++k; |
| 129 |
} |
| 130 |
while (k!=values.end()) |
| 131 |
{ |
| 132 |
os << ", "; |
| 133 |
print_value(*k); |
| 134 |
++k; |
| 135 |
} |
| 136 |
os << "], " << std::endl; |
| 137 |
} |
| 138 |
|
| 139 |
}; |
| 140 |
|
| 141 |
/// Fallback formatting for any value type |
| 142 |
template<typename T> |
| 143 |
void Printer::print_value(T value) |
| 144 |
{ |
| 145 |
os << value; |
| 146 |
} |
| 147 |
|
| 148 |
/// Use precision for floats |
| 149 |
template<> |
| 150 |
void Printer::print_value(double value) |
| 151 |
{ |
| 152 |
os.setf(std::ios::scientific, std::ios::floatfield); |
| 153 |
os.precision(precision); |
| 154 |
if (std::abs(static_cast<double>(value)) < epsilon) |
| 155 |
os << "0.0"; |
| 156 |
else |
| 157 |
os << value; |
| 158 |
} |
| 159 |
|
| 160 |
/// Use precision for floats |
| 161 |
template<> |
| 162 |
void Printer::print_value(float value) |
| 163 |
{ |
| 164 |
print_value(static_cast<double>(value)); |
| 165 |
} |
| 166 |
|
| 167 |
/// Wrap strings in quotes |
| 168 |
template<> |
| 169 |
void Printer::print_value(std::string value) |
| 170 |
{ |
| 171 |
os << '"' << value << '"'; |
| 172 |
} |
| 173 |
|
| 174 |
/// Wrap strings in quotes |
| 175 |
template<> |
| 176 |
void Printer::print_value(const char * value) |
| 177 |
{ |
| 178 |
os << '"' << value << '"'; |
| 179 |
} |
| 180 |
|
| 181 |
#endif |