Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
SC::Time::HighResolutionCounter Struct Reference

An high resolution time counter. More...

#include <Time.h>

Public Member Functions

HighResolutionCountersnap ()
 Sets HighResolutionCounter to current instant
Example:
 
HighResolutionCounter offsetBy (Milliseconds ms) const
 Returns a HighResolutionCounter offset by a given number of Milliseconds.
 
bool isLaterThanOrEqualTo (HighResolutionCounter other) const
 Check if this HighResolutionCounter is later or equal to another HighResolutionCounter.
 
Relative subtractApproximate (HighResolutionCounter other) const
 Subtracts another HighResolutionCounter from this one, returning an approximate Relative.
 
HighResolutionCounter subtractExact (HighResolutionCounter other) const
 Subtracts another HighResolutionCounter from this one, returning a precise HighResolutionCounter.
 
Relative getRelative () const
 Converts to a Relative struct.
 
Nanoseconds toNanoseconds () const
 Converts to Nanoseconds.
 
Milliseconds toMilliseconds () const
 Converts to Milliseconds.
 
Seconds toSeconds () const
 Converts to Seconds.
 

Detailed Description

An high resolution time counter.

Member Function Documentation

◆ getRelative()

Relative SC::Time::HighResolutionCounter::getRelative ( ) const

Converts to a Relative struct.

◆ isLaterThanOrEqualTo()

bool SC::Time::HighResolutionCounter::isLaterThanOrEqualTo ( HighResolutionCounter other) const
nodiscard

Check if this HighResolutionCounter is later or equal to another HighResolutionCounter.

Parameters
otherThe HighResolutionCounter to be used in the comparison
Returns
true if this HighResolutionCounter is later or equal to another HighResolutionCounter
Example:
Time::HighResolutionCounter start;
start.snap();
const Time::HighResolutionCounter end = start.offsetBy(123_ms);
SC_TEST_EXPECT(end.isLaterThanOrEqualTo(start));
SC_TEST_EXPECT(not start.isLaterThanOrEqualTo(end));

◆ offsetBy()

HighResolutionCounter SC::Time::HighResolutionCounter::offsetBy ( Milliseconds ms) const
nodiscard

Returns a HighResolutionCounter offset by a given number of Milliseconds.

Parameters
msHow many Milliseconds the returned HighResolutionCounter must be offset of
Returns
A HighResolutionCounter that is offset by ms
Example:
Time::HighResolutionCounter start, end;
end = start.offsetBy(Time::Milliseconds(321));
Time::Milliseconds elapsed = end.subtractExact(start).toMilliseconds();
SC_TEST_EXPECT(elapsed == 321_ms);
// Test overflow handling
Time::Absolute maxTime(static_cast<int64_t>(INT64_MAX - 1000));
Time::Absolute overflowed = maxTime.offsetBy(Time::Milliseconds(2000));
SC_TEST_EXPECT(overflowed.milliseconds == INT64_MAX);
// Test time normalization by adding times that cause nanosecond overflow
Time::HighResolutionCounter counter;
counter = counter.offsetBy(999_ms);
// Add enough milliseconds to cause overflow from nanoseconds to seconds
counter = counter.offsetBy(2001_ms); // This will cause nanoseconds to overflow the 1e9 limit
// The total time should be 3 seconds (2001 + 999 = 3000 ms)
Time::HighResolutionCounter base;
Time::HighResolutionCounter normalized = counter.subtractExact(base);
Time::Milliseconds totalMs = normalized.toMilliseconds();
SC_TEST_EXPECT(totalMs == Time::Milliseconds(3000)); // Total time should be 3 seconds
SC_TEST_EXPECT(normalized.toSeconds() == 3_sec);
// Test time normalization by subtracting larger from smaller time
Time::HighResolutionCounter smaller = base;
smaller = smaller.offsetBy(100_ms); // Add 0.1 seconds
Time::HighResolutionCounter larger = base;
larger = larger.offsetBy(500_ms); // Add 0.5 seconds
// Subtracting larger from smaller should give normalized negative time
Time::HighResolutionCounter negDiff = smaller.subtractExact(larger);
Time::Milliseconds negDiffMs = negDiff.toMilliseconds();
SC_TEST_EXPECT(negDiffMs == Time::Milliseconds(-400)); // -0.4 seconds
// While subtracting smaller from larger gives positive time
Time::HighResolutionCounter posDiff = larger.subtractExact(smaller);
Time::Milliseconds posDiffMs = posDiff.toMilliseconds();
SC_TEST_EXPECT(posDiffMs == Time::Milliseconds(400)); // +0.4 seconds

◆ snap()

