iceoryx_hoofs 2.0.3
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
33namespace iox
34{
35namespace cxx
36{
37template <uint64_t Capacity>
38class string;
39struct TruncateToCapacity_t;
40
41namespace internal
42{
44template <bool GreaterUint8, bool GreaterUint16, bool GreaterUint32>
45struct BestFittingTypeImpl
46{
47 using Type_t = uint64_t;
48};
49
50template <>
51struct BestFittingTypeImpl<false, false, false>
52{
53 using Type_t = uint8_t;
54};
55
56template <>
57struct BestFittingTypeImpl<true, false, false>
58{
59 using Type_t = uint16_t;
60};
61
62template <>
63struct BestFittingTypeImpl<true, true, false>
64{
65 using Type_t = uint32_t;
66};
67
68constexpr char ASCII_A = 'a';
69constexpr char ASCII_Z = 'z';
70constexpr char ASCII_CAPITAL_A = 'A';
71constexpr char ASCII_CAPITAL_Z = 'Z';
72constexpr char ASCII_0 = '0';
73constexpr char ASCII_9 = '9';
74constexpr char ASCII_MINUS = '-';
75constexpr char ASCII_DOT = '.';
76constexpr char ASCII_COLON = ':';
77constexpr char ASCII_UNDERSCORE = '_';
78} // namespace internal
79
80template <typename T, typename = typename std::enable_if<std::is_pointer<T>::value, void>::type>
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
99template <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
118template <typename T, T Minimum, T Maximum>
119struct 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
137template <typename T>
138T align(const T value, const T alignment) noexcept
139{
140 T remainder = value % alignment;
141 return value + ((remainder == 0u) ? 0u : alignment - remainder);
142}
143
148void* alignedAlloc(const uint64_t alignment, const uint64_t size) noexcept;
149
152void alignedFree(void* const memory) noexcept;
153
155template <size_t s = 0>
156constexpr size_t maxAlignment() noexcept
157{
158 return s;
159}
160
162template <typename T, typename... Args>
163constexpr size_t maxAlignment() noexcept
164{
165 return alignof(T) > maxAlignment<Args...>() ? alignof(T) : maxAlignment<Args...>();
166}
167
169template <size_t s = 0>
170constexpr size_t maxSize() noexcept
171{
172 return s;
173}
174
176template <typename T, typename... Args>
177constexpr size_t maxSize() noexcept
178{
179 return sizeof(T) > maxSize<Args...>() ? sizeof(T) : maxSize<Args...>();
180}
181
183template <typename T, typename Enumeration>
184const char* convertEnumToString(T port, const Enumeration source) noexcept
185{
186 return port[static_cast<size_t>(source)];
187}
188
190template <typename enum_type>
191auto 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
201template <typename Container, typename Functor>
202void forEach(Container& c, const Functor& f) noexcept
203{
204 for (auto& element : c)
205 {
206 f(element);
207 }
208}
209
214template <uint64_t SizeValue>
215static constexpr uint64_t strlen2(char const (&/*notInterested*/)[SizeValue]) noexcept
216{
217 return SizeValue - 1;
218}
219
221template <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
233template <uint64_t Value>
234using BestFittingType_t = typename BestFittingType<Value>::Type_t;
235
238constexpr bool isCompiledOn32BitSystem() noexcept
239{
240 return INTPTR_MAX == INT32_MAX;
241}
242
245template <typename T>
246constexpr 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
254template <uint64_t StringCapacity>
255bool isValidFileName(const string<StringCapacity>& name) noexcept;
256
259template <uint64_t StringCapacity>
260bool isValidFilePath(const string<StringCapacity>& name) noexcept;
261
302template <typename F, typename T>
303constexpr T from(const F value) noexcept;
304
315template <typename T, typename F>
316constexpr 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