time.h 2.7 KB

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