aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/objects/objectdumper.h
blob: 0fb27c03ac235df89a445d4d96d5ba89b07c296b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "objectvisitor.h"

namespace vespalib {

/**
 * This is a concrete object visitor that will build up a structured
 * human-readable string representation of an object.
 **/
class ObjectDumper : public ObjectVisitor
{
private:
    vespalib::string _str;
    int         _indent;
    int         _currIndent;

    /**
     * Add a number of spaces equal to the current indent to the
     * string we are building.
     **/
    void addIndent();

    /**
     * Add a complete line of output. Appropriate indentation will be
     * added before the given string and a newline will be added after
     * it.
     *
     * @param line the line we want to add
     **/
    void addLine(const vespalib::string &line);

    /**
     * Open a subscope by increasing the current indent level
     **/
    void openScope();

    /**
     * Close a subscope by decreasing the current indent level
     **/
    void closeScope();

public:
    /**
     * Create an object dumper with the given indent size; default is
     * 4 spaces per indent level.
     *
     * @param indent indent size in number of spaces
     **/
    ObjectDumper(int indent = 4);
    ~ObjectDumper();

    /**
     * Obtain the created object string representation. This object
     * should be invoked after the complete object structure has been
     * visited.
     *
     * @return object string representation
     **/
    vespalib::string toString() const { return _str; }

    void openStruct(const vespalib::string &name, const vespalib::string &type) override;
    void closeStruct() override;
    void visitBool(const vespalib::string &name, bool value) override;
    void visitInt(const vespalib::string &name, int64_t value) override;
    void visitFloat(const vespalib::string &name, double value) override;
    void visitString(const vespalib::string &name, const vespalib::string &value) override;
    void visitNull(const vespalib::string &name) override;
    void visitNotImplemented() override;
};

} // namespace vespalib