Skip to content

Commit 0d04b04

Browse files
Add Doxygen comments to RationalTime (AcademySoftwareFoundation#1765)
Signed-off-by: Darby Johnston <[email protected]>
1 parent e10fdd2 commit 0d04b04

File tree

1 file changed

+114
-17
lines changed

1 file changed

+114
-17
lines changed

src/opentime/rationalTime.h

+114-17
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212

1313
namespace opentime { namespace OPENTIME_VERSION {
1414

15+
/// @brief This enumeration provides options for drop frame timecode.
1516
enum IsDropFrameRate : int
1617
{
1718
InferFromRate = -1,
1819
ForceNo = 0,
1920
ForceYes = 1,
2021
};
2122

23+
/// @brief Returns the absolute value.
24+
///
25+
/// \todo Document why this function is used instead of "std::fabs()".
2226
constexpr double
2327
fabs(double val) noexcept
2428
{
@@ -31,77 +35,104 @@ fabs(double val) noexcept
3135
return bits.f;
3236
}
3337

38+
/// @brief This class represents a measure of time defined by a value and rate.
3439
class RationalTime
3540
{
3641
public:
42+
/// @brief Construct a new time with an optional value and rate.
3743
explicit constexpr RationalTime(double value = 0, double rate = 1) noexcept
3844
: _value{ value }
3945
, _rate{ rate }
4046
{}
4147

48+
/// @brief Returns true if the time is invalid.
49+
///
50+
/// The time is considered invalid if the value or rate are a NaN value,
51+
/// or if the rate is less than or equal to zero.
4252
bool is_invalid_time() const noexcept
4353
{
4454
return (std::isnan(_rate) || std::isnan(_value)) ? true : (_rate <= 0);
4555
}
4656

57+
/// @brief Returns the time value.
4758
constexpr double value() const noexcept { return _value; }
4859

60+
/// @brief Returns the time rate.
4961
constexpr double rate() const noexcept { return _rate; }
5062

63+
/// @brief Returns the time converted to a new rate.
5164
constexpr RationalTime rescaled_to(double new_rate) const noexcept
5265
{
5366
return RationalTime{ value_rescaled_to(new_rate), new_rate };
5467
}
5568

69+
/// @brief Returns the time converted to a new rate.
5670
constexpr RationalTime rescaled_to(RationalTime rt) const noexcept
5771
{
5872
return RationalTime{ value_rescaled_to(rt._rate), rt._rate };
5973
}
6074

75+
/// @brief Returns the time value converted to a new rate.
6176
constexpr double value_rescaled_to(double new_rate) const noexcept
6277
{
6378
return new_rate == _rate ? _value : (_value * new_rate) / _rate;
6479
}
6580

81+
/// @brief Returns the time value converted to a new rate.
6682
constexpr double value_rescaled_to(RationalTime rt) const noexcept
6783
{
6884
return value_rescaled_to(rt._rate);
6985
}
7086

87+
/// @brief Returns whether time is almost equal to another time.
88+
///
89+
/// @param other The other time for comparison.
90+
/// @param delta The tolerance used for the comparison.
7191
constexpr bool
7292
almost_equal(RationalTime other, double delta = 0) const noexcept
7393
{
7494
return fabs(value_rescaled_to(other._rate) - other._value) <= delta;
7595
}
7696

77-
// Return whether the value and rate are equal to another RationalTime.
78-
// This is different from the operator "==" that rescales the time before
79-
// comparison.
97+
/// @brief Returns whether the value and rate are equal to another time.
98+
///
99+
/// This is different from the operator "==" that rescales the time before
100+
/// comparison.
80101
constexpr bool strictly_equal(RationalTime other) const noexcept
81102
{
82103
return _value == other._value && _rate == other._rate;
83104
}
84105

85-
// Return a RationalTime with the largest integer value not greater than
86-
// this value.
106+
/// @brief Returns a time with the largest integer value not greater than
107+
/// this value.
87108
RationalTime floor() const
88109
{
89110
return RationalTime{ std::floor(_value), _rate };
90111
}
91112

92-
// Return a RationalTime with the smallest integer value not less than
93-
// this value.
113+
/// @brief Returns a time with the smallest integer value not less than
114+
/// this value.
94115
RationalTime ceil() const
95116
{
96117
return RationalTime{ std::ceil(_value), _rate };
97118
}
98119

99-
// Return a RationalTime with the nearest integer value to this value.
120+
/// @brief Returns a time with the nearest integer value to this value.
100121
RationalTime round() const
101122
{
102123
return RationalTime{ std::round(_value), _rate };
103124
}
104125

126+
/// @brief Compute the duration of samples from first to last (excluding
127+
/// last).
128+
///
129+
/// Note that this is not the same as distance.
130+
///
131+
/// For example, the duration of a clip from frame 10 to frame 15 is 5
132+
/// frames. The result will be in the rate of start time.
133+
///
134+
/// @param start_time The start time.
135+
/// @param end_time_exclusive The exclusive end time.
105136
static constexpr RationalTime duration_from_start_end_time(
106137
RationalTime start_time,
107138
RationalTime end_time_exclusive) noexcept
@@ -116,6 +147,16 @@ class RationalTime
116147
start_time._rate };
117148
}
118149

150+
/// @brief Compute the duration of samples from first to last (including
151+
/// last).
152+
///
153+
/// Note that this is not the same as distance.
154+
///
155+
/// For example, the duration of a clip from frame 10 to frame 15 is 6
156+
/// frames. Result will be in the rate of start time.
157+
///
158+
/// @param start_time The start time.
159+
/// @param end_time_exclusive The inclusive end time.
119160
static constexpr RationalTime duration_from_start_end_time_inclusive(
120161
RationalTime start_time,
121162
RationalTime end_time_inclusive) noexcept
@@ -130,80 +171,112 @@ class RationalTime
130171
start_time._rate };
131172
}
132173

