timestamp.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 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_AR_TIMESTAMP_H
25 #define PXR_USD_AR_TIMESTAMP_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/usd/ar/api.h"
31 
32 #include "pxr/base/arch/hints.h"
33 #include "pxr/base/tf/hash.h"
34 
35 #include <limits>
36 
37 PXR_NAMESPACE_OPEN_SCOPE
38 
43 {
44 public:
47  : _time(std::numeric_limits<double>::quiet_NaN())
48  {
49  }
50 
52  explicit ArTimestamp(double time)
53  : _time(time)
54  {
55  }
56 
58  bool IsValid() const
59  {
60  return !std::isnan(_time);
61  }
62 
66  double GetTime() const
67  {
68  if (ARCH_UNLIKELY(!IsValid())) {
69  _IssueInvalidGetTimeError();
70  }
71  return _time;
72  }
73 
78 
79  friend bool operator==(const ArTimestamp& lhs, const ArTimestamp& rhs)
80  {
81  return (!lhs.IsValid() && !rhs.IsValid()) ||
82  (lhs.IsValid() && rhs.IsValid() && lhs._time == rhs._time);
83  }
84 
85  friend bool operator!=(const ArTimestamp& lhs, const ArTimestamp& rhs)
86  {
87  return !(lhs == rhs);
88  }
89 
90  friend bool operator<(const ArTimestamp& lhs, const ArTimestamp& rhs)
91  {
92  return (!lhs.IsValid() && rhs.IsValid()) ||
93  (lhs.IsValid() && rhs.IsValid() && lhs._time < rhs._time);
94  }
95 
96  friend bool operator>=(const ArTimestamp& lhs, const ArTimestamp& rhs)
97  {
98  return !(lhs < rhs);
99  }
100 
101  friend bool operator<=(const ArTimestamp& lhs, const ArTimestamp& rhs)
102  {
103  return !lhs.IsValid() || (rhs.IsValid() && lhs._time <= rhs._time);
104  }
105 
106  friend bool operator>(const ArTimestamp& lhs, const ArTimestamp& rhs)
107  {
108  return !(lhs <= rhs);
109  }
110 
112 
113 private:
114  AR_API
115  void _IssueInvalidGetTimeError() const;
116 
117  // TfHash support.
118  template <class HashState>
119  friend void TfHashAppend(HashState& h, const ArTimestamp& t)
120  {
121  h.Append(t._time);
122  }
123 
124  double _time;
125 };
126 
127 PXR_NAMESPACE_CLOSE_SCOPE
128 
129 #endif
Represents a timestamp for an asset.
Definition: timestamp.h:42
Compiler hints.
double GetTime() const
Return the time represented by this timestamp as a double.
Definition: timestamp.h:66
ArTimestamp()
Create an invalid timestamp.
Definition: timestamp.h:46
ArTimestamp(double time)
Create a timestamp at time, which must be a Unix time value.
Definition: timestamp.h:52
friend bool operator==(const ArTimestamp &lhs, const ArTimestamp &rhs)
Comparison operators Note that invalid timestamps are considered less than all other timestamps.
Definition: timestamp.h:79
bool IsValid() const
Return true if this timestamp is valid, false otherwise.
Definition: timestamp.h:58