$darkmode
duration.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
23 #pragma once
24 #ifndef CLOE_CORE_DURATION_HPP_
25 #define CLOE_CORE_DURATION_HPP_
26 
27 #include <chrono> // for duration<>, duration_cast<>, nanoseconds
28 #include <ratio> // for micro, milli
29 #include <string> // for string
30 
31 #include <nlohmann/json.hpp> // for Json, adl_serializer
32 
33 namespace cloe {
34 
45 using Duration = std::chrono::nanoseconds;
46 
47 using Microseconds = std::chrono::duration<double, std::micro>;
48 using Milliseconds = std::chrono::duration<double, std::milli>;
49 using Seconds = std::chrono::duration<double>;
50 
51 std::string to_string(const Duration& ns);
52 
53 // Occasionally, we want to have a human-readable and a machine-readable
54 // representation of a duration. This function does exactly that.
55 nlohmann::json to_convenient_json(const Duration& ns);
56 
57 } // namespace cloe
58 
59 /*
60  * In order to provide serialization for third-party types, we need to either
61  * use their namespace or provide a specialization in that of nlohmann. It is
62  * illegal to define anything in the std namespace, so we are left no choice in
63  * this regard.
64  *
65  * See: https://github.com/nlohmann/json
66  */
67 namespace nlohmann {
68 
69 inline std::string to_string_hr(double d) {
70  auto n = std::to_string(d);
71  n.erase(n.find_last_not_of('0') + 1, std::string::npos);
72  if (n[n.size() - 1] == '.') {
73  n.pop_back();
74  }
75  return n;
76 }
77 
78 template <>
79 struct adl_serializer<cloe::Microseconds> {
80  static void to_json(json& j, const cloe::Microseconds& us) {
81  j = to_string_hr(us.count()) + "us";
82  }
83 };
84 
85 template <>
86 struct adl_serializer<cloe::Milliseconds> {
87  static void to_json(json& j, const cloe::Milliseconds& ms) {
88  j = to_string_hr(ms.count()) + "ms";
89  }
90 };
91 
92 template <>
93 struct adl_serializer<cloe::Seconds> {
94  static void to_json(json& j, const cloe::Seconds& s) { j = to_string_hr(s.count()) + "s"; }
95 };
96 
97 template <>
98 struct adl_serializer<cloe::Duration> {
99  static void to_json(json& j, const cloe::Duration& ns) {
100  auto count = ns.count();
101  if (count > 1e9) {
102  j = std::chrono::duration_cast<cloe::Seconds>(ns);
103  } else if (count > 1e6) {
104  j = std::chrono::duration_cast<cloe::Milliseconds>(ns);
105  } else if (count > 1e3) {
106  j = std::chrono::duration_cast<cloe::Microseconds>(ns);
107  } else {
108  j = to_string_hr(count) + "ns";
109  }
110  }
111 };
112 
113 } // namespace nlohmann
114 
115 #endif // CLOE_CORE_DURATION_HPP_
Definition: coordinator.hpp:36
std::chrono::nanoseconds Duration
Definition: duration.hpp:45
Definition: confable.hpp:132