1 // Copyright 2020 Google LLC 2 // 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 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #ifndef GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_SERVICE_CODE_GENERATOR_H 15 #define GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_SERVICE_CODE_GENERATOR_H 16 17 #include "google/cloud/status.h" 18 #include "generator/internal/codegen_utils.h" 19 #include "generator/internal/descriptor_utils.h" 20 #include "generator/internal/generator_interface.h" 21 #include "generator/internal/printer.h" 22 #include <google/protobuf/descriptor.h> 23 24 namespace google { 25 namespace cloud { 26 namespace generator_internal { 27 28 class ServiceCodeGenerator : public GeneratorInterface { 29 public: 30 ServiceCodeGenerator( 31 std::string const& header_path_key, std::string const& cc_path_key, 32 google::protobuf::ServiceDescriptor const* service_descriptor, 33 VarsDictionary service_vars, 34 std::map<std::string, VarsDictionary> service_method_vars, 35 google::protobuf::compiler::GeneratorContext* context); 36 37 ServiceCodeGenerator( 38 std::string const& header_path_key, 39 google::protobuf::ServiceDescriptor const* service_descriptor, 40 VarsDictionary service_vars, 41 std::map<std::string, VarsDictionary> service_method_vars, 42 google::protobuf::compiler::GeneratorContext* context); 43 44 ~ServiceCodeGenerator() override = default; 45 46 ServiceCodeGenerator(ServiceCodeGenerator const&) = delete; 47 ServiceCodeGenerator& operator=(ServiceCodeGenerator const&) = delete; 48 ServiceCodeGenerator(ServiceCodeGenerator&&) = default; 49 ServiceCodeGenerator& operator=(ServiceCodeGenerator&&) = default; 50 51 Status Generate() override; 52 53 protected: 54 virtual Status GenerateHeader() = 0; 55 virtual Status GenerateCc() = 0; 56 57 VarsDictionary const& vars() const; 58 std::string vars(std::string const& key) const; 59 std::vector< 60 std::reference_wrapper<google::protobuf::MethodDescriptor const>> const& 61 methods() const; 62 void SetVars(absl::string_view header_path); 63 VarsDictionary MergeServiceAndMethodVars( 64 google::protobuf::MethodDescriptor const& method) const; 65 66 void HeaderLocalIncludes(std::vector<std::string> const& local_includes); 67 void CcLocalIncludes(std::vector<std::string> const& local_includes); 68 void HeaderSystemIncludes(std::vector<std::string> const& system_includes); 69 void CcSystemIncludes(std::vector<std::string> const& system_includes); 70 71 Status HeaderOpenNamespaces(NamespaceType ns_type = NamespaceType::kNormal); 72 void HeaderCloseNamespaces(); 73 Status CcOpenNamespaces(NamespaceType ns_type = NamespaceType::kNormal); 74 void CcCloseNamespaces(); 75 76 void HeaderPrint(std::string const& text); 77 Status HeaderPrintMethod(google::protobuf::MethodDescriptor const& method, 78 std::vector<MethodPattern> const& patterns, 79 char const* file, int line); 80 void CcPrint(std::string const& text); 81 Status CcPrintMethod(google::protobuf::MethodDescriptor const& method, 82 std::vector<MethodPattern> const& patterns, 83 char const* file, int line); 84 85 private: 86 enum class FileType { kHeaderFile, kCcFile }; 87 static void GenerateLocalIncludes(Printer& p, 88 std::vector<std::string> local_includes, 89 FileType file_type = FileType::kHeaderFile); 90 static void GenerateSystemIncludes(Printer& p, 91 std::vector<std::string> system_includes); 92 93 Status OpenNamespaces(Printer& p, 94 NamespaceType ns_type = NamespaceType::kNormal); 95 void CloseNamespaces(Printer& p); 96 97 google::protobuf::ServiceDescriptor const* service_descriptor_; 98 VarsDictionary service_vars_; 99 std::map<std::string, VarsDictionary> service_method_vars_; 100 std::vector<std::string> namespaces_; 101 std::vector<std::reference_wrapper<google::protobuf::MethodDescriptor const>> 102 methods_; 103 Printer header_; 104 Printer cc_; 105 }; 106 107 } // namespace generator_internal 108 } // namespace cloud 109 } // namespace google 110 111 #endif // GOOGLE_CLOUD_CPP_GENERATOR_INTERNAL_SERVICE_CODE_GENERATOR_H 112