17 #ifndef IOX_UTILS_CXX_HELPLETS_HPP
18 #define IOX_UTILS_CXX_HELPLETS_HPP
20 #include "iceoryx_utils/cxx/generic_raii.hpp"
21 #include "iceoryx_utils/platform/platform_correction.hpp"
27 #include <type_traits>
36 Require(
const bool condition,
const char* file,
const int line,
const char*
function,
const char* conditionString)
40 std::cerr <<
"Condition: " << conditionString <<
" in " <<
function <<
" is violated. (" << file <<
":" << line
47 template <
bool GreaterU
int8,
bool GreaterU
int16,
bool GreaterU
int32>
50 using Type_t = uint64_t;
56 using Type_t = uint8_t;
62 using Type_t = uint16_t;
68 using Type_t = uint32_t;
75 #define Expects(condition) internal::Require(condition, __FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
80 #define Ensures(condition) internal::Require(condition, __FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
82 template <typename T, typename = typename std::enable_if<std::is_pointer<T>::value,
void>::type>
89 Expects(t !=
nullptr);
92 constexpr
operator T()
const
101 template <
typename T, T Minimum>
108 Expects(t >= Minimum);
111 constexpr
operator T()
const
120 template <
typename T, T Minimum, T Maximum>
127 Expects(t >= Minimum && t <= Maximum);
130 constexpr
operator T()
const
139 template <
typename T>
140 T align(
const T value,
const T alignment)
142 T remainder = value % alignment;
143 return value + ((remainder == 0u) ? 0u : alignment - remainder);
150 void* alignedAlloc(
const uint64_t alignment,
const uint64_t size) noexcept;
154 void alignedFree(
void*
const memory);
157 template <
size_t s = 0>
158 constexpr
size_t maxAlignment()
164 template <
typename T,
typename... Args>
165 constexpr
size_t maxAlignment()
167 return alignof(T) > maxAlignment<Args...>() ?
alignof(T) : maxAlignment<Args...>();
171 template <
size_t s = 0>
172 constexpr
size_t maxSize()
178 template <
typename T,
typename... Args>
179 constexpr
size_t maxSize()
181 return sizeof(T) > maxSize<Args...>() ?
sizeof(T) : maxSize<Args...>();
191 template <
typename T,
typename... CTorArgs>
192 GenericRAII makeScopedStatic(T& memory, CTorArgs&&... ctorArgs)
194 memory.emplace(std::forward<CTorArgs>(ctorArgs)...);
195 return GenericRAII([] {}, [&memory] { memory.reset(); });
198 template <
typename T,
typename Enumeration>
199 const char* convertEnumToString(T port,
const Enumeration source)
201 return port[
static_cast<size_t>(source)];
205 template <
typename enum_type>
206 auto enumTypeAsUnderlyingType(enum_type
const value) ->
typename std::underlying_type<enum_type>::type
208 return static_cast<typename std::underlying_type<enum_type>::type
>(value);
216 template <
typename Container,
typename Functor>
217 void forEach(Container& c,
const Functor& f) noexcept
219 for (
auto& element : c)
229 template <u
int64_t SizeValue>
230 static constexpr uint64_t strlen2(
char const (&)[SizeValue])
232 return SizeValue - 1;
236 template <u
int64_t Value>
240 #pragma GCC diagnostic push
241 #pragma GCC diagnostic ignored "-Wtype-limits"
243 (Value > std::numeric_limits<uint16_t>::max()),
244 (Value > std::numeric_limits<uint32_t>::max())>::
Type_t;
245 #pragma GCC diagnostic pop
248 template <u
int64_t Value>
253 constexpr
bool isCompiledOn32BitSystem()
255 return INTPTR_MAX == INT32_MAX;
260 template <
typename T>
261 constexpr
bool isPowerOfTwo(
const T n)
263 static_assert(std::is_unsigned<T>::value && !std::is_same<T, bool>::value,
"Only unsigned integer are allowed!");
264 return n && ((n & (n - 1U)) == 0U);
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28
get the best fitting unsigned integer type for a given value at compile time
Definition: helplets.hpp:238
typename internal::BestFittingTypeImpl<(Value > std::numeric_limits< uint8_t >::max()),(Value > std::numeric_limits< uint16_t >::max()),(Value > std::numeric_limits< uint32_t >::max())>::Type_t Type_t
ignore the warnings because we need the comparisons to find the best fitting type
Definition: helplets.hpp:244
Definition: helplets.hpp:103
struct to find the best fitting unsigned integer type
Definition: helplets.hpp:49
Definition: helplets.hpp:84
Definition: helplets.hpp:122