F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
RawTime.cpp
Go to the documentation of this file.
1// ======================================================================
2// \title Os/RawTime.cpp
3// \brief common function implementation for Os::RawTime
4// ======================================================================
5#include <Fw/Types/Assert.hpp>
6#include <Os/RawTime.hpp>
7
8namespace Os {
9
10RawTime::RawTime() : m_handle_storage(), m_delegate(*RawTimeInterface::getDelegate(m_handle_storage)) {
11 FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
12}
13
15 FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
16 m_delegate.~RawTimeInterface();
17}
18
20 : m_handle_storage(), m_delegate(*RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
21 FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
22}
23
25 if (this != &other) {
26 this->m_delegate = *RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate);
27 }
28 return *this;
29}
30
32 FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
33 return this->m_delegate.getHandle();
34}
35
37 FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
38 return this->m_delegate.now();
39}
40
42 FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
43 return this->m_delegate.getTimeInterval(other, result);
44}
45
47 FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
48 return this->m_delegate.serialize(buffer);
49}
50
52 FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
53 return this->m_delegate.deserialize(buffer);
54}
55
56RawTime::Status RawTime::getDiffUsec(const RawTime& other, U32& result) const {
57 Fw::TimeInterval interval;
58 Status status = this->getTimeInterval(other, interval);
59 if (status != Status::OP_OK) {
60 return status;
61 }
62
63 // Check overflows in computation
64 U32 seconds = interval.getSeconds();
65 U32 useconds = interval.getUSeconds();
66 if (seconds > (std::numeric_limits<U32>::max() / 1000000)) {
67 result = std::numeric_limits<U32>::max();
69 }
70 U32 secToUsec = seconds * 1000000;
71 if (secToUsec > (std::numeric_limits<U32>::max() - useconds)) {
72 result = std::numeric_limits<U32>::max();
74 }
75 // No overflow, we can safely add values to get total microseconds
76 result = secToUsec + useconds;
77 return status;
78}
79
80} // namespace Os
#define FW_ASSERT(...)
Definition Assert.hpp:14
U32 getSeconds() const
U32 getUSeconds() const
RawTimeHandle * getHandle() override
return the underlying RawTime handle (implementation specific)
Definition RawTime.cpp:31
RawTime()
Constructor.
Definition RawTime.cpp:10
~RawTime() final
Destructor.
Definition RawTime.cpp:14
Status getDiffUsec(const RawTime &other, U32 &result) const
Calculate the difference in microseconds between two RawTime objects.
Definition RawTime.cpp:56
Status now() override
Get the current time.
Definition RawTime.cpp:36
Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const override
Calculate the time interval between this and another raw time.
Definition RawTime.cpp:41
RawTime & operator=(const RawTime &other)
assignment operator that copies the internal representation
Definition RawTime.cpp:24
Fw::SerializeStatus serialize(Fw::SerializeBufferBase &buffer) const override
Serialize the contents of the RawTimeInterface object into a buffer.
Definition RawTime.cpp:46
Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer) override
Deserialize the contents of the RawTimeInterface object from a buffer.
Definition RawTime.cpp:51
@ OP_OK
Operation was successful.
Definition RawTime.hpp:28
@ OP_OVERFLOW
Operation result caused an overflow.
Definition RawTime.hpp:29
virtual ~RawTimeInterface()=default
default virtual destructor
virtual Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const =0
Calculate the time interval between this and another raw time.
virtual Status now()=0
Get the current time.
static RawTimeInterface * getDelegate(RawTimeHandleStorage &aligned_new_memory, const RawTimeInterface *to_copy=nullptr)
provide a pointer to a RawTime delegate object
virtual RawTimeHandle * getHandle()=0
return the underlying RawTime handle (implementation specific)
virtual Fw::SerializeStatus serialize(Fw::SerializeBufferBase &buffer) const =0
Serialize the contents of the RawTimeInterface object into a buffer.
virtual Fw::SerializeStatus deserialize(Fw::SerializeBufferBase &buffer)=0
Deserialize the contents of the RawTimeInterface object from a buffer.
SerializeStatus
forward declaration for string