174+
/// @brief Returns true if the rate is valid for use with timecode.
133175
static bool is_valid_timecode_rate(double rate);
134176

177+
/// @brief Returns the first valid timecode rate that has the least
178+
/// difference from rate.
135179
static double nearest_valid_timecode_rate(double rate);
136180

181+
/// @brief Convert a frame number and rate into a time.
137182
static constexpr RationalTime
138183
from_frames(double frame, double rate) noexcept
139184
{
140185
return RationalTime{ double(int(frame)), rate };
141186
}
142187

188+
/// @brief Convert a value in seconds and rate into a time.
143189
static constexpr RationalTime
144190
from_seconds(double seconds, double rate) noexcept
145191
{
146192
return RationalTime{ seconds, 1 }.rescaled_to(rate);
147193
}
148194

195+
/// @brief Convert a value in seconds into a time.
149196
static constexpr RationalTime from_seconds(double seconds) noexcept
150197
{
151198
return RationalTime{ seconds, 1 };
152199
}
153200

201+
/// @brief Convert a timecode string ("HH:MM:SS;FRAME") into a time.
202+
///
203+
/// @param timecode The timecode string.
204+
/// @param rate The timecode rate.
205+
/// @param error_status Optional error status.
154206
static RationalTime from_timecode(
155207
std::string const& timecode,
156208
double rate,
157209
ErrorStatus* error_status = nullptr);
158210

159-
// parse a string in the form
160-
// hours:minutes:seconds
161-
// which may have a leading negative sign. seconds may have up to
162-
// microsecond precision.
211+
/// @brief Parse a string in the form "hours:minutes:seconds".
212+
///
213+
/// The string may have a leading negative sign.
214+
///
215+
/// Seconds may have up to microsecond precision.
216+
///
217+
/// @param time_string The time string.
218+
/// @param rate The time rate.
219+
/// @param error_status Optional error status.
163220
static RationalTime from_time_string(
164221
std::string const& time_string,
165222
double rate,
166223
ErrorStatus* error_status = nullptr);
167224

225+
/// @brief Returns the frame number based on the current rate.
168226
constexpr int to_frames() const noexcept { return int(_value); }
169227

228+
/// @brief Returns the frame number based on the given rate.
170229
constexpr int to_frames(double rate) const noexcept
171230
{
172231
return int(value_rescaled_to(rate));
173232
}
174233

234+
/// @brief Returns the value in seconds.
175235
constexpr double to_seconds() const noexcept
176236
{
177237
return value_rescaled_to(1);
178238
}
179239

240+
/// @brief Convert to timecode (e.g., "HH:MM:SS;FRAME").
241+
///
242+
/// @param rate The timecode rate.
243+
/// @param drop_frame Whether to use drop frame timecode.
244+
/// @param error_status Optional error status.
180245
std::string to_timecode(
181246
double rate,
182247
IsDropFrameRate drop_frame,
183248
ErrorStatus* error_status = nullptr) const;
184249

250+
/// @brief Convert to timecode (e.g., "HH:MM:SS;FRAME").
185251
std::string to_timecode(ErrorStatus* error_status = nullptr) const
186252
{
187253
return to_timecode(_rate, IsDropFrameRate::InferFromRate, error_status);
188254
}
189255

