Argus Camera Sample Argus Camera Sample

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Util.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of NVIDIA CORPORATION nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef UTIL_H
30 #define UTIL_H
31
32 #include <stdint.h> // for uint64_t
33 #include <limits> // for numeric_limits
34
35 namespace ArgusSamples
36 {
37
38 /**
39 * A time value. Supports conversion to different time units.
40 */
41 class TimeValue
42 {
43 public:
44 TimeValue()
45 : m_nSec(0)
46 {
47 }
48
49 /**
50 * Types used for values with various units
51 */
52 typedef uint64_t SecType;
53 typedef uint64_t MSecType;
54 typedef uint64_t USecType;
55 typedef uint64_t NSecType;
56 typedef float CyclesPerSecType;
57
58 // construct a infinite time value
59 static TimeValue infinite()
60 {
61 return TimeValue(std::numeric_limits<NSecType>::max());
62 }
63
64 // functions to construct from various time units
65 static TimeValue fromSec(float sec)
66 {
67 return TimeValue(static_cast<NSecType>(sec * 1e9));
68 }
69
70 static TimeValue fromSec(SecType sec)
71 {
72 return TimeValue(sec * 1000000000);
73 }
74
75 static TimeValue fromMSec(MSecType mSec)
76 {
77 return TimeValue(mSec * 1000000);
78 }
79
80 static TimeValue fromUSec(USecType uSec)
81 {
82 return TimeValue(uSec * 1000);
83 }
84
85 static TimeValue fromNSec(NSecType nSec)
86 {
87 return TimeValue(nSec);
88 }
89
90 static TimeValue fromCycelsPerSec(CyclesPerSecType cyclesPerSec)
91 {
92 return TimeValue(static_cast<NSecType>(1e9 / static_cast<double>(cyclesPerSec)));
93 }
94
95 // functions to set from various time uints
96 void setFromSec(SecType sec)
97 {
98 m_nSec = sec * 1000000000;
99 }
100
101 void setFromMSec(MSecType mSec)
102 {
103 m_nSec = mSec * 1000000;
104 }
105
106 void setFromUSec(USecType uSec)
107 {
108 m_nSec = uSec * 1000;
109 }
110
111 void setFromNSec(NSecType nSec)
112 {
113 m_nSec = nSec;
114 }
115
116 // functions to convert to various time units
117 SecType toSec() const
118 {
119 return m_nSec / 1000000000;
120 }
121
122 MSecType toMSec() const
123 {
124 return m_nSec / 1000000;
125 }
126
127 USecType toUSec() const
128 {
129 return m_nSec / 1000;
130 }
131
132 NSecType toNSec() const
133 {
134 return m_nSec;
135 }
136
137 CyclesPerSecType toCyclesPerSec() const
138 {
139 if (m_nSec == 0.0f)
140 return std::numeric_limits<CyclesPerSecType>::max();
141
142 return static_cast<CyclesPerSecType>(1e9 / static_cast<double>(m_nSec));
143 }
144
145 // comparision and equality operators
146 bool operator == (const TimeValue& rhs) const
147 {
148 return (m_nSec == rhs.m_nSec);
149 }
150
151 bool operator != (const TimeValue &rhs) const
152 {
153 return !(operator == (rhs));
154 }
155
156 bool operator < (const TimeValue& rhs) const
157 {
158 return (m_nSec < rhs.m_nSec);
159 }
160
161 bool operator >= (const TimeValue &rhs) const
162 {
163 return !(operator < (rhs));
164 }
165
166 bool operator <= (const TimeValue &rhs) const
167 {
168 return (m_nSec <= rhs.m_nSec);
169 }
170
171 bool operator > (const TimeValue &rhs) const
172 {
173 return !(operator <= (rhs));
174 }
175
176 TimeValue operator + (const TimeValue& rhs) const
177 {
178 return (m_nSec + rhs.m_nSec);
179 }
180
181 TimeValue operator - (const TimeValue& rhs) const
182 {
183 return (m_nSec - rhs.m_nSec);
184 }
185
186 private:
187 TimeValue(NSecType value)
188 : m_nSec(value)
189 {
190 }
191
192 NSecType m_nSec;
193 };
194
195 /*!
196 * Get the current time.
197 */
198 TimeValue getCurrentTime();
199
200 }; // namespace ArgusSamples
201
202 #endif // UTIL_H