1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/Writer.h
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#ifndef LIBBOARDGAME_BASE_WRITER_H
#define LIBBOARDGAME_BASE_WRITER_H
#include <iosfwd>
#include <string>
#include <vector>
#include "StringUtil.h"
namespace libboardgame_base {
using namespace std;
//-----------------------------------------------------------------------------
class Writer
{
public:
explicit Writer(ostream& out);
/** @name Formatting options.
Should be set before starting to write. */
/** @{ */
/** @param indent The number of spaces to indent subtrees, -1 means
to not even use newlines. */
void set_indent(int indent) { m_indent = indent; }
/** @} */ // @name
void begin_tree();
void end_tree();
void begin_node();
void end_node();
void write_property(const string& id, const char* value);
template<typename T>
void write_property(const string& id, const T& value);
template<typename T>
void write_property(const string& id, const vector<T>& values);
private:
ostream& m_out;
int m_indent = 0;
unsigned m_current_indent = 0;
unsigned m_level = 0;
static string get_escaped(const string& s);
void write_indent();
};
inline void Writer::write_property(const string& id, const char* value)
{
vector<const char*> values(1, value);
write_property(id, values);
}
template<typename T>
void Writer::write_property(const string& id, const T& value)
{
vector<T> values(1, value);
write_property(id, values);
}
template<typename T>
void Writer::write_property(const string& id, const vector<T>& values)
{
m_out << id;
for (auto& i : values)
m_out << '[' << get_escaped(to_string(i)) << ']';
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
#endif // LIBBOARDGAME_BASE_WRITER_H
|