grpc_library.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. *
  3. * Copyright 2015 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_GRPC_LIBRARY_H
  19. #define GRPCPP_IMPL_GRPC_LIBRARY_H
  20. #include <iostream>
  21. #include <grpc/grpc.h>
  22. #include <grpcpp/impl/codegen/config.h>
  23. #include <grpcpp/impl/codegen/core_codegen.h>
  24. #include <grpcpp/impl/codegen/grpc_library.h> // IWYU pragma: export
  25. namespace grpc {
  26. namespace internal {
  27. class GrpcLibrary final : public GrpcLibraryInterface {
  28. public:
  29. void init() override { grpc_init(); }
  30. void shutdown() override { grpc_shutdown(); }
  31. };
  32. /// Instantiating this class ensures the proper initialization of gRPC.
  33. class GrpcLibraryInitializer final {
  34. public:
  35. GrpcLibraryInitializer() {
  36. if (grpc::g_glip == nullptr) {
  37. static auto* const g_gli = new GrpcLibrary();
  38. grpc::g_glip = g_gli;
  39. }
  40. if (grpc::g_core_codegen_interface == nullptr) {
  41. static auto* const g_core_codegen = new CoreCodegen();
  42. grpc::g_core_codegen_interface = g_core_codegen;
  43. }
  44. }
  45. /// A no-op method to force the linker to reference this class, which will
  46. /// take care of initializing and shutting down the gRPC runtime.
  47. int summon() { return 0; }
  48. };
  49. } // namespace internal
  50. } // namespace grpc
  51. #endif // GRPCPP_IMPL_GRPC_LIBRARY_H