generator.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. Copyright (c) 2020 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_GENERATOR_H__
  14. #define __RPC_GENERATOR_H__
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <vector>
  19. #include <map>
  20. #include <list>
  21. #include <string>
  22. #include <algorithm>
  23. #include "printer.h"
  24. #include "parser.h"
  25. struct GeneratorParams
  26. {
  27. const char *out_dir;
  28. bool generate_skeleton;
  29. std::string idl_file;
  30. std::string input_dir;
  31. GeneratorParams() : out_dir(NULL), generate_skeleton(true) { }
  32. };
  33. class Generator
  34. {
  35. public:
  36. Generator(bool is_thrift):
  37. parser(is_thrift),
  38. printer(is_thrift)
  39. {
  40. this->suffix = ".srpc.";
  41. this->thrift_suffix = ".thrift.";
  42. this->skeleton_suffix = ".skeleton.";
  43. this->is_thrift = is_thrift;
  44. }
  45. bool generate(struct GeneratorParams& params);
  46. protected:
  47. virtual bool generate_server_cpp_file(const idl_info& cur_info,
  48. const std::string& idle_file_name);
  49. virtual bool generate_client_cpp_file(const idl_info& cur_info,
  50. const std::string& idle_file_name);
  51. std::string server_cpp_file;
  52. std::string client_cpp_file;
  53. private:
  54. bool generate_header(idl_info& cur_info, struct GeneratorParams& params);
  55. void generate_skeleton(const std::string& idl_file);
  56. bool generate_srpc_file(const idl_info& cur_info);
  57. bool generate_thrift_type_file(idl_info& cur_info);
  58. void thrift_replace_include(const idl_info& cur_info,
  59. std::vector<rpc_param>& params);
  60. bool init_file_names(const std::string& idl_file, const char *out_dir);
  61. Parser parser;
  62. Printer printer;
  63. idl_info info;
  64. std::string out_dir;
  65. std::string suffix;
  66. std::string thrift_suffix;
  67. std::string skeleton_suffix;
  68. std::string prefix;
  69. std::string srpc_file;
  70. std::string thrift_type_file;
  71. bool is_thrift;
  72. };
  73. #endif