$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 
25 #include <chrono> // for duration<>, duration_cast<>, nanoseconds
26 #include <ratio> // for micro, milli
27 #include <string> // for string
28 
29 #include <nlohmann/json.hpp> // for Json, adl_serializer
30 
31 namespace cloe {
32 
43 using Duration = std::chrono::nanoseconds;
44 
45 using Microseconds = std::chrono::duration<double, std::micro>;
46 using Milliseconds = std::chrono::duration<double, std::milli>;
47 using Seconds = std::chrono::duration<double>;
48 
49 std::string to_string(const Duration& ns);
50 
51 // Occasionally, we want to have a human-readable and a machine-readable
52 // representation of a duration. This function does exactly that.
53 nlohmann::json to_convenient_json(const Duration& ns);
54 
55 } // namespace cloe
56 
57 /*
58  * In order to provide serialization for third-party types, we need to either
59  * use their namespace or provide a specialization in that of nlohmann. It is
60  * illegal to define anything in the std namespace, so we are left no choice in
61  * this regard.
62  *
63  * See: https://github.com/nlohmann/json
64  */
65 namespace nlohmann {
66 
67 inline std::string to_string_hr(double d) {
68  auto n = std::to_string(d);
69  n.erase(n.find_last_not_of('0') + 1, std::string::npos);
70  if (n[n.size() - 1] == '.') {
71  n.pop_back();
72  }
73  return n;
74 }
75 
76 template <>
77 struct adl_serializer<cloe::Microseconds> {
78  static void to_json(json& j, const cloe::Microseconds& us) {
79  j = to_string_hr(us.count()) + "us";
80  }
81 };
82 
83 template <>
84 struct adl_serializer<cloe::Milliseconds> {
85  static void to_json(json& j, const cloe::Milliseconds& ms) {
86  j = to_string_hr(ms.count()) + "ms";
87  }
88 };
89 
90 template <>
91 struct adl_serializer<cloe::Seconds> {
92  static void to_json(json& j, const cloe::Seconds& s) { j = to_string_hr(s.count()) + "s"; }
93 };
94 
95 template <>
96 struct adl_serializer<cloe::Duration> {
97  static void to_json(json& j, const cloe::Duration& ns) {
98  auto count = ns.count();
99  if (count > 1e9) {
100  j = std::chrono::duration_cast<cloe::Seconds>(ns);
101  } else if (count > 1e6) {
102  j = std::chrono::duration_cast<cloe::Milliseconds>(ns);
103  } else if (count > 1e3) {
104  j = std::chrono::duration_cast<cloe::Microseconds>(ns);
105  } else {
106  j = to_string_hr(count) + "ns";
107  }
108  }
109 };
110 
111 } // namespace nlohmann
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36