You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

time.h 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_CODEGEN_TIME_H
  19. #define GRPCPP_IMPL_CODEGEN_TIME_H
  20. // IWYU pragma: private, include <grpcpp/support/time.h>
  21. #include <chrono>
  22. #include <grpc/impl/codegen/grpc_types.h>
  23. #include <grpcpp/impl/codegen/config.h>
  24. namespace grpc
  25. {
  26. /** If you are trying to use CompletionQueue::AsyncNext with a time class that
  27. isn't either gpr_timespec or std::chrono::system_clock::time_point, you
  28. will most likely be looking at this comment as your compiler will have
  29. fired an error below. In order to fix this issue, you have two potential
  30. solutions:
  31. 1. Use gpr_timespec or std::chrono::system_clock::time_point instead
  32. 2. Specialize the TimePoint class with whichever time class that you
  33. want to use here. See below for two examples of how to do this.
  34. */
  35. template<typename T>
  36. class TimePoint
  37. {
  38. public:
  39. // If you see the error with methods below, you may need either
  40. // i) using the existing types having a conversion class such as
  41. // gpr_timespec and std::chrono::system_clock::time_point or
  42. // ii) writing a new TimePoint<YourType> to address your case.
  43. TimePoint(const T& /*time*/) = delete;
  44. gpr_timespec raw_time() = delete;
  45. };
  46. template<>
  47. class TimePoint<gpr_timespec>
  48. {
  49. public:
  50. /* NOLINTNEXTLINE(google-explicit-constructor) */
  51. TimePoint(const gpr_timespec& time) :
  52. time_(time)
  53. {
  54. }
  55. gpr_timespec raw_time()
  56. {
  57. return time_;
  58. }
  59. private:
  60. gpr_timespec time_;
  61. };
  62. } // namespace grpc
  63. namespace grpc
  64. {
  65. // from and to should be absolute time.
  66. void Timepoint2Timespec(const std::chrono::system_clock::time_point& from, gpr_timespec* to);
  67. void TimepointHR2Timespec(
  68. const std::chrono::high_resolution_clock::time_point& from,
  69. gpr_timespec* to
  70. );
  71. std::chrono::system_clock::time_point Timespec2Timepoint(gpr_timespec t);
  72. template<>
  73. class TimePoint<std::chrono::system_clock::time_point>
  74. {
  75. public:
  76. /* NOLINTNEXTLINE(google-explicit-constructor) */
  77. TimePoint(const std::chrono::system_clock::time_point& time)
  78. {
  79. Timepoint2Timespec(time, &time_);
  80. }
  81. gpr_timespec raw_time() const
  82. {
  83. return time_;
  84. }
  85. private:
  86. gpr_timespec time_;
  87. };
  88. } // namespace grpc
  89. #endif // GRPCPP_IMPL_CODEGEN_TIME_H