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


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59