F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
DelegateRawTime.cpp
Go to the documentation of this file.
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 DelegateRawTime::DelegateRawTime() : m_delegate(*RawTimeInterface::getDelegate(m_handle_storage)) {
12  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
13 }
14 
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 
21  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
22  m_delegate.~RawTimeInterface();
23 }
24 
25 // m_handle_storage is placement-new storage populated by getDelegate below
26 // cppcheck-suppress missingMemberCopy
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 
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 
42  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
43  return this->m_delegate.getHandle();
44 }
45 
47  FW_ASSERT(&this->m_delegate == reinterpret_cast<RawTimeInterface*>(&this->m_handle_storage[0]));
48  return this->m_delegate.now();
49 }
50 
52  FW_ASSERT(&this->m_delegate == reinterpret_cast<const RawTimeInterface*>(&this->m_handle_storage[0]));
53  return this->m_delegate.getTimeInterval(other, result);
54 }
55 
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 
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 
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 
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 
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
RawTimeSource
Timer source selection for RawTime.
Operation succeeded.
Definition: Os.hpp:27
RawTimeHandle * getHandle() override
return the underlying RawTime handle (implementation specific)
RawTimeSource getSource() const
Get the timer source used by this RawTime instance.
virtual Status now()=0
Get the current time.
Status now() override
Get the current time.
virtual Status getDiffUsec(const RawTime &other, U32 &result) const
Calculate the difference in microseconds between two RawTime objects.
virtual ~RawTimeInterface()=default
default virtual destructor
SerializeStatus
forward declaration for string
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)
virtual bool operator==(const RawTime &other) const
Compare whether two RawTime objects are the same (i.e. refer to the same microsecond) ...
Status getTimeInterval(const Os::RawTime &other, Fw::TimeInterval &interval) const override
Calculate the time interval between this and another raw time.
U32 getUSeconds() const
Fw::SerializeStatus deserializeFrom(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) override
Deserialize the contents of the RawTimeInterface object from a buffer.
bool operator==(const RawTime &other) const override
Compare whether two RawTime objects are the same (i.e. refer to the same microsecond) ...
Fw::SerializeStatus serializeTo(Fw::SerialBufferBase &buffer, Fw::Endianness mode=Fw::Endianness::BIG) const override
Serialize the contents of the RawTimeInterface object into a buffer.
Status getDiffUsec(const RawTime &other, U32 &result) const override
Calculate the difference in microseconds between two RawTime objects.
~DelegateRawTime() final
Destructor.
static RawTimeInterface * getDelegate(RawTimeHandleStorage &aligned_new_memory, const RawTimeInterface *to_copy=nullptr, RawTimeSource source=RAWTIME_DEFAULT)
provide a pointer to a RawTime delegate object
DelegateRawTime()
Default constructor.
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.
DelegateRawTime & operator=(const DelegateRawTime &other)
assignment operator that copies the internal representation
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const