| 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 | GrpcLibraryCodegen(bool call_grpc_init = true) : grpc_init_called_(false) { |
| 41 | if (call_grpc_init) { |
| 42 | GPR_CODEGEN_ASSERT(g_glip && |
| 43 | "gRPC library not initialized. See " |
| 44 | "grpc::internal::GrpcLibraryInitializer." ); |
| 45 | g_glip->init(); |
| 46 | grpc_init_called_ = true; |
| 47 | } |
| 48 | } |
| 49 | virtual ~GrpcLibraryCodegen() { |
| 50 | if (grpc_init_called_) { |
| 51 | GPR_CODEGEN_ASSERT(g_glip && |
| 52 | "gRPC library not initialized. See " |
| 53 | "grpc::internal::GrpcLibraryInitializer." ); |
| 54 | g_glip->shutdown(); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private: |
| 59 | bool grpc_init_called_; |
| 60 | }; |
| 61 | |
| 62 | } // namespace grpc |
| 63 | |
| 64 | #endif // GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H |
| 65 | |