HighResolutionCounter & SC::Time::HighResolutionCounter::snap ( )

Sets HighResolutionCounter to current instant
Example:

Time::HighResolutionCounter start, end;
start.snap();
Thread::Sleep(100);
end.snap();
// Test all time conversion methods
Time::Milliseconds elapsedMs = end.subtractExact(start).toMilliseconds();
SC_TEST_EXPECT(elapsedMs < 2000_ms and elapsedMs > 0_ms);
// Test nanoseconds conversion
Time::Nanoseconds elapsedNs = end.toNanoseconds();
SC_TEST_EXPECT(elapsedNs.ns > 800000000); // More than 800ms in ns
// Test seconds conversion
Time::Seconds elapsedSec = end.toSeconds();
SC_TEST_EXPECT(elapsedSec.sec >= 0); // Should be roughly 1 second
// Test relative time and approximate subtraction
Time::Relative relativeTime = end.getRelative();
SC_TEST_EXPECT(relativeTime > Time::Relative::fromSeconds(0.8));
Time::Relative approxTime = end.subtractApproximate(start);
SC_TEST_EXPECT(approxTime > Time::Relative::fromSeconds(0.05));

◆ subtractApproximate()

Relative SC::Time::HighResolutionCounter::subtractApproximate ( HighResolutionCounter other) const
nodiscard

Subtracts another HighResolutionCounter from this one, returning an approximate Relative.

Parameters
otherThe HighResolutionCounter to be subtracted
Returns
A Relative holding the time interval between the two HighResolutionCounter
Example:
Time::HighResolutionCounter start, end;
end = start.offsetBy(Time::Milliseconds(321));
Time::Milliseconds elapsed = end.subtractExact(start).toMilliseconds();
SC_TEST_EXPECT(elapsed == 321_ms);
// Test overflow handling
Time::Absolute maxTime(static_cast<int64_t>(INT64_MAX - 1000));
Time::Absolute overflowed = maxTime.offsetBy(Time::Milliseconds(2000));
SC_TEST_EXPECT(overflowed.milliseconds == INT64_MAX);
// Test time normalization by adding times that cause nanosecond overflow
Time::HighResolutionCounter counter;
counter = counter.offsetBy(999_ms);
// Add enough milliseconds to cause overflow from nanoseconds to seconds
counter = counter.offsetBy(2001_ms); // This will cause nanoseconds to overflow the 1e9 limit
// The total time should be 3 seconds (2001 + 999 = 3000 ms)
Time::HighResolutionCounter base;
Time::HighResolutionCounter normalized = counter.subtractExact(base);
Time::Milliseconds totalMs = normalized.toMilliseconds();
SC_TEST_EXPECT(totalMs == Time::Milliseconds(3000)); // Total time should be 3 seconds
SC_TEST_EXPECT(normalized.toSeconds() == 3_sec);
// Test time normalization by subtracting larger from smaller time
Time::HighResolutionCounter smaller = base;
smaller = smaller.offsetBy(100_ms); // Add 0.1 seconds
Time::HighResolutionCounter larger = base;
larger = larger.offsetBy(500_ms); // Add 0.5 seconds
// Subtracting larger from smaller should give normalized negative time
Time::HighResolutionCounter negDiff = smaller.subtractExact(larger);
Time::Milliseconds negDiffMs = negDiff.toMilliseconds();
SC_TEST_EXPECT(negDiffMs == Time::Milliseconds(-400)); // -0.4 seconds
// While subtracting smaller from larger gives positive time
Time::HighResolutionCounter posDiff = larger.subtractExact(smaller);
Time::Milliseconds posDiffMs = posDiff.toMilliseconds();
SC_TEST_EXPECT(posDiffMs == Time::Milliseconds(400)); // +0.4 seconds

◆ subtractExact()

HighResolutionCounter SC::Time::HighResolutionCounter::subtractExact ( HighResolutionCounter other) const
nodiscard

Subtracts another HighResolutionCounter from this one, returning a precise HighResolutionCounter.

Parameters
otherThe HighResolutionCounter to be subtracted
Returns
A HighResolutionCounter holding the time interval between the two HighResolutionCounter

◆ toMilliseconds()

Milliseconds SC::Time::HighResolutionCounter::toMilliseconds ( ) const

Converts to Milliseconds.

◆ toNanoseconds()

Nanoseconds SC::Time::HighResolutionCounter::toNanoseconds ( ) const

Converts to Nanoseconds.

◆ toSeconds()

Seconds SC::Time::HighResolutionCounter::toSeconds ( ) const

Converts to Seconds.


The documentation for this struct was generated from the following file: