From f1c2464fd83e6623000ad6eb59272b189fffb78e Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 5 Mar 2019 06:04:04 +0000 Subject: Transform to slime. --- .../src/vespa/vespalib/objects/CMakeLists.txt | 1 + .../src/vespa/vespalib/objects/object2slime.cpp | 75 ++++++++++++++++++++++ .../src/vespa/vespalib/objects/object2slime.h | 41 ++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp create mode 100644 staging_vespalib/src/vespa/vespalib/objects/object2slime.h (limited to 'staging_vespalib/src') diff --git a/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt b/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt index 12970a7d39b..f3afda94fe6 100644 --- a/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt +++ b/staging_vespalib/src/vespa/vespalib/objects/CMakeLists.txt @@ -5,6 +5,7 @@ vespa_add_library(staging_vespalib_vespalib_objects OBJECT namedobject.cpp objectvisitor.cpp objectdumper.cpp + object2slime.cpp visit.cpp objectpredicate.cpp objectoperation.cpp diff --git a/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp b/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp new file mode 100644 index 00000000000..f06060529c3 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/objects/object2slime.cpp @@ -0,0 +1,75 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#include "object2slime.h" +#include +#include + + +namespace vespalib { + +Object2Slime::Object2Slime(slime::Cursor & cursor) + : _cursor(&cursor), + _stack() +{ +} + +Object2Slime::~Object2Slime() = default; + +//----------------------------------------------------------------------------- + +void +Object2Slime::openStruct(const vespalib::string &name, const vespalib::string &type) +{ + _stack.push_back(_cursor); + + if (name.empty()) { + _cursor = & _cursor->setObject(type); + } else { + _cursor = & _cursor->setObject(name); + _cursor->setString("[type]", type); + } +} + +void +Object2Slime::closeStruct() +{ + _cursor = _stack.back(); + _stack.pop_back(); +} + +void +Object2Slime::visitBool(const vespalib::string &name, bool value) +{ + _cursor->setBool(name, value); +} + +void +Object2Slime::visitInt(const vespalib::string &name, int64_t value) +{ + _cursor->setLong(name, value); +} + +void +Object2Slime::visitFloat(const vespalib::string &name, double value) +{ + _cursor->setDouble(name, value); +} + +void +Object2Slime::visitString(const vespalib::string &name, const vespalib::string &value) +{ + _cursor->setString(name, value); +} + +void +Object2Slime::visitNull(const vespalib::string &name) +{ + _cursor->setNix(name); +} + +void +Object2Slime::visitNotImplemented() +{ + _cursor->setNix("not_implemented"); +} + +} // namespace vespalib diff --git a/staging_vespalib/src/vespa/vespalib/objects/object2slime.h b/staging_vespalib/src/vespa/vespalib/objects/object2slime.h new file mode 100644 index 00000000000..9c147047a74 --- /dev/null +++ b/staging_vespalib/src/vespa/vespalib/objects/object2slime.h @@ -0,0 +1,41 @@ +// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include "objectvisitor.h" +#include + +namespace vespalib { + +namespace slime { class Cursor; } + +/** + * This is a concrete object visitor that will build up a structured + * human-readable string representation of an object. + **/ +class Object2Slime : public ObjectVisitor +{ +private: + slime::Cursor * _cursor; + std::vector _stack; + +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 + **/ + Object2Slime(slime::Cursor & cursor); + ~Object2Slime(); + + 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 -- cgit v1.2.3