QP/C++  6.9.3
Real-Time Embedded Framework
qpset.hpp
Go to the documentation of this file.
1 
39 #ifndef QPSET_HPP
40 #define QPSET_HPP
41 
42 #ifndef QF_MAX_ACTIVE
43  // default value when NOT defined
44  #define QF_MAX_ACTIVE 32U
45 #endif
46 
47 namespace QP {
48 
49 #if (QF_MAX_ACTIVE < 1U) || (64U < QF_MAX_ACTIVE)
50  #error "QF_MAX_ACTIVE out of range. Valid range is 1U..64U"
51 #elif (QF_MAX_ACTIVE <= 8U)
53 #elif (QF_MAX_ACTIVE <= 16U)
54  using QPSetBits = std::uint16_t;
55 #else
57  using QPSetBits = std::uint32_t;
58 #endif
59 
60 //****************************************************************************
61 // Log-base-2 calculations ...
62 #ifndef QF_LOG2
63  extern "C" std::uint_fast8_t QF_LOG2(QPSetBits x) noexcept;
64 #endif // QF_LOG2
65 
66 //****************************************************************************
67 #if (QF_MAX_ACTIVE <= 32)
76 struct QPSet {
77 
78  QPSetBits volatile m_bits;
79 
81  void setEmpty(void) noexcept {
82  m_bits = 0U;
83  }
84 
86  bool isEmpty(void) const noexcept {
87  return (m_bits == 0U);
88  }
89 
91  bool notEmpty(void) const noexcept {
92  return (m_bits != 0U);
93  }
94 
96  bool hasElement(std::uint_fast8_t const n) const noexcept {
97  return (m_bits & (1U << (n - 1U))) != 0U;
98  }
99 
101  void insert(std::uint_fast8_t const n) noexcept {
102  m_bits |= (1U << (n - 1U));
103  }
104 
109  void rmove(std::uint_fast8_t const n) noexcept {
110  m_bits &=
111  static_cast<QPSetBits>(~(static_cast<QPSetBits>(1) << (n - 1U)));
112  }
113 
114  std::uint_fast8_t findMax(void) const noexcept {
115  return QF_LOG2(m_bits);
116  }
117 };
118 
119 #else // QF_MAX_ACTIVE > 32U
120 
129 struct QPSet {
130 
132  std::uint32_t volatile m_bits[2];
133 
135  void setEmpty(void) noexcept {
136  m_bits[0] = 0U;
137  m_bits[1] = 0U;
138  }
139 
141  // the following logic avoids UB in volatile access for MISRA compliantce
142  bool isEmpty(void) const noexcept {
143  return (m_bits[0] == 0U) ? (m_bits[1] == 0U) : false;
144  }
145 
147  // the following logic avoids UB in volatile access for MISRA compliantce
148  bool notEmpty(void) const noexcept {
149  return (m_bits[0] != 0U) ? true : (m_bits[1] != 0U);
150  }
151 
153  bool hasElement(std::uint_fast8_t const n) const noexcept {
154  return (n <= 32U)
155  ? ((m_bits[0] & (static_cast<std::uint32_t>(1) << (n - 1U)))
156  != 0U)
157  : ((m_bits[1] & (static_cast<std::uint32_t>(1) << (n - 33U)))
158  != 0U);
159  }
160 
162  void insert(std::uint_fast8_t const n) noexcept {
163  if (n <= 32U) {
164  m_bits[0] |= (static_cast<std::uint32_t>(1) << (n - 1U));
165  }
166  else {
167  m_bits[1] |= (static_cast<std::uint32_t>(1) << (n - 33U));
168  }
169  }
170 
175  void rmove(std::uint_fast8_t const n) noexcept {
176  if (n <= 32U) {
177  (m_bits[0] &= ~(static_cast<std::uint32_t>(1) << (n - 1U)));
178  }
179  else {
180  (m_bits[1] &= ~(static_cast<std::uint32_t>(1) << (n - 33U)));
181  }
182  }
183 
185  std::uint_fast8_t findMax(void) const noexcept {
186  return (m_bits[1] != 0U)
187  ? (QF_LOG2(m_bits[1]) + 32U)
188  : (QF_LOG2(m_bits[0]));
189  }
190 };
191 
192 #endif // QF_MAX_ACTIVE
193 
194 } // namespace QP
195 
196 #endif // QPSET_HPP
197 
unsigned int uint16_t
Definition: 16bit/stdint.h:30
unsigned long int uint32_t
Definition: 16bit/stdint.h:31
unsigned char uint8_t
Definition: 16bit/stdint.h:29
unsigned int uint_fast8_t
Definition: 16bit/stdint.h:36
Definition: struct.dox:1
std::uint8_t QPSetBits
Definition: qpset.hpp:52
std::uint_fast8_t QF_LOG2(QPSetBits x) noexcept
bool isEmpty(void) const noexcept
Evaluates to true if the priority set is empty.
Definition: qpset.hpp:86
bool hasElement(std::uint_fast8_t const n) const noexcept
the function evaluates to TRUE if the priority set has the element n.
Definition: qpset.hpp:96
void insert(std::uint_fast8_t const n) noexcept
insert element n into the set, n = 1..QF_MAX_ACTIVE
Definition: qpset.hpp:101
void rmove(std::uint_fast8_t const n) noexcept
Definition: qpset.hpp:109
QPSetBits volatile m_bits
bitmask with a bit for each element
Definition: qpset.hpp:78
bool notEmpty(void) const noexcept
Evaluates to true if the priority set is not empty.
Definition: qpset.hpp:91
void setEmpty(void) noexcept
Makes the priority set me_ empty.
Definition: qpset.hpp:81
std::uint_fast8_t findMax(void) const noexcept
Definition: qpset.hpp:114