• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef _SHARED_PTR_H
36 #include <google/protobuf/stubs/shared_ptr.h>
37 #endif
38 
39 #include <google/protobuf/stubs/casts.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/util/internal/object_writer.h>
42 
43 namespace google {
44 namespace protobuf {
45 namespace util {
46 namespace converter {
47 
48 // An StructuredObjectWriter is an ObjectWriter for writing
49 // tree-structured data in a stream of events representing objects
50 // and collections. Implementation of this interface can be used to
51 // write an object stream to an in-memory structure, protobufs,
52 // JSON, XML, or any other output format desired. The ObjectSource
53 // interface is typically used as the source of an object stream.
54 //
55 // See JsonObjectWriter for a sample implementation of
56 // StructuredObjectWriter and its use.
57 //
58 // Derived classes could be thread-unsafe.
59 class LIBPROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter {
60  public:
~StructuredObjectWriter()61   virtual ~StructuredObjectWriter() {}
62 
63  protected:
64   // A base element class for subclasses to extend, makes tracking state easier.
65   //
66   // StructuredObjectWriter behaves as a visitor. BaseElement represents a node
67   // in the input tree. Implementation of StructuredObjectWriter should also
68   // extend BaseElement to keep track of the location in the input tree.
69   class LIBPROTOBUF_EXPORT BaseElement {
70    public:
71     // Takes ownership of the parent Element.
BaseElement(BaseElement * parent)72     explicit BaseElement(BaseElement* parent)
73         : parent_(parent), level_(parent == NULL ? 0 : parent->level() + 1) {}
~BaseElement()74     virtual ~BaseElement() {}
75 
76     // Releases ownership of the parent and returns a pointer to it.
77     template <typename ElementType>
pop()78     ElementType* pop() {
79       return down_cast<ElementType*>(parent_.release());
80     }
81 
82     // Returns true if this element is the root.
is_root()83     bool is_root() const { return parent_ == NULL; }
84 
85     // Returns the number of hops from this element to the root element.
level()86     int level() const { return level_; }
87 
88    protected:
89     // Returns pointer to parent element without releasing ownership.
parent()90     virtual BaseElement* parent() const { return parent_.get(); }
91 
92    private:
93     // Pointer to the parent Element.
94     google::protobuf::scoped_ptr<BaseElement> parent_;
95 
96     // Number of hops to the root Element.
97     // The root Element has NULL parent_ and a level_ of 0.
98     const int level_;
99 
100     GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(BaseElement);
101   };
102 
StructuredObjectWriter()103   StructuredObjectWriter() {}
104 
105   // Returns the current element. Used for indentation and name overrides.
106   virtual BaseElement* element() = 0;
107 
108  private:
109   // Do not add any data members to this class.
110   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StructuredObjectWriter);
111 };
112 
113 }  // namespace converter
114 }  // namespace util
115 }  // namespace protobuf
116 
117 }  // namespace google
118 #endif  // GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__
119