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