• 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_OBJECT_WRITER_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__
33 
34 #include <google/protobuf/stubs/common.h>
35 #include <google/protobuf/stubs/stringpiece.h>
36 
37 #include <google/protobuf/port_def.inc>
38 
39 namespace google {
40 namespace protobuf {
41 namespace util {
42 namespace converter {
43 
44 class DataPiece;
45 
46 // An ObjectWriter is an interface for writing a stream of events
47 // representing objects and collections. Implementation of this
48 // interface can be used to write an object stream to an in-memory
49 // structure, protobufs, JSON, XML, or any other output format
50 // desired. The ObjectSource interface is typically used as the
51 // source of an object stream.
52 //
53 // See JsonObjectWriter for a sample implementation of ObjectWriter
54 // and its use.
55 //
56 // Derived classes could be thread-unsafe.
57 //
58 // TODO(xinb): seems like a prime candidate to apply the RAII paradigm
59 // and get rid the need to call EndXXX().
60 class PROTOBUF_EXPORT ObjectWriter {
61  public:
~ObjectWriter()62   virtual ~ObjectWriter() {}
63 
64   // Starts an object. If the name is empty, the object will not be named.
65   virtual ObjectWriter* StartObject(StringPiece name) = 0;
66 
67   // Ends an object.
68   virtual ObjectWriter* EndObject() = 0;
69 
70   // Starts a list. If the name is empty, the list will not be named.
71   virtual ObjectWriter* StartList(StringPiece name) = 0;
72 
73   // Ends a list.
74   virtual ObjectWriter* EndList() = 0;
75 
76   // Renders a boolean value.
77   virtual ObjectWriter* RenderBool(StringPiece name, bool value) = 0;
78 
79   // Renders an 32-bit integer value.
80   virtual ObjectWriter* RenderInt32(StringPiece name, int32 value) = 0;
81 
82   // Renders an 32-bit unsigned integer value.
83   virtual ObjectWriter* RenderUint32(StringPiece name, uint32 value) = 0;
84 
85   // Renders a 64-bit integer value.
86   virtual ObjectWriter* RenderInt64(StringPiece name, int64 value) = 0;
87 
88   // Renders an 64-bit unsigned integer value.
89   virtual ObjectWriter* RenderUint64(StringPiece name, uint64 value) = 0;
90 
91 
92   // Renders a double value.
93   virtual ObjectWriter* RenderDouble(StringPiece name, double value) = 0;
94 
95   // Renders a float value.
96   virtual ObjectWriter* RenderFloat(StringPiece name, float value) = 0;
97 
98   // Renders a StringPiece value. This is for rendering strings.
99   virtual ObjectWriter* RenderString(StringPiece name,
100                                      StringPiece value) = 0;
101 
102   // Renders a bytes value.
103   virtual ObjectWriter* RenderBytes(StringPiece name, StringPiece value) = 0;
104 
105   // Renders a Null value.
106   virtual ObjectWriter* RenderNull(StringPiece name) = 0;
107 
108 
109   // Renders a DataPiece object to a ObjectWriter.
110   static void RenderDataPieceTo(const DataPiece& data, StringPiece name,
111                                 ObjectWriter* ow);
112 
113   // Indicates whether this ObjectWriter has completed writing the root message,
114   // usually this means writing of one complete object. Subclasses must override
115   // this behavior appropriately.
done()116   virtual bool done() { return false; }
117 
set_use_strict_base64_decoding(bool value)118   void set_use_strict_base64_decoding(bool value) {
119     use_strict_base64_decoding_ = value;
120   }
121 
use_strict_base64_decoding()122   bool use_strict_base64_decoding() const {
123     return use_strict_base64_decoding_;
124   }
125 
126  protected:
ObjectWriter()127   ObjectWriter() : use_strict_base64_decoding_(true) {}
128 
129  private:
130   // If set to true, we use the stricter version of base64 decoding for byte
131   // fields by making sure decoded version encodes back to the original string.
132   bool use_strict_base64_decoding_;
133 
134   // Do not add any data members to this class.
135   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ObjectWriter);
136 };
137 
138 }  // namespace converter
139 }  // namespace util
140 }  // namespace protobuf
141 }  // namespace google
142 
143 #include <google/protobuf/port_undef.inc>
144 
145 #endif  // GOOGLE_PROTOBUF_UTIL_CONVERTER_OBJECT_WRITER_H__
146