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