timeCode.h
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef PXR_USD_USD_TIME_CODE_H
25 #define PXR_USD_USD_TIME_CODE_H
26 
27 #include "pxr/pxr.h"
28 #include "pxr/usd/usd/api.h"
29 #include "pxr/usd/sdf/timeCode.h"
30 #include "pxr/base/arch/hints.h"
32 #include "pxr/base/tf/hash.h"
33 
34 #include <limits>
35 #include <iosfwd>
36 #include <cmath>
37 
38 
39 PXR_NAMESPACE_OPEN_SCOPE
40 
41 
42 #define USD_TIME_CODE_TOKENS \
43  (DEFAULT) \
44  (EARLIEST)
45 
46 TF_DECLARE_PUBLIC_TOKENS(UsdTimeCodeTokens, USD_API, USD_TIME_CODE_TOKENS);
47 
48 
84 class UsdTimeCode {
85 public:
87  constexpr UsdTimeCode(double t = 0.0) noexcept : _value(t) {}
88 
90  constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
91  : _value(timeCode.GetValue()) {}
92 
101  static constexpr UsdTimeCode EarliestTime() {
102  return UsdTimeCode(std::numeric_limits<double>::lowest());
103  }
104 
112  static constexpr UsdTimeCode Default() {
113  return UsdTimeCode(std::numeric_limits<double>::quiet_NaN());
114  }
115 
125  static constexpr double
126  SafeStep(double maxValue=1e6, double maxCompression=10.0) {
127  return std::numeric_limits<double>::epsilon() *
128  maxValue * maxCompression * 2.0;
129  }
130 
133  bool IsEarliestTime() const {
134  return IsNumeric() && (_value == std::numeric_limits<double>::lowest());
135  }
136 
139  bool IsDefault() const {
140  return std::isnan(_value);
141  }
142 
145  bool IsNumeric() const {
146  return !IsDefault();
147  }
148 
151  double GetValue() const {
152  if (ARCH_UNLIKELY(IsDefault()))
153  _IssueGetValueOnDefaultError();
154  return _value;
155  }
156 
158  friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
159  return lhs.IsDefault() == rhs.IsDefault() &&
160  (lhs.IsDefault() || (lhs.GetValue() == rhs.GetValue()));
161  }
162 
164  friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode& rhs) {
165  return !(lhs == rhs);
166  }
167 
170  friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
171  return (lhs.IsDefault() && rhs.IsNumeric()) ||
172  (lhs.IsNumeric() && rhs.IsNumeric() &&
173  lhs.GetValue() < rhs.GetValue());
174  }
175 
178  friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
179  return !(lhs < rhs);
180  }
181 
184  friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
185  return lhs.IsDefault() ||
186  (rhs.IsNumeric() && lhs.GetValue() <= rhs.GetValue());
187  }
188 
191  friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs) {
192  return !(lhs <= rhs);
193  }
194 
196  friend size_t hash_value(const UsdTimeCode &time) {
197  return TfHash()(time._value);
198  }
199 
200 private:
201  USD_API
202  void _IssueGetValueOnDefaultError() const;
203 
204  double _value;
205 };
206 
207 // Stream I/O operators.
208 USD_API
209 std::ostream& operator<<(std::ostream& os, const UsdTimeCode& time);
210 
211 USD_API
212 std::istream& operator>>(std::istream& is, UsdTimeCode& time);
213 
214 
215 PXR_NAMESPACE_CLOSE_SCOPE
216 
217 #endif // PXR_USD_USD_TIME_CODE_H
friend bool operator==(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Equality comparison.
Definition: timeCode.h:158
static constexpr UsdTimeCode EarliestTime()
Produce a UsdTimeCode representing the lowest/earliest possible timeCode.
Definition: timeCode.h:101
friend bool operator>=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-equal.
Definition: timeCode.h:178
friend bool operator<=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-equal.
Definition: timeCode.h:184
Compiler hints.
bool IsNumeric() const
Return true if this time represents a numeric value, false otherwise.
Definition: timeCode.h:145
constexpr UsdTimeCode(double t=0.0) noexcept
Construct with optional time value. Impilicitly convert from double.
Definition: timeCode.h:87
A user-extensible hashing mechanism for use with runtime hash tables.
Definition: hash.h:504
Value type that represents a time code.
Definition: timeCode.h:44
Represent a time value, which may be either numeric, holding a double value, or a sentinel value UsdT...
Definition: timeCode.h:84
#define TF_DECLARE_PUBLIC_TOKENS(...)
Macro to define public tokens.
Definition: staticTokens.h:118
friend size_t hash_value(const UsdTimeCode &time)
Hash function.
Definition: timeCode.h:196
USDUTILS_API std::istream & operator>>(std::istream &is, UsdUtilsTimeCodeRange &timeCodeRange)
Stream extraction operator.
constexpr UsdTimeCode(const SdfTimeCode &timeCode) noexcept
Construct and implicitly cast from SdfTimeCode.
Definition: timeCode.h:90
This file defines some macros that are useful for declaring and using static TfTokens.
static constexpr UsdTimeCode Default()
Produce a UsdTimeCode representing the sentinel value for 'default'.
Definition: timeCode.h:112
bool IsEarliestTime() const
Return true if this time represents the lowest/earliest possible timeCode, false otherwise.
Definition: timeCode.h:133
friend bool operator!=(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Inequality comparison.
Definition: timeCode.h:164
static constexpr double SafeStep(double maxValue=1e6, double maxCompression=10.0)
Produce a safe step value such that for any numeric UsdTimeCode t in [-maxValue, maxValue],...
Definition: timeCode.h:126
double GetValue() const
Return the numeric value for this time.
Definition: timeCode.h:151
bool IsDefault() const
Return true if this time represents the 'default' sentinel value, false otherwise.
Definition: timeCode.h:139
SDF_API std::ostream & operator<<(std::ostream &out, const SdfTimeCode &ap)
Stream insertion operator for the string representation of this time code.
friend bool operator<(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Less-than.
Definition: timeCode.h:170
friend bool operator>(const UsdTimeCode &lhs, const UsdTimeCode &rhs)
Greater-than.
Definition: timeCode.h:191