time.h 2.7 KB

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