iceoryx_hoofs  2.0.2
helplets.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 - 2022 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_HOOFS_CXX_HELPLETS_HPP
18 #define IOX_HOOFS_CXX_HELPLETS_HPP
19 
20 #include "iceoryx_hoofs/cxx/string.hpp"
21 #include "iceoryx_hoofs/cxx/type_traits.hpp"
22 
23 #include <cassert>
24 #include <cstdint>
25 #include <cstring>
26 #include <iostream>
27 #include <limits>
28 #include <type_traits>
29 
30 #include "iceoryx_hoofs/platform/platform_correction.hpp"
31 #include "iceoryx_hoofs/platform/platform_settings.hpp"
32 
33 namespace iox
34 {
35 namespace cxx
36 {
37 template <uint64_t Capacity>
38 class string;
39 struct TruncateToCapacity_t;
40 
41 namespace internal
42 {
44 template <bool GreaterUint8, bool GreaterUint16, bool GreaterUint32>
45 struct BestFittingTypeImpl
46 {
47  using Type_t = uint64_t;
48 };
49 
50 template <>
51 struct BestFittingTypeImpl<false, false, false>
52 {
53  using Type_t = uint8_t;
54 };
55 
56 template <>
57 struct BestFittingTypeImpl<true, false, false>
58 {
59  using Type_t = uint16_t;
60 };
61 
62 template <>
63 struct BestFittingTypeImpl<true, true, false>
64 {
65  using Type_t = uint32_t;
66 };
67 
68 constexpr char ASCII_A = 'a';
69 constexpr char ASCII_Z = 'z';
70 constexpr char ASCII_CAPITAL_A = 'A';
71 constexpr char ASCII_CAPITAL_Z = 'Z';
72 constexpr char ASCII_0 = '0';
73 constexpr char ASCII_9 = '9';
74 constexpr char ASCII_MINUS = '-';
75 constexpr char ASCII_DOT = '.';
76 constexpr char ASCII_COLON = ':';
77 constexpr char ASCII_UNDERSCORE = '_';
78 } // namespace internal
79 
80 template <typename T, typename = typename std::enable_if<std::is_pointer<T>::value, void>::type>
81 struct not_null
82 {
83  public:
84  not_null(T t) noexcept
85  : value(t)
86  {
87  Expects(t != nullptr);
88  }
89 
90  constexpr operator T() const noexcept
91  {
92  return value;
93  }
94 
95  private:
96  T value;
97 };
98 
99 template <typename T, T Minimum>
101 {
102  public:
103  greater_or_equal(T t) noexcept
104  : value(t)
105  {
106  Expects(t >= Minimum);
107  }
108 
109  constexpr operator T() const noexcept
110  {
111  return value;
112  }
113 
114  private:
115  T value;
116 };
117 
118 template <typename T, T Minimum, T Maximum>
119 struct range
120 {
121  public:
122  range(T t) noexcept
123  : value(t)
124  {
125  Expects(t >= Minimum && t <= Maximum);
126  }
127 
128  constexpr operator T() const noexcept
129  {
130  return value;
131  }
132 
133  private:
134  T value;
135 };
136 
137 template <typename T>
138 T align(const T value, const T alignment) noexcept
139 {
140  T remainder = value % alignment;
141  return value + ((remainder == 0u) ? 0u : alignment - remainder);
142 }
143 
148 void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept;
149 
152 void alignedFree(void* const memory) noexcept;
153 
155 template <size_t s = 0>
156 constexpr size_t maxAlignment() noexcept
157 {
158  return s;
159 }
160 
162 template <typename T, typename... Args>
163 constexpr size_t maxAlignment() noexcept
164 {
165  return alignof(T) > maxAlignment<Args...>() ? alignof(T) : maxAlignment<Args...>();
166 }
167 
169 template <size_t s = 0>
170 constexpr size_t maxSize() noexcept
171 {
172  return s;
173 }
174 
176 template <typename T, typename... Args>
177 constexpr size_t maxSize() noexcept
178 {
179  return sizeof(T) > maxSize<Args...>() ? sizeof(T) : maxSize<Args...>();
180 }
181 
183 template <typename T, typename Enumeration>
184 const char* convertEnumToString(T port, const Enumeration source) noexcept
185 {
186  return port[static_cast<size_t>(source)];
187 }
188 
190 template <typename enum_type>
191 auto enumTypeAsUnderlyingType(enum_type const value) noexcept -> typename std::underlying_type<enum_type>::type
192 {
193  return static_cast<typename std::underlying_type<enum_type>::type>(value);
194 }
195 
201 template <typename Container, typename Functor>
202 void forEach(Container& c, const Functor& f) noexcept
203 {
204  for (auto& element : c)
205  {
206  f(element);
207  }
208 }
209 
214 template <uint64_t SizeValue>
215 static constexpr uint64_t strlen2(char const (&/*notInterested*/)[SizeValue]) noexcept
216 {
217  return SizeValue - 1;
218 }
219 
221 template <uint64_t Value>
223 {
225 #pragma GCC diagnostic push
226 #pragma GCC diagnostic ignored "-Wtype-limits"
227  using Type_t = typename internal::BestFittingTypeImpl<(Value > std::numeric_limits<uint8_t>::max()),
228  (Value > std::numeric_limits<uint16_t>::max()),
229  (Value > std::numeric_limits<uint32_t>::max())>::Type_t;
230 #pragma GCC diagnostic pop
231 };
232 
233 template <uint64_t Value>
234 using BestFittingType_t = typename BestFittingType<Value>::Type_t;
235 
238 constexpr bool isCompiledOn32BitSystem() noexcept
239 {
240  return INTPTR_MAX == INT32_MAX;
241 }
242 
245 template <typename T>
246 constexpr bool isPowerOfTwo(const T n) noexcept
247 {
248  static_assert(std::is_unsigned<T>::value && !std::is_same<T, bool>::value, "Only unsigned integer are allowed!");
249  return n && ((n & (n - 1U)) == 0U);
250 }
251 
254 template <uint64_t StringCapacity>
255 bool isValidFileName(const string<StringCapacity>& name) noexcept;
256 
259 template <uint64_t StringCapacity>
260 bool isValidFilePath(const string<StringCapacity>& name) noexcept;
261 
302 template <typename F, typename T>
303 constexpr T from(const F value) noexcept;
304 
315 template <typename T, typename F>
316 constexpr T into(const F value) noexcept;
317 
344 #define IOX_BUILDER_PARAMETER(type, name, defaultValue) \
345  public: \
346  decltype(auto) name(type const& value)&& \
347  { \
348  m_##name = value; \
349  return std::move(*this); \
350  } \
351  \
352  decltype(auto) name(type&& value)&& \
353  { \
354  m_##name = std::move(value); \
355  return std::move(*this); \
356  } \
357  \
358  private: \
359  type m_##name{defaultValue};
360 
361 } // namespace cxx
362 } // namespace iox
363 
364 #include "iceoryx_hoofs/internal/cxx/helplets.inl"
365 
366 #endif // IOX_HOOFS_CXX_HELPLETS_HPP
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
get the best fitting unsigned integer type for a given value at compile time
Definition: helplets.hpp:223
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:229
Definition: helplets.hpp:101
Definition: helplets.hpp:82
Definition: helplets.hpp:120