grpc_library.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H
  19. #define GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H
  20. // IWYU pragma: private, include <grpcpp/impl/grpc_library.h>
  21. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  22. namespace grpc {
  23. class GrpcLibraryInterface {
  24. public:
  25. virtual ~GrpcLibraryInterface() = default;
  26. virtual void init() = 0;
  27. virtual void shutdown() = 0;
  28. };
  29. /// Initialized by \a grpc::GrpcLibraryInitializer from
  30. /// <grpcpp/impl/grpc_library.h>
  31. extern GrpcLibraryInterface* g_glip;
  32. /// Classes that require gRPC to be initialized should inherit from this class.
  33. class GrpcLibraryCodegen {
  34. public:
  35. explicit GrpcLibraryCodegen(bool call_grpc_init = true)
  36. : grpc_init_called_(false) {
  37. if (call_grpc_init) {
  38. GPR_CODEGEN_ASSERT(g_glip &&
  39. "gRPC library not initialized. See "
  40. "grpc::internal::GrpcLibraryInitializer.");
  41. g_glip->init();
  42. grpc_init_called_ = true;
  43. }
  44. }
  45. virtual ~GrpcLibraryCodegen() {
  46. if (grpc_init_called_) {
  47. GPR_CODEGEN_ASSERT(g_glip &&
  48. "gRPC library not initialized. See "
  49. "grpc::internal::GrpcLibraryInitializer.");
  50. g_glip->shutdown();
  51. }
  52. }
  53. private:
  54. bool grpc_init_called_;
  55. };
  56. } // namespace grpc
  57. #endif // GRPCPP_IMPL_CODEGEN_GRPC_LIBRARY_H