fprime/Os/RawTime.cpp
Vince Woo f38010abca
(De)Serialization clean up of temporary workarounds (#3971)
* Staging DEPRECATE changes in preparation for new FPP alpha. Removed backwards compat hacks. Updated some uses of the legacy functions that were missed originally.

* Remove declaration of serialize and deserialize from RawTime as those should be inherited now

* Removing FW_SERIALIZE_UNIMPLEMENTED and FW_DESERIALIZE_UNIMPLEMENTED from SerializeStatus

* Removing superfluous comma

* Missed AmpcsEvrLogPacket. Fixing a minor whitespace issue in Serializable.

* Un-deprecating serialize/deserialize for this release

* Restoring DEPRECATED for the noLength functions

* Pulling in fpp 3.0.0a17. Fixing some lingering issues with refactoring.

* Format files

* Update fprime-fpp package

* Fixing some lingering issues with serialization modernization.

* Fixing weird merge issue with AmpcsEvrLogPacket

* More clang issue fixes

* Still pesky clang formatting issues. Superfluous whitespace.

* Removing redundant virtuals for overriden methods

* Incorporating PR comments

* clang formatted Serializable.hpp

* Removing redundant serialize and deserialize methods in TestAbsType. Inherit from parent.

---------

Co-authored-by: thomas-bc <thomas.boyerchammard@gmail.com>
2025-08-06 11:50:32 -07:00

90 lines
3.5 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::SerializeBufferBase& buffer) const {
FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.serializeTo(buffer);
}
Fw::SerializeStatus RawTime::deserializeFrom(Fw::SerializeBufferBase& buffer) {
FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
return this->m_delegate.deserializeFrom(buffer);
}
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