iceoryx_doc  1.0.1
helplets.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_CXX_HELPLETS_HPP
18 #define IOX_UTILS_CXX_HELPLETS_HPP
19 
20 #include "iceoryx_utils/cxx/generic_raii.hpp"
21 #include "iceoryx_utils/platform/platform_correction.hpp"
22 
23 #include <assert.h>
24 #include <cstdint>
25 #include <iostream>
26 #include <limits>
27 #include <type_traits>
28 
29 namespace iox
30 {
31 namespace cxx
32 {
33 namespace internal
34 {
35 inline void
36 Require(const bool condition, const char* file, const int line, const char* function, const char* conditionString)
37 {
38  if (!condition)
39  {
40  std::cerr << "Condition: " << conditionString << " in " << function << " is violated. (" << file << ":" << line
41  << ")" << std::endl;
42  std::terminate();
43  }
44 }
45 
47 template <bool GreaterUint8, bool GreaterUint16, bool GreaterUint32>
49 {
50  using Type_t = uint64_t;
51 };
52 
53 template <>
54 struct BestFittingTypeImpl<false, false, false>
55 {
56  using Type_t = uint8_t;
57 };
58 
59 template <>
60 struct BestFittingTypeImpl<true, false, false>
61 {
62  using Type_t = uint16_t;
63 };
64 
65 template <>
66 struct BestFittingTypeImpl<true, true, false>
67 {
68  using Type_t = uint32_t;
69 };
70 } // namespace internal
71 
72 // implementing C++ Core Guideline, I.6. Prefer Expects
73 // see:
74 // https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-expects
75 #define Expects(condition) internal::Require(condition, __FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
76 
77 // implementing C++ Core Guideline, I.8. Prefer Ensures
78 // see:
79 // https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Ri-ensures
80 #define Ensures(condition) internal::Require(condition, __FILE__, __LINE__, __PRETTY_FUNCTION__, #condition)
81 
82 template <typename T, typename = typename std::enable_if<std::is_pointer<T>::value, void>::type>
83 struct not_null
84 {
85  public:
86  not_null(T t)
87  : value(t)
88  {
89  Expects(t != nullptr);
90  }
91 
92  constexpr operator T() const
93  {
94  return value;
95  }
96 
97  private:
98  T value;
99 };
100 
101 template <typename T, T Minimum>
103 {
104  public:
105  greater_or_equal(T t)
106  : value(t)
107  {
108  Expects(t >= Minimum);
109  }
110 
111  constexpr operator T() const
112  {
113  return value;
114  }
115 
116  private:
117  T value;
118 };
119 
120 template <typename T, T Minimum, T Maximum>
121 struct range
122 {
123  public:
124  range(T t)
125  : value(t)
126  {
127  Expects(t >= Minimum && t <= Maximum);
128  }
129 
130  constexpr operator T() const
131  {
132  return value;
133  }
134 
135  private:
136  T value;
137 };
138 
139 template <typename T>
140 T align(const T value, const T alignment)
141 {
142  T remainder = value % alignment;
143  return value + ((remainder == 0u) ? 0u : alignment - remainder);
144 }
145 
150 void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept;
151 
154 void alignedFree(void* const memory);
155 
157 template <size_t s = 0>
158 constexpr size_t maxAlignment()
159 {
160  return s;
161 }
162 
164 template <typename T, typename... Args>
165 constexpr size_t maxAlignment()
166 {
167  return alignof(T) > maxAlignment<Args...>() ? alignof(T) : maxAlignment<Args...>();
168 }
169 
171 template <size_t s = 0>
172 constexpr size_t maxSize()
173 {
174  return s;
175 }
176 
178 template <typename T, typename... Args>
179 constexpr size_t maxSize()
180 {
181  return sizeof(T) > maxSize<Args...>() ? sizeof(T) : maxSize<Args...>();
182 }
183 
191 template <typename T, typename... CTorArgs>
192 GenericRAII makeScopedStatic(T& memory, CTorArgs&&... ctorArgs)
193 {
194  memory.emplace(std::forward<CTorArgs>(ctorArgs)...);
195  return GenericRAII([] {}, [&memory] { memory.reset(); });
196 }
198 template <typename T, typename Enumeration>
199 const char* convertEnumToString(T port, const Enumeration source)
200 {
201  return port[static_cast<size_t>(source)];
202 }
203 
205 template <typename enum_type>
206 auto enumTypeAsUnderlyingType(enum_type const value) -> typename std::underlying_type<enum_type>::type
207 {
208  return static_cast<typename std::underlying_type<enum_type>::type>(value);
209 }
210 
216 template <typename Container, typename Functor>
217 void forEach(Container& c, const Functor& f) noexcept
218 {
219  for (auto& element : c)
220  {
221  f(element);
222  }
223 }
224 
229 template <uint64_t SizeValue>
230 static constexpr uint64_t strlen2(char const (&/*notInterested*/)[SizeValue])
231 {
232  return SizeValue - 1;
233 }
234 
236 template <uint64_t Value>
238 {
240 #pragma GCC diagnostic push
241 #pragma GCC diagnostic ignored "-Wtype-limits"
242  using Type_t = typename internal::BestFittingTypeImpl<(Value > std::numeric_limits<uint8_t>::max()),
243  (Value > std::numeric_limits<uint16_t>::max()),
244  (Value > std::numeric_limits<uint32_t>::max())>::Type_t;
245 #pragma GCC diagnostic pop
246 };
247 
248 template <uint64_t Value>
249 using BestFittingType_t = typename BestFittingType<Value>::Type_t;
250 
253 constexpr bool isCompiledOn32BitSystem()
254 {
255  return INTPTR_MAX == INT32_MAX;
256 }
257 
260 template <typename T>
261 constexpr bool isPowerOfTwo(const T n)
262 {
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);
265 }
266 
267 } // namespace cxx
268 } // namespace iox
269 
270 #endif // IOX_UTILS_CXX_HELPLETS_HPP
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