fprime/Os/RawTime.cpp
Vince Woo 48e4720419
Created new SerialBufferBase as a parent of SerializeBufferBase (now renamed LinearBufferBase). (#4288)
* Created new SerialBufferBase as a parent of SerializeBufferBase. Renaming interface functions to be less confusing.

* Deprecating copyRawOffset. No direct use-cases in F' core.

* Make SerialBufferBase a true pure virtual interface.

* Changing Serializable to work with SerialBufferBase parent interface.

* Changing copyRaw and copyRawOffset to work with SerialBufferBase

* Updating documentation for SerialBufferBase usage

* Adding some documentation. Adding missing ASSERT in copyRaw. Fixing some bugs that new ASSERT uncovered.

* Renaming SerializeBufferBase to LinearBufferBase. Add a using declaration to maintain backwards compatability. Properly mark LinearBufferBase functions as override.

* Filling in the rest of the docstrings for the classes in Serializable

* Removing redundant virtual keyword on override function

* Applying clang formatting

* Incorporating PR comments

* Fix compile issues

* Bump version to alpha

* Format

* v

---------

Co-authored-by: M Starch <LeStarch@googlemail.com>
2025-11-06 16:23:20 -08:00

90 lines
3.6 KiB
C++

// ======================================================================
// \title Os/RawTime.cpp
// \brief common function implementation for Os::RawTime
// ======================================================================
#include <Fw/Types/Assert.hpp>
#include <Os/RawTime.hpp>
namespace Os {
RawTime::RawTime() : m_handle_storage(), m_delegate(*RawTimeInterface::getDelegate(m_handle_storage)) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
}
RawTime::~RawTime() {
FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
m_delegate.~RawTimeInterface();
}
RawTime::RawTime(const RawTime& other)
: m_handle_storage(), m_delegate(*RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
}
RawTime& RawTime::operator=(const RawTime& other) {
if (this != &other) {
this->m_delegate = *RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate);
}
return *this;
}
RawTimeHandle* RawTime::getHandle() {
FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.getHandle();
}
RawTime::Status RawTime::now() {
FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.now();
}
RawTime::Status RawTime::getTimeInterval(const Os::RawTime& other, Fw::TimeInterval& result) const {
FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.getTimeInterval(other, result);
}
Fw::SerializeStatus RawTime::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const {
FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.serializeTo(buffer, mode);
}
Fw::SerializeStatus RawTime::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.deserializeFrom(buffer, mode);
}
RawTime::Status RawTime::getDiffUsec(const RawTime& other, U32& result) const {
Fw::TimeInterval interval;
Status status = this->getTimeInterval(other, interval);
if (status != Status::OP_OK) {
return status;
}
// Check overflows in computation
U32 seconds = interval.getSeconds();
U32 useconds = interval.getUSeconds();
if (seconds > (std::numeric_limits<U32>::max() / 1000000)) {
result = std::numeric_limits<U32>::max();
return Status::OP_OVERFLOW;
}
U32 secToUsec = seconds * 1000000;
if (secToUsec > (std::numeric_limits<U32>::max() - useconds)) {
result = std::numeric_limits<U32>::max();
return Status::OP_OVERFLOW;
}
// No overflow, we can safely add values to get total microseconds
result = secToUsec + useconds;
return status;
}
bool RawTime::operator==(const RawTime& other) const {
Fw::TimeInterval interval;
Status status = this->getTimeInterval(other, interval);
// If we error out, then the values are either:
// 1) impossible to compare, in which case it's perfectly reasonable to consider them different, or
// 2) too far apart to fit in a TimeInterval, in which case they are definitely different
return status == Status::OP_OK && interval.getSeconds() == 0 && interval.getUSeconds() == 0;
}
} // namespace Os