| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title Os/DelegateRawTime.cpp | ||
| 3 | // \brief common function implementation for Os::RawTimeInterface and Os::DelegateRawTime | ||
| 4 | // ====================================================================== | ||
| 5 | #include <Fw/Types/Assert.hpp> | ||
| 6 | #include <Os/DelegateRawTime.hpp> | ||
| 7 | #include <Os/RawTime.hpp> | ||
| 8 | |||
| 9 | namespace Os { | ||
| 10 | |||
| 11 |
1/1✓ Branch 11 taken 6 times.
|
6 | DelegateRawTime::DelegateRawTime() : m_delegate(*RawTimeInterface::getDelegate(m_handle_storage)) { |
| 12 | 6 | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 13 | 6 | } | |
| 14 | |||
| 15 | ✗ | DelegateRawTime::DelegateRawTime(RawTimeSource source) | |
| 16 | ✗ | : m_delegate(*RawTimeInterface::getDelegate(m_handle_storage, nullptr, source)), m_source(source) { | |
| 17 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 18 | ✗ | } | |
| 19 | |||
| 20 | 12 | DelegateRawTime::~DelegateRawTime() { | |
| 21 | 12 | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 22 | 12 | m_delegate.~RawTimeInterface(); | |
| 23 | 12 | } | |
| 24 | |||
| 25 | // m_handle_storage is placement-new storage populated by getDelegate below | ||
| 26 | // cppcheck-suppress missingMemberCopy | ||
| 27 | ✗ | DelegateRawTime::DelegateRawTime(const DelegateRawTime& other) | |
| 28 | ✗ | : m_delegate(*RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate, other.m_source)), | |
| 29 | ✗ | m_source(other.m_source) { | |
| 30 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 31 | ✗ | } | |
| 32 | |||
| 33 | ✗ | DelegateRawTime& DelegateRawTime::operator=(const DelegateRawTime& other) { | |
| 34 | ✗ | if (this != &other) { | |
| 35 | ✗ | this->m_source = other.m_source; | |
| 36 | ✗ | this->m_delegate = *RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate, other.m_source); | |
| 37 | } | ||
| 38 | ✗ | return *this; | |
| 39 | } | ||
| 40 | |||
| 41 | 3 | RawTimeHandle* DelegateRawTime::getHandle() { | |
| 42 | 3 | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 43 | 3 | return this->m_delegate.getHandle(); | |
| 44 | } | ||
| 45 | |||
| 46 | 6 | DelegateRawTime::Status DelegateRawTime::now() { | |
| 47 | 6 | FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 48 | 6 | return this->m_delegate.now(); | |
| 49 | } | ||
| 50 | |||
| 51 | 3 | DelegateRawTime::Status DelegateRawTime::getTimeInterval(const Os::RawTime& other, Fw::TimeInterval& result) const { | |
| 52 | 3 | FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 53 | 3 | return this->m_delegate.getTimeInterval(other, result); | |
| 54 | } | ||
| 55 | |||
| 56 | ✗ | Fw::SerializeStatus DelegateRawTime::serializeTo(Fw::SerialBufferBase& buffer, Fw::Endianness mode) const { | |
| 57 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 58 | ✗ | return this->m_delegate.serializeTo(buffer, mode); | |
| 59 | } | ||
| 60 | |||
| 61 | ✗ | Fw::SerializeStatus DelegateRawTime::deserializeFrom(Fw::SerialBufferBase& buffer, Fw::Endianness mode) { | |
| 62 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 63 | ✗ | return this->m_delegate.deserializeFrom(buffer, mode); | |
| 64 | } | ||
| 65 | |||
| 66 | ✗ | DelegateRawTime::Status DelegateRawTime::getDiffUsec(const RawTime& other, U32& result) const { | |
| 67 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 68 | ✗ | return this->m_delegate.getDiffUsec(other, result); | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | bool DelegateRawTime::operator==(const RawTime& other) const { | |
| 72 | ✗ | FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0])); | |
| 73 | ✗ | return this->m_delegate == other; | |
| 74 | } | ||
| 75 | |||
| 76 | ✗ | RawTimeSource DelegateRawTime::getSource() const { | |
| 77 | ✗ | return this->m_source; | |
| 78 | } | ||
| 79 | |||
| 80 | // ------------------------------------------------------------ | ||
| 81 | // RawTimeInterface default implementations | ||
| 82 | // Built on pure virtual methods. Located here (not RawTimeInterface.cpp) | ||
| 83 | // to keep RawTime implementation code in one translation unit. | ||
| 84 | // ------------------------------------------------------------ | ||
| 85 | |||
| 86 | ✗ | RawTimeInterface::Status RawTimeInterface::getDiffUsec(const RawTime& other, U32& result) const { | |
| 87 | ✗ | Fw::TimeInterval interval; | |
| 88 | ✗ | Status status = this->getTimeInterval(other, interval); | |
| 89 | ✗ | if (status != Status::OP_OK) { | |
| 90 | ✗ | return status; | |
| 91 | } | ||
| 92 | |||
| 93 | // Check overflows in computation | ||
| 94 | ✗ | U32 seconds = interval.getSeconds(); | |
| 95 | ✗ | U32 useconds = interval.getUSeconds(); | |
| 96 | ✗ | if (seconds > (std::numeric_limits<U32>::max() / 1000000)) { | |
| 97 | ✗ | result = std::numeric_limits<U32>::max(); | |
| 98 | ✗ | return Status::OP_OVERFLOW; | |
| 99 | } | ||
| 100 | ✗ | U32 secToUsec = seconds * 1000000; | |
| 101 | ✗ | if (secToUsec > (std::numeric_limits<U32>::max() - useconds)) { | |
| 102 | ✗ | result = std::numeric_limits<U32>::max(); | |
| 103 | ✗ | return Status::OP_OVERFLOW; | |
| 104 | } | ||
| 105 | // No overflow, we can safely add values to get total microseconds | ||
| 106 | ✗ | result = secToUsec + useconds; | |
| 107 | ✗ | return status; | |
| 108 | ✗ | } | |
| 109 | |||
| 110 | ✗ | bool RawTimeInterface::operator==(const RawTime& other) const { | |
| 111 | ✗ | Fw::TimeInterval interval; | |
| 112 | ✗ | Status status = this->getTimeInterval(other, interval); | |
| 113 | // If we error out, then the values are either: | ||
| 114 | // 1) impossible to compare, in which case it's perfectly reasonable to consider them different, or | ||
| 115 | // 2) too far apart to fit in a TimeInterval, in which case they are definitely different | ||
| 116 | ✗ | return status == Status::OP_OK && interval.getSeconds() == 0 && interval.getUSeconds() == 0; | |
| 117 | ✗ | } | |
| 118 | |||
| 119 | } // namespace Os | ||
| 120 |