• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 //  Based on original Protocol Buffers design by
10 //  Sanjay Ghemawat, Jeff Dean, and others.
11 
12 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_SERVICE_H__
13 #define GOOGLE_PROTOBUF_COMPILER_CPP_SERVICE_H__
14 
15 #include <string>
16 
17 #include "absl/container/flat_hash_map.h"
18 #include "absl/strings/string_view.h"
19 #include "google/protobuf/compiler/cpp/options.h"
20 #include "google/protobuf/descriptor.h"
21 #include "google/protobuf/io/printer.h"
22 
23 namespace google {
24 namespace protobuf {
25 namespace compiler {
26 namespace cpp {
27 class ServiceGenerator {
28  public:
29   // See generator.cc for the meaning of dllexport_decl.
ServiceGenerator(const ServiceDescriptor * descriptor,const absl::flat_hash_map<absl::string_view,std::string> & vars,const Options & options)30   ServiceGenerator(
31       const ServiceDescriptor* descriptor,
32       const absl::flat_hash_map<absl::string_view, std::string>& vars,
33       const Options& options)
34       : descriptor_(descriptor), options_(&options), vars_(vars) {
35     vars_["classname"] = descriptor_->name();
36     vars_["full_name"] = descriptor_->full_name();
37   }
38 
39   ServiceGenerator(const ServiceGenerator&) = delete;
40   ServiceGenerator& operator=(const ServiceGenerator&) = delete;
41   ServiceGenerator(ServiceGenerator&&) = delete;
42   ServiceGenerator& operator=(ServiceGenerator&&) = delete;
43 
44   ~ServiceGenerator() = default;
45 
46   // Generate the class definitions for the service's interface and the
47   // stub implementation.
48   void GenerateDeclarations(io::Printer* printer);
49 
50   // Generate implementations of everything declared by
51   // GenerateDeclarations().
52   void GenerateImplementation(io::Printer* printer);
53 
54  private:
55   enum RequestOrResponse { kRequest, kResponse };
56   enum VirtualOrNot { kVirtual, kNonVirtual };
57 
58   // Prints signatures for all methods in the
59   void GenerateMethodSignatures(VirtualOrNot virtual_or_not,
60                                 io::Printer* printer);
61 
62   // Generate the default implementations of the service methods, which
63   // produce a "not implemented" error.
64   void GenerateNotImplementedMethods(io::Printer* printer);
65 
66   // Generate the CallMethod() method of the service.
67   void GenerateCallMethod(io::Printer* printer);
68 
69   // Generate the Get{Request,Response}Prototype() methods.
70   void GenerateGetPrototype(RequestOrResponse which, io::Printer* printer);
71 
72   // Generate the cases in CallMethod().
73   void GenerateCallMethodCases(io::Printer* printer);
74 
75   // Generate the stub's implementations of the service methods.
76   void GenerateStubMethods(io::Printer* printer);
77 
78   const ServiceDescriptor* descriptor_;
79   const Options* options_;
80   absl::flat_hash_map<absl::string_view, std::string> vars_;
81 
82   int index_in_metadata_;
83 
84   friend class FileGenerator;
85 };
86 }  // namespace cpp
87 }  // namespace compiler
88 }  // namespace protobuf
89 }  // namespace google
90 
91 #endif  // GOOGLE_PROTOBUF_COMPILER_CPP_SERVICE_H__
92