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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // extendee_type, and appends them to output in an undefined | 90 // extendee_type, and appends them to output in an undefined |
91 // order. This method is best-effort: it's not guaranteed that the | 91 // order. This method is best-effort: it's not guaranteed that the |
92 // database will find all extensions, and it's not guaranteed that | 92 // database will find all extensions, and it's not guaranteed that |
93 // FindFileContainingExtension will return true on all of the found | 93 // FindFileContainingExtension will return true on all of the found |
94 // numbers. Returns true if the search was successful, otherwise | 94 // numbers. Returns true if the search was successful, otherwise |
95 // returns false and leaves output unchanged. | 95 // returns false and leaves output unchanged. |
96 // | 96 // |
97 // This method has a default implementation that always returns | 97 // This method has a default implementation that always returns |
98 // false. | 98 // false. |
99 virtual bool FindAllExtensionNumbers(const string& /* extendee_type */, | 99 virtual bool FindAllExtensionNumbers(const string& /* extendee_type */, |
100 std::vector<int>* /* output */) { | 100 vector<int>* /* output */) { |
101 return false; | 101 return false; |
102 } | 102 } |
103 | 103 |
104 | 104 |
105 private: | 105 private: |
106 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorDatabase); | 106 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorDatabase); |
107 }; | 107 }; |
108 | 108 |
109 // A DescriptorDatabase into which you can insert files manually. | 109 // A DescriptorDatabase into which you can insert files manually. |
110 // | 110 // |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 143 |
144 // implements DescriptorDatabase ----------------------------------- | 144 // implements DescriptorDatabase ----------------------------------- |
145 bool FindFileByName(const string& filename, | 145 bool FindFileByName(const string& filename, |
146 FileDescriptorProto* output); | 146 FileDescriptorProto* output); |
147 bool FindFileContainingSymbol(const string& symbol_name, | 147 bool FindFileContainingSymbol(const string& symbol_name, |
148 FileDescriptorProto* output); | 148 FileDescriptorProto* output); |
149 bool FindFileContainingExtension(const string& containing_type, | 149 bool FindFileContainingExtension(const string& containing_type, |
150 int field_number, | 150 int field_number, |
151 FileDescriptorProto* output); | 151 FileDescriptorProto* output); |
152 bool FindAllExtensionNumbers(const string& extendee_type, | 152 bool FindAllExtensionNumbers(const string& extendee_type, |
153 std::vector<int>* output); | 153 vector<int>* output); |
154 | 154 |
155 private: | 155 private: |
156 // So that it can use DescriptorIndex. | 156 // So that it can use DescriptorIndex. |
157 friend class EncodedDescriptorDatabase; | 157 friend class EncodedDescriptorDatabase; |
158 | 158 |
159 // An index mapping file names, symbol names, and extension numbers to | 159 // An index mapping file names, symbol names, and extension numbers to |
160 // some sort of values. | 160 // some sort of values. |
161 template <typename Value> | 161 template <typename Value> |
162 class DescriptorIndex { | 162 class DescriptorIndex { |
163 public: | 163 public: |
164 // Helpers to recursively add particular descriptors and all their contents | 164 // Helpers to recursively add particular descriptors and all their contents |
165 // to the index. | 165 // to the index. |
166 bool AddFile(const FileDescriptorProto& file, | 166 bool AddFile(const FileDescriptorProto& file, |
167 Value value); | 167 Value value); |
168 bool AddSymbol(const string& name, Value value); | 168 bool AddSymbol(const string& name, Value value); |
169 bool AddNestedExtensions(const DescriptorProto& message_type, | 169 bool AddNestedExtensions(const DescriptorProto& message_type, |
170 Value value); | 170 Value value); |
171 bool AddExtension(const FieldDescriptorProto& field, | 171 bool AddExtension(const FieldDescriptorProto& field, |
172 Value value); | 172 Value value); |
173 | 173 |
174 Value FindFile(const string& filename); | 174 Value FindFile(const string& filename); |
175 Value FindSymbol(const string& name); | 175 Value FindSymbol(const string& name); |
176 Value FindExtension(const string& containing_type, int field_number); | 176 Value FindExtension(const string& containing_type, int field_number); |
177 bool FindAllExtensionNumbers(const string& containing_type, | 177 bool FindAllExtensionNumbers(const string& containing_type, |
178 std::vector<int>* output); | 178 vector<int>* output); |
179 | 179 |
180 private: | 180 private: |
181 std::map<string, Value> by_name_; | 181 map<string, Value> by_name_; |
182 std::map<string, Value> by_symbol_; | 182 map<string, Value> by_symbol_; |
183 std::map<std::pair<string, int>, Value> by_extension_; | 183 map<pair<string, int>, Value> by_extension_; |
184 | 184 |
185 // Invariant: The by_symbol_ map does not contain any symbols which are | 185 // Invariant: The by_symbol_ map does not contain any symbols which are |
186 // prefixes of other symbols in the map. For example, "foo.bar" is a | 186 // prefixes of other symbols in the map. For example, "foo.bar" is a |
187 // prefix of "foo.bar.baz" (but is not a prefix of "foo.barbaz"). | 187 // prefix of "foo.bar.baz" (but is not a prefix of "foo.barbaz"). |
188 // | 188 // |
189 // This invariant is important because it means that given a symbol name, | 189 // This invariant is important because it means that given a symbol name, |
190 // we can find a key in the map which is a prefix of the symbol in O(lg n) | 190 // we can find a key in the map which is a prefix of the symbol in O(lg n) |
191 // time, and we know that there is at most one such key. | 191 // time, and we know that there is at most one such key. |
192 // | 192 // |
193 // The prefix lookup algorithm works like so: | 193 // The prefix lookup algorithm works like so: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 // 9) Therefore, the found key must match the match key. | 228 // 9) Therefore, the found key must match the match key. |
229 // | 229 // |
230 // The above proof assumes the match key exists. In the case that the | 230 // The above proof assumes the match key exists. In the case that the |
231 // match key does not exist, then step (1) will return some other symbol. | 231 // match key does not exist, then step (1) will return some other symbol. |
232 // That symbol cannot be a super-symbol of the search key since if it were, | 232 // That symbol cannot be a super-symbol of the search key since if it were, |
233 // then it would be a match, and we're assuming the match key doesn't exist. | 233 // then it would be a match, and we're assuming the match key doesn't exist. |
234 // Therefore, step 2 will correctly return no match. | 234 // Therefore, step 2 will correctly return no match. |
235 | 235 |
236 // Find the last entry in the by_symbol_ map whose key is less than or | 236 // Find the last entry in the by_symbol_ map whose key is less than or |
237 // equal to the given name. | 237 // equal to the given name. |
238 typename std::map<string, Value>::iterator FindLastLessOrEqual( | 238 typename map<string, Value>::iterator FindLastLessOrEqual( |
239 const string& name); | 239 const string& name); |
240 | 240 |
241 // True if either the arguments are equal or super_symbol identifies a | 241 // True if either the arguments are equal or super_symbol identifies a |
242 // parent symbol of sub_symbol (e.g. "foo.bar" is a parent of | 242 // parent symbol of sub_symbol (e.g. "foo.bar" is a parent of |
243 // "foo.bar.baz", but not a parent of "foo.barbaz"). | 243 // "foo.bar.baz", but not a parent of "foo.barbaz"). |
244 bool IsSubSymbol(const string& sub_symbol, const string& super_symbol); | 244 bool IsSubSymbol(const string& sub_symbol, const string& super_symbol); |
245 | 245 |
246 // Returns true if and only if all characters in the name are alphanumerics, | 246 // Returns true if and only if all characters in the name are alphanumerics, |
247 // underscores, or periods. | 247 // underscores, or periods. |
248 bool ValidateSymbolName(const string& name); | 248 bool ValidateSymbolName(const string& name); |
249 }; | 249 }; |
250 | 250 |
251 | 251 |
252 DescriptorIndex<const FileDescriptorProto*> index_; | 252 DescriptorIndex<const FileDescriptorProto*> index_; |
253 std::vector<const FileDescriptorProto*> files_to_delete_; | 253 vector<const FileDescriptorProto*> files_to_delete_; |
254 | 254 |
255 // If file is non-NULL, copy it into *output and return true, otherwise | 255 // If file is non-NULL, copy it into *output and return true, otherwise |
256 // return false. | 256 // return false. |
257 bool MaybeCopy(const FileDescriptorProto* file, | 257 bool MaybeCopy(const FileDescriptorProto* file, |
258 FileDescriptorProto* output); | 258 FileDescriptorProto* output); |
259 | 259 |
260 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleDescriptorDatabase); | 260 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleDescriptorDatabase); |
261 }; | 261 }; |
262 | 262 |
263 // Very similar to SimpleDescriptorDatabase, but stores all the descriptors | 263 // Very similar to SimpleDescriptorDatabase, but stores all the descriptors |
(...skipping 24 matching lines...) Expand all Loading... |
288 | 288 |
289 // implements DescriptorDatabase ----------------------------------- | 289 // implements DescriptorDatabase ----------------------------------- |
290 bool FindFileByName(const string& filename, | 290 bool FindFileByName(const string& filename, |
291 FileDescriptorProto* output); | 291 FileDescriptorProto* output); |
292 bool FindFileContainingSymbol(const string& symbol_name, | 292 bool FindFileContainingSymbol(const string& symbol_name, |
293 FileDescriptorProto* output); | 293 FileDescriptorProto* output); |
294 bool FindFileContainingExtension(const string& containing_type, | 294 bool FindFileContainingExtension(const string& containing_type, |
295 int field_number, | 295 int field_number, |
296 FileDescriptorProto* output); | 296 FileDescriptorProto* output); |
297 bool FindAllExtensionNumbers(const string& extendee_type, | 297 bool FindAllExtensionNumbers(const string& extendee_type, |
298 std::vector<int>* output); | 298 vector<int>* output); |
299 | 299 |
300 private: | 300 private: |
301 SimpleDescriptorDatabase::DescriptorIndex<std::pair<const void*, int> > | 301 SimpleDescriptorDatabase::DescriptorIndex<pair<const void*, int> > index_; |
302 index_; | 302 vector<void*> files_to_delete_; |
303 std::vector<void*> files_to_delete_; | |
304 | 303 |
305 // If encoded_file.first is non-NULL, parse the data into *output and return | 304 // If encoded_file.first is non-NULL, parse the data into *output and return |
306 // true, otherwise return false. | 305 // true, otherwise return false. |
307 bool MaybeParse(std::pair<const void*, int> encoded_file, | 306 bool MaybeParse(pair<const void*, int> encoded_file, |
308 FileDescriptorProto* output); | 307 FileDescriptorProto* output); |
309 | 308 |
310 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EncodedDescriptorDatabase); | 309 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EncodedDescriptorDatabase); |
311 }; | 310 }; |
312 | 311 |
313 // A DescriptorDatabase that fetches files from a given pool. | 312 // A DescriptorDatabase that fetches files from a given pool. |
314 class LIBPROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase { | 313 class LIBPROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase { |
315 public: | 314 public: |
316 explicit DescriptorPoolDatabase(const DescriptorPool& pool); | 315 explicit DescriptorPoolDatabase(const DescriptorPool& pool); |
317 ~DescriptorPoolDatabase(); | 316 ~DescriptorPoolDatabase(); |
318 | 317 |
319 // implements DescriptorDatabase ----------------------------------- | 318 // implements DescriptorDatabase ----------------------------------- |
320 bool FindFileByName(const string& filename, | 319 bool FindFileByName(const string& filename, |
321 FileDescriptorProto* output); | 320 FileDescriptorProto* output); |
322 bool FindFileContainingSymbol(const string& symbol_name, | 321 bool FindFileContainingSymbol(const string& symbol_name, |
323 FileDescriptorProto* output); | 322 FileDescriptorProto* output); |
324 bool FindFileContainingExtension(const string& containing_type, | 323 bool FindFileContainingExtension(const string& containing_type, |
325 int field_number, | 324 int field_number, |
326 FileDescriptorProto* output); | 325 FileDescriptorProto* output); |
327 bool FindAllExtensionNumbers(const string& extendee_type, | 326 bool FindAllExtensionNumbers(const string& extendee_type, |
328 std::vector<int>* output); | 327 vector<int>* output); |
329 | 328 |
330 private: | 329 private: |
331 const DescriptorPool& pool_; | 330 const DescriptorPool& pool_; |
332 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPoolDatabase); | 331 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPoolDatabase); |
333 }; | 332 }; |
334 | 333 |
335 // A DescriptorDatabase that wraps two or more others. It first searches the | 334 // A DescriptorDatabase that wraps two or more others. It first searches the |
336 // first database and, if that fails, tries the second, and so on. | 335 // first database and, if that fails, tries the second, and so on. |
337 class LIBPROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { | 336 class LIBPROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase { |
338 public: | 337 public: |
339 // Merge just two databases. The sources remain property of the caller. | 338 // Merge just two databases. The sources remain property of the caller. |
340 MergedDescriptorDatabase(DescriptorDatabase* source1, | 339 MergedDescriptorDatabase(DescriptorDatabase* source1, |
341 DescriptorDatabase* source2); | 340 DescriptorDatabase* source2); |
342 // Merge more than two databases. The sources remain property of the caller. | 341 // Merge more than two databases. The sources remain property of the caller. |
343 // The vector may be deleted after the constructor returns but the | 342 // The vector may be deleted after the constructor returns but the |
344 // DescriptorDatabases need to stick around. | 343 // DescriptorDatabases need to stick around. |
345 explicit MergedDescriptorDatabase( | 344 explicit MergedDescriptorDatabase(const vector<DescriptorDatabase*>& sources); |
346 const std::vector<DescriptorDatabase*>& sources); | |
347 ~MergedDescriptorDatabase(); | 345 ~MergedDescriptorDatabase(); |
348 | 346 |
349 // implements DescriptorDatabase ----------------------------------- | 347 // implements DescriptorDatabase ----------------------------------- |
350 bool FindFileByName(const string& filename, | 348 bool FindFileByName(const string& filename, |
351 FileDescriptorProto* output); | 349 FileDescriptorProto* output); |
352 bool FindFileContainingSymbol(const string& symbol_name, | 350 bool FindFileContainingSymbol(const string& symbol_name, |
353 FileDescriptorProto* output); | 351 FileDescriptorProto* output); |
354 bool FindFileContainingExtension(const string& containing_type, | 352 bool FindFileContainingExtension(const string& containing_type, |
355 int field_number, | 353 int field_number, |
356 FileDescriptorProto* output); | 354 FileDescriptorProto* output); |
357 // Merges the results of calling all databases. Returns true iff any | 355 // Merges the results of calling all databases. Returns true iff any |
358 // of the databases returned true. | 356 // of the databases returned true. |
359 bool FindAllExtensionNumbers(const string& extendee_type, | 357 bool FindAllExtensionNumbers(const string& extendee_type, |
360 std::vector<int>* output); | 358 vector<int>* output); |
361 | 359 |
362 | 360 |
363 private: | 361 private: |
364 std::vector<DescriptorDatabase*> sources_; | 362 vector<DescriptorDatabase*> sources_; |
365 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MergedDescriptorDatabase); | 363 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MergedDescriptorDatabase); |
366 }; | 364 }; |
367 | 365 |
368 } // namespace protobuf | 366 } // namespace protobuf |
369 | 367 |
370 } // namespace google | 368 } // namespace google |
371 #endif // GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__ | 369 #endif // GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__ |
OLD | NEW |