LCOV - code coverage report
Current view: top level - compiler - cpp_plugin.cc (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 23 34 67.6 %
Date: 2015-12-10 22:15:08 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : // Generates cpp gRPC service interface out of Protobuf IDL.
      35             : //
      36             : 
      37             : #include <memory>
      38             : 
      39             : #include "src/compiler/config.h"
      40             : 
      41             : #include "src/compiler/cpp_generator.h"
      42             : #include "src/compiler/cpp_generator_helpers.h"
      43             : 
      44             : class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
      45             :  public:
      46          12 :   CppGrpcGenerator() {}
      47          12 :   virtual ~CppGrpcGenerator() {}
      48             : 
      49          12 :   virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
      50             :                         const grpc::string &parameter,
      51             :                         grpc::protobuf::compiler::GeneratorContext *context,
      52             :                         grpc::string *error) const {
      53          12 :     if (file->options().cc_generic_services()) {
      54             :       *error =
      55             :           "cpp grpc proto compiler plugin does not work with generic "
      56             :           "services. To generate cpp grpc APIs, please set \""
      57           0 :           "cc_generic_service = false\".";
      58           0 :       return false;
      59             :     }
      60             : 
      61          12 :     grpc_cpp_generator::Parameters generator_parameters;
      62             : 
      63          12 :     if (!parameter.empty()) {
      64             :       std::vector<grpc::string> parameters_list =
      65           0 :         grpc_generator::tokenize(parameter, ",");
      66           0 :       for (auto parameter_string = parameters_list.begin();
      67           0 :            parameter_string != parameters_list.end();
      68             :            parameter_string++) {
      69             :         std::vector<grpc::string> param =
      70           0 :           grpc_generator::tokenize(*parameter_string, "=");
      71           0 :         if (param[0] == "services_namespace") {
      72           0 :           generator_parameters.services_namespace = param[1];
      73             :         } else {
      74           0 :           *error = grpc::string("Unknown parameter: ") + *parameter_string;
      75           0 :           return false;
      76             :         }
      77           0 :       }
      78             :     }
      79             : 
      80          24 :     grpc::string file_name = grpc_generator::StripProto(file->name());
      81             : 
      82             :     grpc::string header_code =
      83          24 :         grpc_cpp_generator::GetHeaderPrologue(file, generator_parameters) +
      84          48 :         grpc_cpp_generator::GetHeaderIncludes(file, generator_parameters) +
      85             :         grpc_cpp_generator::GetHeaderServices(file, generator_parameters) +
      86          48 :         grpc_cpp_generator::GetHeaderEpilogue(file, generator_parameters);
      87             :     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
      88          24 :         context->Open(file_name + ".grpc.pb.h"));
      89             :     grpc::protobuf::io::CodedOutputStream header_coded_out(
      90          24 :         header_output.get());
      91          12 :     header_coded_out.WriteRaw(header_code.data(), header_code.size());
      92             : 
      93             :     grpc::string source_code =
      94          24 :         grpc_cpp_generator::GetSourcePrologue(file, generator_parameters) +
      95          48 :         grpc_cpp_generator::GetSourceIncludes(file, generator_parameters) +
      96             :         grpc_cpp_generator::GetSourceServices(file, generator_parameters) +
      97          48 :         grpc_cpp_generator::GetSourceEpilogue(file, generator_parameters);
      98             :     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
      99          24 :         context->Open(file_name + ".grpc.pb.cc"));
     100             :     grpc::protobuf::io::CodedOutputStream source_coded_out(
     101          24 :         source_output.get());
     102          12 :     source_coded_out.WriteRaw(source_code.data(), source_code.size());
     103             : 
     104          24 :     return true;
     105             :   }
     106             : 
     107             :  private:
     108             :   // Insert the given code into the given file at the given insertion point.
     109             :   void Insert(grpc::protobuf::compiler::GeneratorContext *context,
     110             :               const grpc::string &filename, const grpc::string &insertion_point,
     111             :               const grpc::string &code) const {
     112             :     std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
     113             :         context->OpenForInsert(filename, insertion_point));
     114             :     grpc::protobuf::io::CodedOutputStream coded_out(output.get());
     115             :     coded_out.WriteRaw(code.data(), code.size());
     116             :   }
     117             : };
     118             : 
     119          12 : int main(int argc, char *argv[]) {
     120          12 :   CppGrpcGenerator generator;
     121          12 :   return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
     122             : }

Generated by: LCOV version 1.11