F´ Flight Software - C/C++ Documentation  devel
A framework for building embedded system applications to NASA flight quality standards.
TimeInterval.cpp
Go to the documentation of this file.
2 #include <FpConfig.hpp>
3 
4 namespace Fw {
6  this->set(other.m_seconds,other.m_useconds);
7  }
8 
9  TimeInterval::TimeInterval(U32 seconds, U32 useconds) : Serializable() {
10  this->set(seconds,useconds);
11  }
12 
13  void TimeInterval::set(U32 seconds, U32 useconds) {
14  this->m_seconds = seconds;
15  this->m_useconds = useconds;
16  }
17 
19  this->m_useconds = other.m_useconds;
20  this->m_seconds = other.m_seconds;
21 
22  return *this;
23  }
24 
25  bool TimeInterval::operator==(const TimeInterval& other) const {
26  return (TimeInterval::compare(*this,other) == EQ);
27  }
28 
29  bool TimeInterval::operator!=(const TimeInterval& other) const {
30  return (TimeInterval::compare(*this,other) != EQ);
31  }
32 
33  bool TimeInterval::operator>(const TimeInterval& other) const {
34  return (TimeInterval::compare(*this,other) == GT);
35  }
36 
37  bool TimeInterval::operator<(const TimeInterval& other) const {
38  return (TimeInterval::compare(*this,other) == LT);
39  }
40 
41  bool TimeInterval::operator>=(const TimeInterval& other) const {
43  return ((GT == c) or (EQ == c));
44  }
45 
46  bool TimeInterval::operator<=(const TimeInterval& other) const {
48  return ((LT == c) or (EQ == c));
49  }
50 
52  // serialize members
54  stat = buffer.serialize(this->m_seconds);
55  if (stat != FW_SERIALIZE_OK) {
56  return stat;
57  }
58  return buffer.serialize(this->m_useconds);
59  }
60 
62 
64  stat = buffer.deserialize(this->m_seconds);
65  if (stat != FW_SERIALIZE_OK) {
66  return stat;
67  }
68 
69  return buffer.deserialize(this->m_useconds);
70  }
71 
73  return this->m_seconds;
74  }
75 
77  return this->m_useconds;
78  }
79 
81  compare(
82  const TimeInterval &time1,
83  const TimeInterval &time2
84  )
85  {
86  const U32 s1 = time1.getSeconds();
87  const U32 s2 = time2.getSeconds();
88  const U32 us1 = time1.getUSeconds();
89  const U32 us2 = time2.getUSeconds();
90 
91  if (s1 < s2) {
92  return LT;
93  } else if (s1 > s2) {
94  return GT;
95  } else if (us1 < us2) {
96  return LT;
97  } else if (us1 > us2) {
98  return GT;
99  } else {
100  return EQ;
101  }
102  }
103 
105  add(
106  const TimeInterval& a,
107  const TimeInterval& b
108  )
109  {
110  U32 seconds = a.getSeconds() + b.getSeconds();
111  U32 uSeconds = a.getUSeconds() + b.getUSeconds();
112  FW_ASSERT(uSeconds < 1999999);
113  if (uSeconds >= 1000000) {
114  ++seconds;
115  uSeconds -= 1000000;
116  }
117  TimeInterval c(seconds,uSeconds);
118  return c;
119  }
120 
122  sub(
123  const TimeInterval& t1,
124  const TimeInterval& t2
125  )
126  {
127  const TimeInterval& minuend = (t1 > t2) ? t1 : t2;
128  const TimeInterval& subtrahend = (t1 > t2) ? t2 : t1;
129 
130  U32 seconds = minuend.getSeconds() - subtrahend.getSeconds();
131  U32 uSeconds;
132  if (subtrahend.getUSeconds() > minuend.getUSeconds()) {
133  seconds--;
134  uSeconds = minuend.getUSeconds() + 1000000 - subtrahend.getUSeconds();
135  } else {
136  uSeconds = minuend.getUSeconds() - subtrahend.getUSeconds();
137  }
138  return TimeInterval(seconds, static_cast<U32>(uSeconds));
139  }
140 
141  void TimeInterval::add(U32 seconds, U32 useconds) {
142  this->m_seconds += seconds;
143  this->m_useconds += useconds;
144  FW_ASSERT(this->m_useconds < 1999999, static_cast<FwAssertArgType>(this->m_useconds));
145  if (this->m_useconds >= 1000000) {
146  this->m_seconds += 1;
147  this->m_useconds -= 1000000;
148  }
149  }
150 
151 #ifdef BUILD_UT
152  std::ostream& operator<<(std::ostream& os, const TimeInterval& val) {
153 
154  os << "(" << val.getSeconds() << "s," << val.getUSeconds() << "us)";
155  return os;
156  }
157 #endif
158 
159 }
#define FW_ASSERT(...)
Definition: Assert.hpp:14
PlatformAssertArgType FwAssertArgType
Definition: FpConfig.h:39
C++-compatible configuration header for fprime configuration.
forward declaration
SerializeStatus deserialize(U8 &val)
deserialize 8-bit unsigned int
SerializeStatus serialize(U8 val)
serialize 8-bit unsigned int
TimeInterval()=default
void add(U32 seconds, U32 mseconds)
TimeInterval & operator=(const TimeInterval &other)
static Comparison compare(const TimeInterval &time1, const TimeInterval &time2)
bool operator!=(const TimeInterval &other) const
bool operator<(const TimeInterval &other) const
U32 getSeconds() const
SerializeStatus serialize(SerializeBufferBase &buffer) const
serialize contents
static TimeInterval sub(const TimeInterval &t1, const TimeInterval &t2)
bool operator>=(const TimeInterval &other) const
Comparison
The type of a comparison result.
bool operator==(const TimeInterval &other) const
bool operator<=(const TimeInterval &other) const
SerializeStatus deserialize(SerializeBufferBase &buffer)
deserialize to contents
void set(U32 seconds, U32 useconds)
bool operator>(const TimeInterval &other) const
U32 getUSeconds() const
SerializeStatus
forward declaration for string
@ FW_SERIALIZE_OK
Serialization/Deserialization operation was successful.