1ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 2ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc. All rights reserved. 3ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 4ffe3c632Sopenharmony_ci// 5ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 6ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 7ffe3c632Sopenharmony_ci// met: 8ffe3c632Sopenharmony_ci// 9ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 10ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 11ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 12ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 13ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 14ffe3c632Sopenharmony_ci// distribution. 15ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 16ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 17ffe3c632Sopenharmony_ci// this software without specific prior written permission. 18ffe3c632Sopenharmony_ci// 19ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffe3c632Sopenharmony_ci 31ffe3c632Sopenharmony_ci#ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__ 32ffe3c632Sopenharmony_ci#define GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__ 33ffe3c632Sopenharmony_ci 34ffe3c632Sopenharmony_ci#include <memory> 35ffe3c632Sopenharmony_ci 36ffe3c632Sopenharmony_ci#include <google/protobuf/stubs/casts.h> 37ffe3c632Sopenharmony_ci#include <google/protobuf/stubs/common.h> 38ffe3c632Sopenharmony_ci#include <google/protobuf/util/internal/object_writer.h> 39ffe3c632Sopenharmony_ci 40ffe3c632Sopenharmony_ci#include <google/protobuf/port_def.inc> 41ffe3c632Sopenharmony_ci 42ffe3c632Sopenharmony_cinamespace google { 43ffe3c632Sopenharmony_cinamespace protobuf { 44ffe3c632Sopenharmony_cinamespace util { 45ffe3c632Sopenharmony_cinamespace converter { 46ffe3c632Sopenharmony_ci 47ffe3c632Sopenharmony_ci// An StructuredObjectWriter is an ObjectWriter for writing 48ffe3c632Sopenharmony_ci// tree-structured data in a stream of events representing objects 49ffe3c632Sopenharmony_ci// and collections. Implementation of this interface can be used to 50ffe3c632Sopenharmony_ci// write an object stream to an in-memory structure, protobufs, 51ffe3c632Sopenharmony_ci// JSON, XML, or any other output format desired. The ObjectSource 52ffe3c632Sopenharmony_ci// interface is typically used as the source of an object stream. 53ffe3c632Sopenharmony_ci// 54ffe3c632Sopenharmony_ci// See JsonObjectWriter for a sample implementation of 55ffe3c632Sopenharmony_ci// StructuredObjectWriter and its use. 56ffe3c632Sopenharmony_ci// 57ffe3c632Sopenharmony_ci// Derived classes could be thread-unsafe. 58ffe3c632Sopenharmony_ciclass PROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter { 59ffe3c632Sopenharmony_ci public: 60ffe3c632Sopenharmony_ci virtual ~StructuredObjectWriter() {} 61ffe3c632Sopenharmony_ci 62ffe3c632Sopenharmony_ci protected: 63ffe3c632Sopenharmony_ci // A base element class for subclasses to extend, makes tracking state easier. 64ffe3c632Sopenharmony_ci // 65ffe3c632Sopenharmony_ci // StructuredObjectWriter behaves as a visitor. BaseElement represents a node 66ffe3c632Sopenharmony_ci // in the input tree. Implementation of StructuredObjectWriter should also 67ffe3c632Sopenharmony_ci // extend BaseElement to keep track of the location in the input tree. 68ffe3c632Sopenharmony_ci class PROTOBUF_EXPORT BaseElement { 69ffe3c632Sopenharmony_ci public: 70ffe3c632Sopenharmony_ci // Takes ownership of the parent Element. 71ffe3c632Sopenharmony_ci explicit BaseElement(BaseElement* parent) 72ffe3c632Sopenharmony_ci : parent_(parent), level_(parent == NULL ? 0 : parent->level() + 1) {} 73ffe3c632Sopenharmony_ci virtual ~BaseElement() {} 74ffe3c632Sopenharmony_ci 75ffe3c632Sopenharmony_ci // Releases ownership of the parent and returns a pointer to it. 76ffe3c632Sopenharmony_ci template <typename ElementType> 77ffe3c632Sopenharmony_ci ElementType* pop() { 78ffe3c632Sopenharmony_ci return down_cast<ElementType*>(parent_.release()); 79ffe3c632Sopenharmony_ci } 80ffe3c632Sopenharmony_ci 81ffe3c632Sopenharmony_ci // Returns true if this element is the root. 82ffe3c632Sopenharmony_ci bool is_root() const { return parent_ == nullptr; } 83ffe3c632Sopenharmony_ci 84ffe3c632Sopenharmony_ci // Returns the number of hops from this element to the root element. 85ffe3c632Sopenharmony_ci int level() const { return level_; } 86ffe3c632Sopenharmony_ci 87ffe3c632Sopenharmony_ci protected: 88ffe3c632Sopenharmony_ci // Returns pointer to parent element without releasing ownership. 89ffe3c632Sopenharmony_ci virtual BaseElement* parent() const { return parent_.get(); } 90ffe3c632Sopenharmony_ci 91ffe3c632Sopenharmony_ci private: 92ffe3c632Sopenharmony_ci // Pointer to the parent Element. 93ffe3c632Sopenharmony_ci std::unique_ptr<BaseElement> parent_; 94ffe3c632Sopenharmony_ci 95ffe3c632Sopenharmony_ci // Number of hops to the root Element. 96ffe3c632Sopenharmony_ci // The root Element has nullptr parent_ and a level_ of 0. 97ffe3c632Sopenharmony_ci const int level_; 98ffe3c632Sopenharmony_ci 99ffe3c632Sopenharmony_ci GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(BaseElement); 100ffe3c632Sopenharmony_ci }; 101ffe3c632Sopenharmony_ci 102ffe3c632Sopenharmony_ci StructuredObjectWriter() {} 103ffe3c632Sopenharmony_ci 104ffe3c632Sopenharmony_ci // Returns the current element. Used for indentation and name overrides. 105ffe3c632Sopenharmony_ci virtual BaseElement* element() = 0; 106ffe3c632Sopenharmony_ci 107ffe3c632Sopenharmony_ci private: 108ffe3c632Sopenharmony_ci // Do not add any data members to this class. 109ffe3c632Sopenharmony_ci GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StructuredObjectWriter); 110ffe3c632Sopenharmony_ci}; 111ffe3c632Sopenharmony_ci 112ffe3c632Sopenharmony_ci} // namespace converter 113ffe3c632Sopenharmony_ci} // namespace util 114ffe3c632Sopenharmony_ci} // namespace protobuf 115ffe3c632Sopenharmony_ci} // namespace google 116ffe3c632Sopenharmony_ci 117ffe3c632Sopenharmony_ci#include <google/protobuf/port_undef.inc> 118ffe3c632Sopenharmony_ci 119ffe3c632Sopenharmony_ci#endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__ 120