Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: third_party/protobuf/src/google/protobuf/util/internal/default_value_objectwriter.h

Issue 1291903002: Pull new version of protobuf sources. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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_DEFAULT_VALUE_OBJECTWRITER_H__
32 #define GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
33
34 #include <memory>
35 #ifndef _SHARED_PTR_H
36 #include <google/protobuf/stubs/shared_ptr.h>
37 #endif
38 #include <stack>
39 #include <vector>
40
41 #include <google/protobuf/stubs/common.h>
42 #include <google/protobuf/util/internal/type_info.h>
43 #include <google/protobuf/util/internal/datapiece.h>
44 #include <google/protobuf/util/internal/object_writer.h>
45 #include <google/protobuf/util/internal/utility.h>
46 #include <google/protobuf/util/type_resolver.h>
47 #include <google/protobuf/stubs/stringpiece.h>
48
49 namespace google {
50 namespace protobuf {
51 namespace util {
52 namespace converter {
53
54 // An ObjectWriter that renders non-repeated primitive fields of proto messages
55 // with their default values. DefaultValueObjectWriter holds objects, lists and
56 // fields it receives in a tree structure and writes them out to another
57 // ObjectWriter when EndObject() is called on the root object. It also writes
58 // out all non-repeated primitive fields that haven't been explicitly rendered
59 // with their default values (0 for numbers, "" for strings, etc).
60 class LIBPROTOBUF_EXPORT DefaultValueObjectWriter : public ObjectWriter {
61 public:
62 DefaultValueObjectWriter(TypeResolver* type_resolver,
63 const google::protobuf::Type& type,
64 ObjectWriter* ow);
65
66 virtual ~DefaultValueObjectWriter();
67
68 // ObjectWriter methods.
69 virtual DefaultValueObjectWriter* StartObject(StringPiece name);
70
71 virtual DefaultValueObjectWriter* EndObject();
72
73 virtual DefaultValueObjectWriter* StartList(StringPiece name);
74
75 virtual DefaultValueObjectWriter* EndList();
76
77 virtual DefaultValueObjectWriter* RenderBool(StringPiece name, bool value);
78
79 virtual DefaultValueObjectWriter* RenderInt32(StringPiece name, int32 value);
80
81 virtual DefaultValueObjectWriter* RenderUint32(StringPiece name,
82 uint32 value);
83
84 virtual DefaultValueObjectWriter* RenderInt64(StringPiece name, int64 value);
85
86 virtual DefaultValueObjectWriter* RenderUint64(StringPiece name,
87 uint64 value);
88
89 virtual DefaultValueObjectWriter* RenderDouble(StringPiece name,
90 double value);
91
92 virtual DefaultValueObjectWriter* RenderFloat(StringPiece name, float value);
93
94 virtual DefaultValueObjectWriter* RenderString(StringPiece name,
95 StringPiece value);
96 virtual DefaultValueObjectWriter* RenderBytes(StringPiece name,
97 StringPiece value);
98
99 virtual DefaultValueObjectWriter* RenderNull(StringPiece name);
100
101 virtual DefaultValueObjectWriter* DisableCaseNormalizationForNextKey();
102
103 private:
104 enum NodeKind {
105 PRIMITIVE = 0,
106 OBJECT = 1,
107 LIST = 2,
108 MAP = 3,
109 };
110
111 // "Node" represents a node in the tree that holds the input of
112 // DefaultValueObjectWriter.
113 class Node {
114 public:
115 Node(const string& name, const google::protobuf::Type* type, NodeKind kind,
116 const DataPiece& data, bool is_placeholder);
117 virtual ~Node() {
118 for (int i = 0; i < children_.size(); ++i) {
119 delete children_[i];
120 }
121 }
122
123 // Adds a child to this node. Takes ownership of this child.
124 void AddChild(Node* child) { children_.push_back(child); }
125
126 // Finds the child given its name.
127 Node* FindChild(StringPiece name);
128
129 // Populates children of this Node based on its type. If there are already
130 // children created, they will be merged to the result. Caller should pass
131 // in TypeInfo for looking up types of the children.
132 void PopulateChildren(TypeInfo* typeinfo);
133
134 // If this node is a leaf (has data), writes the current node to the
135 // ObjectWriter; if not, then recursively writes the children to the
136 // ObjectWriter.
137 void WriteTo(ObjectWriter* ow);
138
139 // Accessors
140 const string& name() const { return name_; }
141
142 const google::protobuf::Type* type() { return type_; }
143
144 void set_type(const google::protobuf::Type* type) { type_ = type; }
145
146 NodeKind kind() { return kind_; }
147
148 int number_of_children() { return children_.size(); }
149
150 void set_data(const DataPiece& data) { data_ = data; }
151
152 void set_disable_normalize(bool disable_normalize) {
153 disable_normalize_ = disable_normalize;
154 }
155
156 bool is_any() { return is_any_; }
157
158 void set_is_any(bool is_any) { is_any_ = is_any; }
159
160 void set_is_placeholder(bool is_placeholder) {
161 is_placeholder_ = is_placeholder;
162 }
163
164 private:
165 // Returns the Value Type of a map given the Type of the map entry and a
166 // TypeInfo instance.
167 const google::protobuf::Type* GetMapValueType(
168 const google::protobuf::Type& entry_type, TypeInfo* typeinfo);
169
170 // The name of this node.
171 string name_;
172 // google::protobuf::Type of this node. Owned by TypeInfo.
173 const google::protobuf::Type* type_;
174 // The kind of this node.
175 NodeKind kind_;
176 // Whether to disable case normalization of the name.
177 bool disable_normalize_;
178 // Whether this is a node for "Any".
179 bool is_any_;
180 // The data of this node when it is a leaf node.
181 DataPiece data_;
182 // Children of this node.
183 std::vector<Node*> children_;
184 // Whether this node is a placeholder for an object or list automatically
185 // generated when creating the parent node. Should be set to false after
186 // the parent node's StartObject()/StartList() method is called with this
187 // node's name.
188 bool is_placeholder_;
189 };
190
191 // Populates children of "node" if it is an "any" Node and its real type has
192 // been given.
193 void MaybePopulateChildrenOfAny(Node* node);
194
195 // Writes the root_ node to ow_ and resets the root_ and current_ pointer to
196 // NULL.
197 void WriteRoot();
198
199 // Creates a DataPiece containing the default value of the type of the field.
200 static DataPiece CreateDefaultDataPieceForField(
201 const google::protobuf::Field& field);
202
203 // Returns disable_normalize_ and reset it to false.
204 bool GetAndResetDisableNormalize() {
205 return disable_normalize_ ? (disable_normalize_ = false, true) : false;
206 }
207
208 // Adds or replaces the data_ of a primitive child node.
209 void RenderDataPiece(StringPiece name, const DataPiece& data);
210
211 // Type information for all the types used in the descriptor. Used to find
212 // google::protobuf::Type of nested messages/enums.
213 google::protobuf::scoped_ptr<TypeInfo> typeinfo_;
214 // google::protobuf::Type of the root message type.
215 const google::protobuf::Type& type_;
216 // Holds copies of strings passed to RenderString.
217 vector<string*> string_values_;
218
219 // Whether to disable case normalization of the next node.
220 bool disable_normalize_;
221 // The current Node. Owned by its parents.
222 Node* current_;
223 // The root Node.
224 google::protobuf::scoped_ptr<Node> root_;
225 // The stack to hold the path of Nodes from current_ to root_;
226 std::stack<Node*> stack_;
227
228 ObjectWriter* ow_;
229
230 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter);
231 };
232
233 } // namespace converter
234 } // namespace util
235 } // namespace protobuf
236
237 } // namespace google
238 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698