诸暨麻将添加redis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

187 lines
6.2 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. #include <google/protobuf/compiler/plugin.h>
  32. #include <iostream>
  33. #include <set>
  34. #ifdef _WIN32
  35. #include <fcntl.h>
  36. #else
  37. #include <unistd.h>
  38. #endif
  39. #include <google/protobuf/stubs/logging.h>
  40. #include <google/protobuf/stubs/common.h>
  41. #include <google/protobuf/compiler/plugin.pb.h>
  42. #include <google/protobuf/compiler/code_generator.h>
  43. #include <google/protobuf/io/zero_copy_stream_impl.h>
  44. #include <google/protobuf/descriptor.h>
  45. #include <google/protobuf/io/io_win32.h>
  46. namespace google {
  47. namespace protobuf {
  48. namespace compiler {
  49. #if defined(_WIN32)
  50. // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
  51. // them like we do below.
  52. using google::protobuf::io::win32::setmode;
  53. #endif
  54. class GeneratorResponseContext : public GeneratorContext {
  55. public:
  56. GeneratorResponseContext(
  57. const Version& compiler_version, CodeGeneratorResponse* response,
  58. const std::vector<const FileDescriptor*>& parsed_files)
  59. : compiler_version_(compiler_version),
  60. response_(response),
  61. parsed_files_(parsed_files) {}
  62. virtual ~GeneratorResponseContext() {}
  63. // implements GeneratorContext --------------------------------------
  64. virtual io::ZeroCopyOutputStream* Open(const std::string& filename) {
  65. CodeGeneratorResponse::File* file = response_->add_file();
  66. file->set_name(filename);
  67. return new io::StringOutputStream(file->mutable_content());
  68. }
  69. virtual io::ZeroCopyOutputStream* OpenForInsert(
  70. const std::string& filename, const std::string& insertion_point) {
  71. CodeGeneratorResponse::File* file = response_->add_file();
  72. file->set_name(filename);
  73. file->set_insertion_point(insertion_point);
  74. return new io::StringOutputStream(file->mutable_content());
  75. }
  76. void ListParsedFiles(std::vector<const FileDescriptor*>* output) {
  77. *output = parsed_files_;
  78. }
  79. void GetCompilerVersion(Version* version) const {
  80. *version = compiler_version_;
  81. }
  82. private:
  83. Version compiler_version_;
  84. CodeGeneratorResponse* response_;
  85. const std::vector<const FileDescriptor*>& parsed_files_;
  86. };
  87. bool GenerateCode(const CodeGeneratorRequest& request,
  88. const CodeGenerator& generator,
  89. CodeGeneratorResponse* response, std::string* error_msg) {
  90. DescriptorPool pool;
  91. for (int i = 0; i < request.proto_file_size(); i++) {
  92. const FileDescriptor* file = pool.BuildFile(request.proto_file(i));
  93. if (file == NULL) {
  94. // BuildFile() already wrote an error message.
  95. return false;
  96. }
  97. }
  98. std::vector<const FileDescriptor*> parsed_files;
  99. for (int i = 0; i < request.file_to_generate_size(); i++) {
  100. parsed_files.push_back(pool.FindFileByName(request.file_to_generate(i)));
  101. if (parsed_files.back() == NULL) {
  102. *error_msg =
  103. "protoc asked plugin to generate a file but "
  104. "did not provide a descriptor for the file: " +
  105. request.file_to_generate(i);
  106. return false;
  107. }
  108. }
  109. GeneratorResponseContext context(request.compiler_version(), response,
  110. parsed_files);
  111. std::string error;
  112. bool succeeded = generator.GenerateAll(parsed_files, request.parameter(),
  113. &context, &error);
  114. if (!succeeded && error.empty()) {
  115. error =
  116. "Code generator returned false but provided no error "
  117. "description.";
  118. }
  119. if (!error.empty()) {
  120. response->set_error(error);
  121. }
  122. return true;
  123. }
  124. int PluginMain(int argc, char* argv[], const CodeGenerator* generator) {
  125. if (argc > 1) {
  126. std::cerr << argv[0] << ": Unknown option: " << argv[1] << std::endl;
  127. return 1;
  128. }
  129. #ifdef _WIN32
  130. setmode(STDIN_FILENO, _O_BINARY);
  131. setmode(STDOUT_FILENO, _O_BINARY);
  132. #endif
  133. CodeGeneratorRequest request;
  134. if (!request.ParseFromFileDescriptor(STDIN_FILENO)) {
  135. std::cerr << argv[0] << ": protoc sent unparseable request to plugin."
  136. << std::endl;
  137. return 1;
  138. }
  139. std::string error_msg;
  140. CodeGeneratorResponse response;
  141. if (GenerateCode(request, *generator, &response, &error_msg)) {
  142. if (!response.SerializeToFileDescriptor(STDOUT_FILENO)) {
  143. std::cerr << argv[0] << ": Error writing to stdout." << std::endl;
  144. return 1;
  145. }
  146. } else {
  147. if (!error_msg.empty()) {
  148. std::cerr << argv[0] << ": " << error_msg << std::endl;
  149. }
  150. return 1;
  151. }
  152. return 0;
  153. }
  154. } // namespace compiler
  155. } // namespace protobuf
  156. } // namespace google