256+
/// @brief Convert to the nearest timecode (e.g., "HH:MM:SS;FRAME").
257+
///
258+
/// @param rate The timecode rate.
259+
/// @param drop_frame Whether to use drop frame timecode.
260+
/// @param error_status Optional error status.
190261
std::string to_nearest_timecode(
191262
double rate,
192263
IsDropFrameRate drop_frame,
193264
ErrorStatus* error_status = nullptr) const;
194265

266+
/// @brief Convert to the nearest timecode (e.g., "HH:MM:SS;FRAME").
195267
std::string to_nearest_timecode(ErrorStatus* error_status = nullptr) const
196268
{
197269
return to_nearest_timecode(_rate, IsDropFrameRate::InferFromRate, error_status);
198270
}
199271

200-
201-
// produce a string in the form
202-
// hours:minutes:seconds
203-
// which may have a leading negative sign. seconds may have up to
204-
// microsecond precision.
272+
/// @brief Return a string in the form "hours:minutes:seconds".
273+
///
274+
/// Seconds may have up to microsecond precision.
275+
///
276+
/// @return The time string, which may have a leading negative sign.
205277
std::string to_time_string() const;
206278

279+
/// @brief Add a time to this time.
207280
constexpr RationalTime const& operator+=(RationalTime other) noexcept
208281
{
209282
if (_rate < other._rate)
@@ -218,6 +291,7 @@ class RationalTime
218291
return *this;
219292
}
220293

294+
/// @brief Subtract a time from this time.
221295
constexpr RationalTime const& operator-=(RationalTime other) noexcept
222296
{
223297
if (_rate < other._rate)
@@ -232,6 +306,7 @@ class RationalTime
232306
return *this;
233307
}
234308

309+
/// @brief Return the addition of two times.
235310
friend constexpr RationalTime
236311
operator+(RationalTime lhs, RationalTime rhs) noexcept
237312
{
@@ -244,6 +319,7 @@ class RationalTime
244319
lhs._rate };
245320
}
246321

322+
/// @brief Return the subtraction of two times.
247323
friend constexpr RationalTime
248324
operator-(RationalTime lhs, RationalTime rhs) noexcept
249325
{
@@ -256,39 +332,60 @@ class RationalTime
256332
lhs._rate };
257333
}
258334

335+
/// @brief Return the negative of this time.
259336
friend constexpr RationalTime operator-(RationalTime lhs) noexcept
260337
{
261338
return RationalTime{ -lhs._value, lhs._rate };
262339
}
263340

341+
/// @brief Return whether a time is greater than another time.
264342
friend constexpr bool operator>(RationalTime lhs, RationalTime rhs) noexcept
265343
{
266344
return (lhs._value / lhs._rate) > (rhs._value / rhs._rate);
267345
}
268346

347+
/// @brief Return whether a time is greater or equal to another time.
269348
friend constexpr bool
270349
operator>=(RationalTime lhs, RationalTime rhs) noexcept
271350
{
272351
return (lhs._value / lhs._rate) >= (rhs._value / rhs._rate);
273352
}
274353

354+
/// @brief Return whether a time is less than another time.
275355
friend constexpr bool operator<(RationalTime lhs, RationalTime rhs) noexcept
276356
{
277357
return !(lhs >= rhs);
278358
}
279359

360+
/// @brief Return whether a time is less than or equal to another time.
280361
friend constexpr bool
281362
operator<=(RationalTime lhs, RationalTime rhs) noexcept
282363
{
283364
return !(lhs > rhs);
284365
}
285366

367+
/// @brief Return whether two times are equal.
368+
///
369+
/// Note that the right hand side time is rescaled to the rate of the
370+
/// left hand side time. To compare two times without scaling, use
371+
/// strictly_equal().
372+
///
373+
/// @param lhs Left hand side time.
374+
/// @param lhs Right hand side time.
286375
friend constexpr bool
287376
operator==(RationalTime lhs, RationalTime rhs) noexcept
288377
{
289378
return lhs.value_rescaled_to(rhs._rate) == rhs._value;
290379
}
291380

381+
/// @brief Return whether two times are not equal.
382+
///
383+
/// Note that the right hand side time is rescaled to the rate of the
384+
/// left hand side time. To compare two times without scaling, use
385+
/// strictly_equal().
386+
///
387+
/// @param lhs Left hand side time.
388+
/// @param lhs Right hand side time.
292389
friend constexpr bool
293390
operator!=(RationalTime lhs, RationalTime rhs) noexcept
294391
{

0 commit comments

Comments
 (0)