1 /* 2 * 3 * Copyright 2016 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H 20 #define GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H 21 22 #include <grpcpp/impl/codegen/core_codegen_interface.h> 23 24 namespace grpc { 25 26 class GrpcLibraryInterface { 27 public: 28 virtual ~GrpcLibraryInterface() = default; 29 virtual void init() = 0; 30 virtual void shutdown() = 0; 31 }; 32 33 /// Initialized by \a grpc::GrpcLibraryInitializer from 34 /// <grpcpp/impl/grpc_library.h> 35 extern GrpcLibraryInterface* g_glip; 36 37 /// Classes that require gRPC to be initialized should inherit from this class. 38 class GrpcLibraryCodegen { 39 public: 40 explicit GrpcLibraryCodegen(bool call_grpc_init = true) grpc_init_called_(false)41 : grpc_init_called_(false) { 42 if (call_grpc_init) { 43 GPR_CODEGEN_ASSERT(g_glip && 44 "gRPC library not initialized. See " 45 "grpc::internal::GrpcLibraryInitializer."); 46 g_glip->init(); 47 grpc_init_called_ = true; 48 } 49 } ~GrpcLibraryCodegen()50 virtual ~GrpcLibraryCodegen() { 51 if (grpc_init_called_) { 52 GPR_CODEGEN_ASSERT(g_glip && 53 "gRPC library not initialized. See " 54 "grpc::internal::GrpcLibraryInitializer."); 55 g_glip->shutdown(); 56 } 57 } 58 59 private: 60 bool grpc_init_called_; 61 }; 62 63 } // namespace grpc 64 65 #endif // GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H 66