F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
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 
8 namespace Os {
9 
10 RawTime::RawTime() : 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 
19 // m_handle_storage is placement-new storage populated by getDelegate below
20 // cppcheck-suppress missingMemberCopy
22  : m_delegate(*RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate)) {
23  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
24 }
25 
27  if (this != &other) {
28  this->m_delegate = *RawTimeInterface::getDelegate(m_handle_storage, &other.m_delegate);
29  }
30  return *this;
31 }
32 
34  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
35  return this->m_delegate.getHandle();
36 }
37 
39  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
40  return this->m_delegate.now();
41 }
42 
44  FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
45  return this->m_delegate.getTimeInterval(other, result);
46 }
47 
49  FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
50  return this->m_delegate.serializeTo(buffer, mode);
51 }
52 
54  FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
55  return this->m_delegate.deserializeFrom(buffer, mode);
56 }
57 
58 RawTime::Status RawTime::getDiffUsec(const RawTime& other, U32& result) const {
59  FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
60  return this->m_delegate.getDiffUsec(other, result);
61 }
62 
64  Fw::TimeInterval interval;
65  Status status = this->getTimeInterval(other, interval);
66  if (status != Status::OP_OK) {
67  return status;
68  }
69 
70  // Check overflows in computation
71  U32 seconds = interval.getSeconds();
72  U32 useconds = interval.getUSeconds();
73  if (seconds > (std::numeric_limits<U32>::max() / 1000000)) {
74  result = std::numeric_limits<U32>::max();
75  return Status::OP_OVERFLOW;
76  }
77  U32 secToUsec = seconds * 1000000;
78  if (secToUsec > (std::numeric_limits<U32>::max() - useconds)) {
79  result = std::numeric_limits<U32>::max();
80  return Status::OP_OVERFLOW;
81  }
82  // No overflow, we can safely add values to get total microseconds
83  result = secToUsec + useconds;
84  return status;
85 }
86 
87 bool RawTime::operator==(const RawTime& other) const {
88  Fw::TimeInterval interval;
89  Status status = this->getTimeInterval(other, interval);
90  // If we error out, then the values are either:
91  // 1) impossible to compare, in which case it's perfectly reasonable to consider them different, or
92  // 2) too far apart to fit in a TimeInterval, in which case they are definitely different
93  return status == Status::OP_OK && interval.getSeconds() == 0 && interval.getUSeconds() == 0;
94 }
95 
96 } // namespace Os
Operation succeeded.
Definition: Os.hpp:27
Status getDiffUsec(const RawTime &other, U32 &result) const override
Calculate the difference in microseconds between two RawTime objects.
Definition: RawTime.cpp:58
virtual Status now()=0
Get the current time.
Status now() override
Get the current time.
Definition: RawTime.cpp:38
RawTime()
Constructor.
Definition: RawTime.cpp:10
virtual Status getDiffUsec(const RawTime &other, U32 &result) const
Calculate the difference in microseconds between two RawTime objects.
Definition: RawTime.cpp:63
static RawTimeInterface * getDelegate(RawTimeHandleStorage &aligned_new_memory, const RawTimeInterface *to_copy=nullptr)
provide a pointer to a RawTime delegate object
virtual ~RawTimeInterface()=default
default virtual destructor
SerializeStatus
forward declaration for string
RawTimeHandle * getHandle() override
return the underlying RawTime handle (implementation specific)
Definition: RawTime.cpp:33
virtual Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const =0
Serialize the contents of the RawTimeInterface object into a buffer.
virtual RawTimeHandle * getHandle()=0
return the underlying RawTime handle (implementation specific)
bool operator==(const RawTime &other) const
Compare whether two RawTime objects are the same (i.e. refer to the same microsecond) ...
Definition: RawTime.cpp:87
~RawTime() final
Destructor.
Definition: RawTime.cpp:14
U32 getUSeconds() const
RawTime & operator=(const RawTime &other)
assignment operator that copies the internal representation
Definition: RawTime.cpp:26
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const override
Serialize the contents of the RawTimeInterface object into a buffer.
Definition: RawTime.cpp:48
Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const override
Calculate the time interval between this and another raw time.
Definition: RawTime.cpp:43
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) override
Deserialize the contents of the RawTimeInterface object from a buffer.
Definition: RawTime.cpp:53
Endianness
virtual Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const =0
Calculate the time interval between this and another raw time.
virtual Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG)=0
Deserialize the contents of the RawTimeInterface object from a buffer.
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const