OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 StringPiece value); | 115 StringPiece value); |
116 virtual DefaultValueObjectWriter* RenderBytes(StringPiece name, | 116 virtual DefaultValueObjectWriter* RenderBytes(StringPiece name, |
117 StringPiece value); | 117 StringPiece value); |
118 | 118 |
119 virtual DefaultValueObjectWriter* RenderNull(StringPiece name); | 119 virtual DefaultValueObjectWriter* RenderNull(StringPiece name); |
120 | 120 |
121 // Register the callback for scrubbing of fields. Owership of | 121 // Register the callback for scrubbing of fields. Owership of |
122 // field_scrub_callback pointer is also transferred to this class | 122 // field_scrub_callback pointer is also transferred to this class |
123 void RegisterFieldScrubCallBack(FieldScrubCallBackPtr field_scrub_callback); | 123 void RegisterFieldScrubCallBack(FieldScrubCallBackPtr field_scrub_callback); |
124 | 124 |
125 // If set to true, empty lists are suppressed from output when default values | |
126 // are written. | |
127 void set_suppress_empty_list(bool value) { suppress_empty_list_ = value; } | |
128 | |
129 private: | 125 private: |
130 enum NodeKind { | 126 enum NodeKind { |
131 PRIMITIVE = 0, | 127 PRIMITIVE = 0, |
132 OBJECT = 1, | 128 OBJECT = 1, |
133 LIST = 2, | 129 LIST = 2, |
134 MAP = 3, | 130 MAP = 3, |
135 }; | 131 }; |
136 | 132 |
137 // "Node" represents a node in the tree that holds the input of | 133 // "Node" represents a node in the tree that holds the input of |
138 // DefaultValueObjectWriter. | 134 // DefaultValueObjectWriter. |
139 class LIBPROTOBUF_EXPORT Node { | 135 class LIBPROTOBUF_EXPORT Node { |
140 public: | 136 public: |
141 Node(const string& name, const google::protobuf::Type* type, NodeKind kind, | 137 Node(const string& name, const google::protobuf::Type* type, NodeKind kind, |
142 const DataPiece& data, bool is_placeholder, | 138 const DataPiece& data, bool is_placeholder, const vector<string>& path, |
143 const std::vector<string>& path, bool suppress_empty_list, | |
144 FieldScrubCallBack* field_scrub_callback); | 139 FieldScrubCallBack* field_scrub_callback); |
145 virtual ~Node() { | 140 virtual ~Node() { |
146 for (int i = 0; i < children_.size(); ++i) { | 141 for (int i = 0; i < children_.size(); ++i) { |
147 delete children_[i]; | 142 delete children_[i]; |
148 } | 143 } |
149 } | 144 } |
150 | 145 |
151 // Adds a child to this node. Takes ownership of this child. | 146 // Adds a child to this node. Takes ownership of this child. |
152 void AddChild(Node* child) { children_.push_back(child); } | 147 void AddChild(Node* child) { children_.push_back(child); } |
153 | 148 |
154 // Finds the child given its name. | 149 // Finds the child given its name. |
155 Node* FindChild(StringPiece name); | 150 Node* FindChild(StringPiece name); |
156 | 151 |
157 // Populates children of this Node based on its type. If there are already | 152 // Populates children of this Node based on its type. If there are already |
158 // children created, they will be merged to the result. Caller should pass | 153 // children created, they will be merged to the result. Caller should pass |
159 // in TypeInfo for looking up types of the children. | 154 // in TypeInfo for looking up types of the children. |
160 void PopulateChildren(const TypeInfo* typeinfo); | 155 void PopulateChildren(const TypeInfo* typeinfo); |
161 | 156 |
162 // If this node is a leaf (has data), writes the current node to the | 157 // If this node is a leaf (has data), writes the current node to the |
163 // ObjectWriter; if not, then recursively writes the children to the | 158 // ObjectWriter; if not, then recursively writes the children to the |
164 // ObjectWriter. | 159 // ObjectWriter. |
165 void WriteTo(ObjectWriter* ow); | 160 void WriteTo(ObjectWriter* ow); |
166 | 161 |
167 // Accessors | 162 // Accessors |
168 const string& name() const { return name_; } | 163 const string& name() const { return name_; } |
169 | 164 |
170 const std::vector<string>& path() const { return path_; } | 165 const vector<string>& path() const { return path_; } |
171 | 166 |
172 const google::protobuf::Type* type() const { return type_; } | 167 const google::protobuf::Type* type() const { return type_; } |
173 | 168 |
174 void set_type(const google::protobuf::Type* type) { type_ = type; } | 169 void set_type(const google::protobuf::Type* type) { type_ = type; } |
175 | 170 |
176 NodeKind kind() const { return kind_; } | 171 NodeKind kind() const { return kind_; } |
177 | 172 |
178 int number_of_children() const { return children_.size(); } | 173 int number_of_children() const { return children_.size(); } |
179 | 174 |
180 void set_data(const DataPiece& data) { data_ = data; } | 175 void set_data(const DataPiece& data) { data_ = data; } |
(...skipping 29 matching lines...) Expand all Loading... |
210 std::vector<Node*> children_; | 205 std::vector<Node*> children_; |
211 // Whether this node is a placeholder for an object or list automatically | 206 // Whether this node is a placeholder for an object or list automatically |
212 // generated when creating the parent node. Should be set to false after | 207 // generated when creating the parent node. Should be set to false after |
213 // the parent node's StartObject()/StartList() method is called with this | 208 // the parent node's StartObject()/StartList() method is called with this |
214 // node's name. | 209 // node's name. |
215 bool is_placeholder_; | 210 bool is_placeholder_; |
216 | 211 |
217 // Path of the field of this node | 212 // Path of the field of this node |
218 std::vector<string> path_; | 213 std::vector<string> path_; |
219 | 214 |
220 // Whether to suppress empty list output. | |
221 bool suppress_empty_list_; | |
222 | |
223 // Pointer to function for determining whether a field needs to be scrubbed | 215 // Pointer to function for determining whether a field needs to be scrubbed |
224 // or not. This callback is owned by the creator of this node. | 216 // or not. This callback is owned by the creator of this node. |
225 FieldScrubCallBack* field_scrub_callback_; | 217 FieldScrubCallBack* field_scrub_callback_; |
226 | 218 |
227 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node); | 219 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Node); |
228 }; | 220 }; |
229 | 221 |
230 // Populates children of "node" if it is an "any" Node and its real type has | 222 // Populates children of "node" if it is an "any" Node and its real type has |
231 // been given. | 223 // been given. |
232 void MaybePopulateChildrenOfAny(Node* node); | 224 void MaybePopulateChildrenOfAny(Node* node); |
(...skipping 16 matching lines...) Expand all Loading... |
249 const TypeInfo* typeinfo); | 241 const TypeInfo* typeinfo); |
250 | 242 |
251 // Type information for all the types used in the descriptor. Used to find | 243 // Type information for all the types used in the descriptor. Used to find |
252 // google::protobuf::Type of nested messages/enums. | 244 // google::protobuf::Type of nested messages/enums. |
253 const TypeInfo* typeinfo_; | 245 const TypeInfo* typeinfo_; |
254 // Whether the TypeInfo object is owned by this class. | 246 // Whether the TypeInfo object is owned by this class. |
255 bool own_typeinfo_; | 247 bool own_typeinfo_; |
256 // google::protobuf::Type of the root message type. | 248 // google::protobuf::Type of the root message type. |
257 const google::protobuf::Type& type_; | 249 const google::protobuf::Type& type_; |
258 // Holds copies of strings passed to RenderString. | 250 // Holds copies of strings passed to RenderString. |
259 std::vector<string*> string_values_; | 251 vector<string*> string_values_; |
260 | 252 |
261 // The current Node. Owned by its parents. | 253 // The current Node. Owned by its parents. |
262 Node* current_; | 254 Node* current_; |
263 // The root Node. | 255 // The root Node. |
264 google::protobuf::scoped_ptr<Node> root_; | 256 google::protobuf::scoped_ptr<Node> root_; |
265 // The stack to hold the path of Nodes from current_ to root_; | 257 // The stack to hold the path of Nodes from current_ to root_; |
266 std::stack<Node*> stack_; | 258 std::stack<Node*> stack_; |
267 | 259 |
268 // Whether to suppress output of empty lists. | |
269 bool suppress_empty_list_; | |
270 | |
271 // Unique Pointer to function for determining whether a field needs to be | 260 // Unique Pointer to function for determining whether a field needs to be |
272 // scrubbed or not. | 261 // scrubbed or not. |
273 FieldScrubCallBackPtr field_scrub_callback_; | 262 FieldScrubCallBackPtr field_scrub_callback_; |
274 | 263 |
275 ObjectWriter* ow_; | 264 ObjectWriter* ow_; |
276 | 265 |
277 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter); | 266 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DefaultValueObjectWriter); |
278 }; | 267 }; |
279 | 268 |
280 } // namespace converter | 269 } // namespace converter |
281 } // namespace util | 270 } // namespace util |
282 } // namespace protobuf | 271 } // namespace protobuf |
283 | 272 |
284 } // namespace google | 273 } // namespace google |
285 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__ | 274 #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_DEFAULT_VALUE_OBJECTWRITER_H__ |
OLD | NEW |