CppPlugin.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Generates C++ tars service interface out of Protobuf IDL.
  2. //
  3. // This is a Proto2 compiler plugin. See net/proto2/compiler/proto/plugin.proto
  4. // and net/proto2/compiler/public/plugin.h for more information on plugins.
  5. #include <iostream>
  6. #include <google/protobuf/compiler/plugin.h>
  7. #include <google/protobuf/compiler/plugin.pb.h>
  8. #include <google/protobuf/descriptor.h>
  9. #include "CppPlugin.h"
  10. #include "CppGenCallback.h"
  11. #include "CppGenProxy.h"
  12. #include "CppGenServant.h"
  13. bool CppTarsGenerator::Generate(const google::protobuf::FileDescriptor *file,
  14. const std::string &parameter,
  15. google::protobuf::compiler::GeneratorContext *context,
  16. std::string *error) const {
  17. if (!_CheckFile(file, error)) {
  18. return false;
  19. }
  20. std::string content = _GenHeader(ProtoFileBaseName(file->name()));
  21. // namespace
  22. content += _GenNamespaceBegin(file->package());
  23. const int indent = 1;
  24. content += LineFeed(indent);
  25. for (int i = 0; i < file->service_count(); ++i) {
  26. content += GenPrxCallback(file->service(i), indent);
  27. content += GenPrx(file->service(i), indent);
  28. content += GenServant(file->service(i), indent);
  29. }
  30. content += _GenNamespaceEnd(file->package());
  31. // gen response to parent
  32. const std::string& outputFile = ProtoFileBaseName(file->name()) + ".tars.h";
  33. std::string output = _GenResponse(outputFile, content);
  34. std::cout << output;
  35. return true;
  36. }
  37. std::string
  38. CppTarsGenerator::_GenResponse(const std::string& filename,
  39. const std::string& content) {
  40. google::protobuf::compiler::CodeGeneratorResponse response;
  41. auto f = response.add_file();
  42. f->set_name(filename);
  43. f->set_content(content);
  44. // output to parent
  45. std::string outbytes;
  46. response.SerializeToString(&outbytes);
  47. return outbytes;
  48. }
  49. bool CppTarsGenerator::_CheckFile(const google::protobuf::FileDescriptor* file,
  50. std::string* error) const {
  51. if (file->package().empty()) {
  52. error->append("package name is missed.");
  53. return false;
  54. }
  55. return true;
  56. }
  57. std::string CppTarsGenerator::_GenHeader(const std::string& name) {
  58. std::string content;
  59. content.reserve(8 * 1024);
  60. content += kDeclaration;
  61. content += "#pragma once\n\n";
  62. content += "#include <map>\n";
  63. content += "#include <string>\n";
  64. content += "#include <vector>\n";
  65. content += "#include \"servant/ServantProxy.h\"\n";
  66. content += "#include \"servant/Servant.h\"\n";
  67. // include pb file
  68. content += "#include \"" + name + ".pb.h\"\n";
  69. return content;
  70. }
  71. std::string CppTarsGenerator::_GenNamespaceBegin(const std::string& ns) {
  72. std::string content;
  73. content += "\n\nnamespace ";
  74. content += ns + "\n{";
  75. return content;
  76. }
  77. std::string CppTarsGenerator::_GenNamespaceEnd(const std::string& ns) {
  78. return std::string("\n} // end namespace " + ns + "\n\n");
  79. }
  80. int main(int argc, char *argv[]) {
  81. CppTarsGenerator generator;
  82. return google::protobuf::compiler::PluginMain(argc, argv, &generator);
  83. }