1 /* 2 * Copyright 2016 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef NET_GRPC_COMPILER_JAVA_GENERATOR_H_ 18 #define NET_GRPC_COMPILER_JAVA_GENERATOR_H_ 19 20 #include <stdlib.h> // for abort() 21 #include <iostream> 22 #include <map> 23 #include <string> 24 25 #include "src/compiler/schema_interface.h" 26 27 class LogMessageVoidify { 28 public: LogMessageVoidify()29 LogMessageVoidify() {} 30 // This has to be an operator with a precedence lower than << but 31 // higher than ?: 32 void operator&(std::ostream&) {} 33 }; 34 35 class LogHelper { 36 std::ostream* os_; 37 38 public: LogHelper(std::ostream * os)39 LogHelper(std::ostream* os) : os_(os) {} 40 #if defined(_MSC_VER) 41 #pragma warning(push) 42 #pragma warning( \ 43 disable : 4722) // the flow of control terminates in a destructor 44 // (needed to compile ~LogHelper where destructor emits abort intentionally - 45 // inherited from grpc/java code generator). 46 #endif ~LogHelper()47 ~LogHelper() { 48 *os_ << std::endl; 49 ::abort(); 50 } 51 #if defined(_MSC_VER) 52 #pragma warning(pop) 53 #endif get_os()54 std::ostream& get_os() const { return *os_; } 55 }; 56 57 // Abort the program after logging the mesage if the given condition is not 58 // true. Otherwise, do nothing. 59 #define GRPC_CODEGEN_CHECK(x) \ 60 (x) ? (void)0 \ 61 : LogMessageVoidify() & LogHelper(&std::cerr).get_os() \ 62 << "CHECK FAILED: " << __FILE__ << ":" \ 63 << __LINE__ << ": " 64 65 // Abort the program after logging the mesage. 66 #define GRPC_CODEGEN_FAIL GRPC_CODEGEN_CHECK(false) 67 68 namespace grpc_java_generator { 69 struct Parameters { 70 // //Defines the custom parameter types for methods 71 // //eg: flatbuffers uses flatbuffers.Builder as input for the client 72 // and output for the server grpc::string custom_method_io_type; 73 74 // Package name for the service 75 grpc::string package_name; 76 }; 77 78 // Return the source of the generated service file. 79 grpc::string GenerateServiceSource(grpc_generator::File* file, 80 const grpc_generator::Service* service, 81 grpc_java_generator::Parameters* parameters); 82 83 } // namespace grpc_java_generator 84 85 #endif // NET_GRPC_COMPILER_JAVA_GENERATOR_H_ 86