诸暨麻将添加redis
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

408 Zeilen
19 KiB

  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Interface for manipulating databases of descriptors.
  35. #ifndef GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__
  36. #define GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__
  37. #include <map>
  38. #include <string>
  39. #include <utility>
  40. #include <vector>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/descriptor.h>
  43. #include <google/protobuf/port_def.inc>
  44. #ifdef SWIG
  45. #error "You cannot SWIG proto headers"
  46. #endif
  47. namespace google {
  48. namespace protobuf {
  49. // Defined in this file.
  50. class DescriptorDatabase;
  51. class SimpleDescriptorDatabase;
  52. class EncodedDescriptorDatabase;
  53. class DescriptorPoolDatabase;
  54. class MergedDescriptorDatabase;
  55. // Abstract interface for a database of descriptors.
  56. //
  57. // This is useful if you want to create a DescriptorPool which loads
  58. // descriptors on-demand from some sort of large database. If the database
  59. // is large, it may be inefficient to enumerate every .proto file inside it
  60. // calling DescriptorPool::BuildFile() for each one. Instead, a DescriptorPool
  61. // can be created which wraps a DescriptorDatabase and only builds particular
  62. // descriptors when they are needed.
  63. class PROTOBUF_EXPORT DescriptorDatabase {
  64. public:
  65. inline DescriptorDatabase() {}
  66. virtual ~DescriptorDatabase();
  67. // Find a file by file name. Fills in in *output and returns true if found.
  68. // Otherwise, returns false, leaving the contents of *output undefined.
  69. virtual bool FindFileByName(const std::string& filename,
  70. FileDescriptorProto* output) = 0;
  71. // Find the file that declares the given fully-qualified symbol name.
  72. // If found, fills in *output and returns true, otherwise returns false
  73. // and leaves *output undefined.
  74. virtual bool FindFileContainingSymbol(const std::string& symbol_name,
  75. FileDescriptorProto* output) = 0;
  76. // Find the file which defines an extension extending the given message type
  77. // with the given field number. If found, fills in *output and returns true,
  78. // otherwise returns false and leaves *output undefined. containing_type
  79. // must be a fully-qualified type name.
  80. virtual bool FindFileContainingExtension(const std::string& containing_type,
  81. int field_number,
  82. FileDescriptorProto* output) = 0;
  83. // Finds the tag numbers used by all known extensions of
  84. // extendee_type, and appends them to output in an undefined
  85. // order. This method is best-effort: it's not guaranteed that the
  86. // database will find all extensions, and it's not guaranteed that
  87. // FindFileContainingExtension will return true on all of the found
  88. // numbers. Returns true if the search was successful, otherwise
  89. // returns false and leaves output unchanged.
  90. //
  91. // This method has a default implementation that always returns
  92. // false.
  93. virtual bool FindAllExtensionNumbers(const std::string& /* extendee_type */,
  94. std::vector<int>* /* output */) {
  95. return false;
  96. }
  97. // Finds the file names and appends them to the output in an
  98. // undefined order. This method is best-effort: it's not guaranteed that the
  99. // database will find all files. Returns true if the database supports
  100. // searching all file names, otherwise returns false and leaves output
  101. // unchanged.
  102. //
  103. // This method has a default implementation that always returns
  104. // false.
  105. virtual bool FindAllFileNames(std::vector<std::string>* /*output*/) {
  106. return false;
  107. }
  108. // Finds the package names and appends them to the output in an
  109. // undefined order. This method is best-effort: it's not guaranteed that the
  110. // database will find all packages. Returns true if the database supports
  111. // searching all package names, otherwise returns false and leaves output
  112. // unchanged.
  113. bool FindAllPackageNames(std::vector<std::string>* output);
  114. // Finds the message names and appends them to the output in an
  115. // undefined order. This method is best-effort: it's not guaranteed that the
  116. // database will find all messages. Returns true if the database supports
  117. // searching all message names, otherwise returns false and leaves output
  118. // unchanged.
  119. bool FindAllMessageNames(std::vector<std::string>* output);
  120. private:
  121. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorDatabase);
  122. };
  123. // A DescriptorDatabase into which you can insert files manually.
  124. //
  125. // FindFileContainingSymbol() is fully-implemented. When you add a file, its
  126. // symbols will be indexed for this purpose. Note that the implementation
  127. // may return false positives, but only if it isn't possible for the symbol
  128. // to be defined in any other file. In particular, if a file defines a symbol
  129. // "Foo", then searching for "Foo.[anything]" will match that file. This way,
  130. // the database does not need to aggressively index all children of a symbol.
  131. //
  132. // FindFileContainingExtension() is mostly-implemented. It works if and only
  133. // if the original FieldDescriptorProto defining the extension has a
  134. // fully-qualified type name in its "extendee" field (i.e. starts with a '.').
  135. // If the extendee is a relative name, SimpleDescriptorDatabase will not
  136. // attempt to resolve the type, so it will not know what type the extension is
  137. // extending. Therefore, calling FindFileContainingExtension() with the
  138. // extension's containing type will never actually find that extension. Note
  139. // that this is an unlikely problem, as all FileDescriptorProtos created by the
  140. // protocol compiler (as well as ones created by calling
  141. // FileDescriptor::CopyTo()) will always use fully-qualified names for all
  142. // types. You only need to worry if you are constructing FileDescriptorProtos
  143. // yourself, or are calling compiler::Parser directly.
  144. class PROTOBUF_EXPORT SimpleDescriptorDatabase : public DescriptorDatabase {
  145. public:
  146. SimpleDescriptorDatabase();
  147. ~SimpleDescriptorDatabase() override;
  148. // Adds the FileDescriptorProto to the database, making a copy. The object
  149. // can be deleted after Add() returns. Returns false if the file conflicted
  150. // with a file already in the database, in which case an error will have
  151. // been written to GOOGLE_LOG(ERROR).
  152. bool Add(const FileDescriptorProto& file);
  153. // Adds the FileDescriptorProto to the database and takes ownership of it.
  154. bool AddAndOwn(const FileDescriptorProto* file);
  155. // implements DescriptorDatabase -----------------------------------
  156. bool FindFileByName(const std::string& filename,
  157. FileDescriptorProto* output) override;
  158. bool FindFileContainingSymbol(const std::string& symbol_name,
  159. FileDescriptorProto* output) override;
  160. bool FindFileContainingExtension(const std::string& containing_type,
  161. int field_number,
  162. FileDescriptorProto* output) override;
  163. bool FindAllExtensionNumbers(const std::string& extendee_type,
  164. std::vector<int>* output) override;
  165. bool FindAllFileNames(std::vector<std::string>* output) override;
  166. private:
  167. // So that it can use DescriptorIndex.
  168. friend class EncodedDescriptorDatabase;
  169. // An index mapping file names, symbol names, and extension numbers to
  170. // some sort of values.
  171. template <typename Value>
  172. class DescriptorIndex {
  173. public:
  174. // Helpers to recursively add particular descriptors and all their contents
  175. // to the index.
  176. bool AddFile(const FileDescriptorProto& file, Value value);
  177. bool AddSymbol(const std::string& name, Value value);
  178. bool AddNestedExtensions(const std::string& filename,
  179. const DescriptorProto& message_type, Value value);
  180. bool AddExtension(const std::string& filename,
  181. const FieldDescriptorProto& field, Value value);
  182. Value FindFile(const std::string& filename);
  183. Value FindSymbol(const std::string& name);
  184. Value FindExtension(const std::string& containing_type, int field_number);
  185. bool FindAllExtensionNumbers(const std::string& containing_type,
  186. std::vector<int>* output);
  187. void FindAllFileNames(std::vector<std::string>* output);
  188. private:
  189. std::map<std::string, Value> by_name_;
  190. std::map<std::string, Value> by_symbol_;
  191. std::map<std::pair<std::string, int>, Value> by_extension_;
  192. // Invariant: The by_symbol_ map does not contain any symbols which are
  193. // prefixes of other symbols in the map. For example, "foo.bar" is a
  194. // prefix of "foo.bar.baz" (but is not a prefix of "foo.barbaz").
  195. //
  196. // This invariant is important because it means that given a symbol name,
  197. // we can find a key in the map which is a prefix of the symbol in O(lg n)
  198. // time, and we know that there is at most one such key.
  199. //
  200. // The prefix lookup algorithm works like so:
  201. // 1) Find the last key in the map which is less than or equal to the
  202. // search key.
  203. // 2) If the found key is a prefix of the search key, then return it.
  204. // Otherwise, there is no match.
  205. //
  206. // I am sure this algorithm has been described elsewhere, but since I
  207. // wasn't able to find it quickly I will instead prove that it works
  208. // myself. The key to the algorithm is that if a match exists, step (1)
  209. // will find it. Proof:
  210. // 1) Define the "search key" to be the key we are looking for, the "found
  211. // key" to be the key found in step (1), and the "match key" to be the
  212. // key which actually matches the search key (i.e. the key we're trying
  213. // to find).
  214. // 2) The found key must be less than or equal to the search key by
  215. // definition.
  216. // 3) The match key must also be less than or equal to the search key
  217. // (because it is a prefix).
  218. // 4) The match key cannot be greater than the found key, because if it
  219. // were, then step (1) of the algorithm would have returned the match
  220. // key instead (since it finds the *greatest* key which is less than or
  221. // equal to the search key).
  222. // 5) Therefore, the found key must be between the match key and the search
  223. // key, inclusive.
  224. // 6) Since the search key must be a sub-symbol of the match key, if it is
  225. // not equal to the match key, then search_key[match_key.size()] must
  226. // be '.'.
  227. // 7) Since '.' sorts before any other character that is valid in a symbol
  228. // name, then if the found key is not equal to the match key, then
  229. // found_key[match_key.size()] must also be '.', because any other value
  230. // would make it sort after the search key.
  231. // 8) Therefore, if the found key is not equal to the match key, then the
  232. // found key must be a sub-symbol of the match key. However, this would
  233. // contradict our map invariant which says that no symbol in the map is
  234. // a sub-symbol of any other.
  235. // 9) Therefore, the found key must match the match key.
  236. //
  237. // The above proof assumes the match key exists. In the case that the
  238. // match key does not exist, then step (1) will return some other symbol.
  239. // That symbol cannot be a super-symbol of the search key since if it were,
  240. // then it would be a match, and we're assuming the match key doesn't exist.
  241. // Therefore, step 2 will correctly return no match.
  242. // Find the last entry in the by_symbol_ map whose key is less than or
  243. // equal to the given name.
  244. typename std::map<std::string, Value>::iterator FindLastLessOrEqual(
  245. const std::string& name);
  246. // True if either the arguments are equal or super_symbol identifies a
  247. // parent symbol of sub_symbol (e.g. "foo.bar" is a parent of
  248. // "foo.bar.baz", but not a parent of "foo.barbaz").
  249. bool IsSubSymbol(const std::string& sub_symbol,
  250. const std::string& super_symbol);
  251. // Returns true if and only if all characters in the name are alphanumerics,
  252. // underscores, or periods.
  253. bool ValidateSymbolName(const std::string& name);
  254. };
  255. DescriptorIndex<const FileDescriptorProto*> index_;
  256. std::vector<std::unique_ptr<const FileDescriptorProto>> files_to_delete_;
  257. // If file is non-NULL, copy it into *output and return true, otherwise
  258. // return false.
  259. bool MaybeCopy(const FileDescriptorProto* file, FileDescriptorProto* output);
  260. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(SimpleDescriptorDatabase);
  261. };
  262. // Very similar to SimpleDescriptorDatabase, but stores all the descriptors
  263. // as raw bytes and generally tries to use as little memory as possible.
  264. //
  265. // The same caveats regarding FindFileContainingExtension() apply as with
  266. // SimpleDescriptorDatabase.
  267. class PROTOBUF_EXPORT EncodedDescriptorDatabase : public DescriptorDatabase {
  268. public:
  269. EncodedDescriptorDatabase();
  270. ~EncodedDescriptorDatabase() override;
  271. // Adds the FileDescriptorProto to the database. The descriptor is provided
  272. // in encoded form. The database does not make a copy of the bytes, nor
  273. // does it take ownership; it's up to the caller to make sure the bytes
  274. // remain valid for the life of the database. Returns false and logs an error
  275. // if the bytes are not a valid FileDescriptorProto or if the file conflicted
  276. // with a file already in the database.
  277. bool Add(const void* encoded_file_descriptor, int size);
  278. // Like Add(), but makes a copy of the data, so that the caller does not
  279. // need to keep it around.
  280. bool AddCopy(const void* encoded_file_descriptor, int size);
  281. // Like FindFileContainingSymbol but returns only the name of the file.
  282. bool FindNameOfFileContainingSymbol(const std::string& symbol_name,
  283. std::string* output);
  284. // implements DescriptorDatabase -----------------------------------
  285. bool FindFileByName(const std::string& filename,
  286. FileDescriptorProto* output) override;
  287. bool FindFileContainingSymbol(const std::string& symbol_name,
  288. FileDescriptorProto* output) override;
  289. bool FindFileContainingExtension(const std::string& containing_type,
  290. int field_number,
  291. FileDescriptorProto* output) override;
  292. bool FindAllExtensionNumbers(const std::string& extendee_type,
  293. std::vector<int>* output) override;
  294. bool FindAllFileNames(std::vector<std::string>* output) override;
  295. private:
  296. SimpleDescriptorDatabase::DescriptorIndex<std::pair<const void*, int> >
  297. index_;
  298. std::vector<void*> files_to_delete_;
  299. // If encoded_file.first is non-NULL, parse the data into *output and return
  300. // true, otherwise return false.
  301. bool MaybeParse(std::pair<const void*, int> encoded_file,
  302. FileDescriptorProto* output);
  303. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EncodedDescriptorDatabase);
  304. };
  305. // A DescriptorDatabase that fetches files from a given pool.
  306. class PROTOBUF_EXPORT DescriptorPoolDatabase : public DescriptorDatabase {
  307. public:
  308. explicit DescriptorPoolDatabase(const DescriptorPool& pool);
  309. ~DescriptorPoolDatabase() override;
  310. // implements DescriptorDatabase -----------------------------------
  311. bool FindFileByName(const std::string& filename,
  312. FileDescriptorProto* output) override;
  313. bool FindFileContainingSymbol(const std::string& symbol_name,
  314. FileDescriptorProto* output) override;
  315. bool FindFileContainingExtension(const std::string& containing_type,
  316. int field_number,
  317. FileDescriptorProto* output) override;
  318. bool FindAllExtensionNumbers(const std::string& extendee_type,
  319. std::vector<int>* output) override;
  320. private:
  321. const DescriptorPool& pool_;
  322. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPoolDatabase);
  323. };
  324. // A DescriptorDatabase that wraps two or more others. It first searches the
  325. // first database and, if that fails, tries the second, and so on.
  326. class PROTOBUF_EXPORT MergedDescriptorDatabase : public DescriptorDatabase {
  327. public:
  328. // Merge just two databases. The sources remain property of the caller.
  329. MergedDescriptorDatabase(DescriptorDatabase* source1,
  330. DescriptorDatabase* source2);
  331. // Merge more than two databases. The sources remain property of the caller.
  332. // The vector may be deleted after the constructor returns but the
  333. // DescriptorDatabases need to stick around.
  334. explicit MergedDescriptorDatabase(
  335. const std::vector<DescriptorDatabase*>& sources);
  336. ~MergedDescriptorDatabase() override;
  337. // implements DescriptorDatabase -----------------------------------
  338. bool FindFileByName(const std::string& filename,
  339. FileDescriptorProto* output) override;
  340. bool FindFileContainingSymbol(const std::string& symbol_name,
  341. FileDescriptorProto* output) override;
  342. bool FindFileContainingExtension(const std::string& containing_type,
  343. int field_number,
  344. FileDescriptorProto* output) override;
  345. // Merges the results of calling all databases. Returns true iff any
  346. // of the databases returned true.
  347. bool FindAllExtensionNumbers(const std::string& extendee_type,
  348. std::vector<int>* output) override;
  349. private:
  350. std::vector<DescriptorDatabase*> sources_;
  351. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MergedDescriptorDatabase);
  352. };
  353. } // namespace protobuf
  354. } // namespace google
  355. #include <google/protobuf/port_undef.inc>
  356. #endif // GOOGLE_PROTOBUF_DESCRIPTOR_DATABASE_H__