ADTF  3.18.3
builds/digitalwerk/solutions/adtf_content/adtf_base/adtf_core/src/libraries/base/include/adtfbase/chrono.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <adtf_utils.h>
9 #include <chrono>
10 
11 #include "adtf_base_type_traits.h"
12 
13 namespace adtf
14 {
15 namespace base
16 {
17 namespace flash
18 {
19 
22 {
23  int64_t nCount;
24 
25  tNanoSeconds():
26  nCount(0)
27  {
28  }
29 
30  tNanoSeconds(const tNanoSeconds&) = default;
31  tNanoSeconds(tNanoSeconds&&) = default;
32 
33  tNanoSeconds& operator=(const tNanoSeconds&) = default;
34  tNanoSeconds& operator=(tNanoSeconds&&) = default;
35 
36  explicit tNanoSeconds(int64_t nCount):
37  nCount(nCount)
38  {
39  }
40 
41  template<typename Representation, typename Period>
42  inline tNanoSeconds(const std::chrono::duration<Representation, Period>& oDuration):
43  nCount(std::chrono::duration_cast<std::chrono::nanoseconds>(oDuration).count())
44  {
45  }
46 
47  tNanoSeconds& operator+=(const tNanoSeconds& tmDuration)
48  {
49  nCount += tmDuration.nCount;
50  return *this;
51  }
52 
53  tNanoSeconds& operator-=(const tNanoSeconds& tmDuration)
54  {
55  nCount -= tmDuration.nCount;
56  return *this;
57  }
58 
59  tNanoSeconds& operator*=(const int64_t& nFactor)
60  {
61  nCount *= nFactor;
62  return *this;
63  }
64 
65  tNanoSeconds& operator/=(const int64_t& nDivisor)
66  {
67  nCount /= nDivisor;
68  return *this;
69  }
70 
71  tNanoSeconds& operator%=(const int64_t& nDivisor)
72  {
73  nCount %= nDivisor;
74  return *this;
75  }
76 
77  inline operator std::chrono::nanoseconds() const
78  {
79  return std::chrono::nanoseconds(nCount);
80  }
81 };
82 
83 inline bool operator<(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
84 {
85  return tmA.nCount < tmB.nCount;
86 }
87 
88 inline bool operator>(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
89 {
90  return tmA.nCount > tmB.nCount;
91 }
92 
93 inline bool operator<=(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
94 {
95  return tmA.nCount <= tmB.nCount;
96 }
97 
98 inline bool operator>=(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
99 {
100  return tmA.nCount >= tmB.nCount;
101 }
102 
103 inline bool operator==(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
104 {
105  return tmA.nCount == tmB.nCount;
106 }
107 
108 inline bool operator!=(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
109 {
110  return tmA.nCount != tmB.nCount;
111 }
112 
113 inline tNanoSeconds operator+(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
114 {
115  return tNanoSeconds{tmA.nCount + tmB.nCount};
116 }
117 
118 inline tNanoSeconds operator-(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
119 {
120  return tNanoSeconds{tmA.nCount - tmB.nCount};
121 }
122 
123 inline tNanoSeconds operator*(const tNanoSeconds& tmDuration, const int64_t& nFactor)
124 {
125  return tNanoSeconds{tmDuration.nCount * nFactor};
126 }
127 
128 inline tNanoSeconds operator*(const int64_t& nFactor, const tNanoSeconds& tmDuration)
129 {
130  return tNanoSeconds{nFactor * tmDuration.nCount};
131 }
132 
133 inline tNanoSeconds operator/(const tNanoSeconds& tmDuration, const int64_t& tmB)
134 {
135  return tNanoSeconds{tmDuration.nCount / tmB};
136 }
137 
138 inline int64_t operator/(const tNanoSeconds& tmA, const tNanoSeconds& tmB)
139 {
140  return tmA.nCount / tmB.nCount;
141 }
142 
149 template <typename DestinationTimeStamp, typename SourceTimeStamp>
150 DestinationTimeStamp duration_cast(const SourceTimeStamp& /*tmSource*/)
151 {
152  static_assert(penguin::detail::always_false<DestinationTimeStamp>, "unsupported duration cast");
153  return DestinationTimeStamp();
154 }
155 
156 template <>
157 inline tTimeStamp duration_cast(const tNanoSeconds& tmSource)
158 {
159  return tmSource.nCount / 1000;
160 }
161 
162 template <>
163 inline tNanoSeconds duration_cast(const tTimeStamp& tmSource)
164 {
165  return tNanoSeconds{tmSource * 1000};
166 }
167 
168 template <>
169 inline std::chrono::nanoseconds duration_cast(const tNanoSeconds& tmSource)
170 {
171  return tmSource;
172 }
173 
174 template <>
175 inline tNanoSeconds duration_cast(const std::chrono::nanoseconds& tmSource)
176 {
177  return tmSource;
178 }
179 
180 template <>
181 inline tTimeStamp duration_cast(const std::chrono::nanoseconds& tmSource)
182 {
183  return tmSource.count() / 1000;
184 }
185 
186 } // namespace flash
187 
188 using flash::tNanoSeconds;
190 
191 } // namespace base
192 } // namespace adtf
Copyright © Audi Electronics Venture GmbH.
tBool operator<(const tErrorCode &lhs, const tErrorCode &rhs)
Less-than operator for POD error type.
tBool operator<=(const tErrorCode &lhs, const tErrorCode &rhs)
Less-than-or-equal operator for POD error type.
tBool operator>(const tErrorCode &lhs, const tErrorCode &rhs)
Greater-than operator for POD error type.
tBool operator!=(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for inequality.
tBool operator==(const tErrorCode &lhs, const tErrorCode &rhs)
Compare two POD error code types for equality.
tBool operator>=(const tErrorCode &lhs, const tErrorCode &rhs)
Greater-than-or-equal operator for POD error type.
simple_pointer_iterator< T >::difference_type operator-(const simple_pointer_iterator< T > &r1, const simple_pointer_iterator< T > &r2)
Define arithmetic - operation between iterators.
simple_pointer_iterator< T >::difference_type operator+(const simple_pointer_iterator< T > &r1, const simple_pointer_iterator< T > &r2)
Define arithmetic + operation between iterators.
DestinationTimeStamp duration_cast(const SourceTimeStamp &)
Duration cast base template to converted between different time resolution.
Namespace for entire ADTF SDK.