aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/objects/objectdumper.cpp
blob: 2287906fe0fcb5921e693d0dec630c67cd498806 (plain) (blame)
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
94
95
96
97
98
99
100
101
102
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "objectdumper.h"
#include <vespa/vespalib/util/stringfmt.h>

namespace vespalib {

void
ObjectDumper::addIndent()
{
    int n = _currIndent;
    if (n < 0) {
        n = 0;
    }
    _str.append(vespalib::string(n, ' '));
}

void
ObjectDumper::addLine(const vespalib::string &line)
{
    addIndent();
    _str.append(line);
    _str.push_back('\n');
}

void
ObjectDumper::openScope()
{
    _currIndent += _indent;
}

void
ObjectDumper::closeScope()
{
    _currIndent -= _indent;
}

ObjectDumper::ObjectDumper(int indent)
    : _str(),
      _indent(indent),
      _currIndent(0)
{
}

ObjectDumper::~ObjectDumper() = default;

//-----------------------------------------------------------------------------

void
ObjectDumper::openStruct(const vespalib::string &name, const vespalib::string &type)
{
    if (name.empty()) {
        addLine(make_string("%s {", type.c_str()));
    } else {
        addLine(make_string("%s: %s {", name.c_str(), type.c_str()));
    }
    openScope();
}

void
ObjectDumper::closeStruct()
{
    closeScope();
    addLine("}");
}

void
ObjectDumper::visitBool(const vespalib::string &name, bool value)
{
    addLine(make_string("%s: %s", name.c_str(), value? "true" : "false"));
}

void
ObjectDumper::visitInt(const vespalib::string &name, int64_t value)
{
    addLine(make_string("%s: %" PRId64 "", name.c_str(), value));
}

void
ObjectDumper::visitFloat(const vespalib::string &name, double value)
{
    addLine(make_string("%s: %g", name.c_str(), value));
}

void
ObjectDumper::visitString(const vespalib::string &name, const vespalib::string &value)
{
    addLine(make_string("%s: '%s'", name.c_str(), value.c_str()));
}

void
ObjectDumper::visitNull(const vespalib::string &name)
{
    addLine(make_string("%s: <NULL>", name.c_str()));
}

void
ObjectDumper::visitNotImplemented()
{
    addLine("<member visit not implemented>");
}

} // namespace vespalib