iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
posix_call.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_POSIX_DESIGN_POSIX_CALL_HPP
15#define IOX2_BB_POSIX_DESIGN_POSIX_CALL_HPP
16
18
21
22#include <cstdint>
23
24namespace iox2 {
25namespace legacy {
26static constexpr uint32_t POSIX_CALL_ERROR_STRING_SIZE = 128U;
28static constexpr int32_t POSIX_CALL_INVALID_ERRNO = -1;
29
30template <typename ReturnType, typename... FunctionArguments>
32
34template <typename T>
44
45namespace detail {
46template <typename ReturnType, typename... FunctionArguments>
47PosixCallBuilder<ReturnType, FunctionArguments...>
49 const char* posixFunctionName,
50 const char* file,
51 const int32_t line,
52 const char* callingFunction) noexcept;
53
54template <typename ReturnType>
56 PosixCallDetails(const char* posixFunctionName, const char* file, int line, const char* callingFunction) noexcept;
57 const char* posixFunctionName = nullptr;
58 const char* file = nullptr;
59 const char* callingFunction = nullptr;
61 bool hasSuccess = true;
62 bool hasIgnoredErrno = false;
63 bool hasSilentErrno = false;
64
66};
67} // namespace detail
68
70template <typename ReturnType>
72 public:
77 template <typename... IgnoredErrnos>
78 PosixCallEvaluator<ReturnType> ignoreErrnos(const IgnoredErrnos... ignoredErrnos) const&& noexcept;
79
84 template <typename... SilentErrnos>
85 PosixCallEvaluator<ReturnType> suppressErrorMessagesForErrnos(const SilentErrnos... silentErrnos) const&& noexcept;
86
91
92 private:
93 template <typename>
95
96 explicit PosixCallEvaluator(detail::PosixCallDetails<ReturnType>& details) noexcept;
97
98 private:
99 // NOLINTJUSTIFICATION refences are intentionally used since the class does not need to be assignable
100 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
101 detail::PosixCallDetails<ReturnType>& m_details;
102};
103
105template <typename ReturnType>
107 public:
111 template <typename... SuccessReturnValues>
112 PosixCallEvaluator<ReturnType> successReturnValue(const SuccessReturnValues... successReturnValues) && noexcept;
113
117 template <typename... FailureReturnValues>
118 PosixCallEvaluator<ReturnType> failureReturnValue(const FailureReturnValues... failureReturnValues) && noexcept;
119
123
124 private:
125 template <typename, typename...>
126 friend class PosixCallBuilder;
127
129
130 private:
131 // NOLINTJUSTIFICATION refences are intentionally used since the class does not need to be assignable
132 // NOLINTNEXTLINE(cppcoreguidelines-avoid-const-or-ref-data-members)
134};
135
136template <typename ReturnType, typename... FunctionArguments>
138 public:
140 using FunctionType_t = ReturnType (*)(FunctionArguments...);
141
146 PosixCallVerificator<ReturnType> operator()(FunctionArguments... arguments) && noexcept;
147
148 private:
149 template <typename ReturnTypeFriend, typename... FunctionArgumentsFriend>
150 friend PosixCallBuilder<ReturnTypeFriend, FunctionArgumentsFriend...>
151 detail::createPosixCallBuilder(ReturnTypeFriend (*IOX2_POSIX_CALL)(FunctionArgumentsFriend...),
152 const char* posixFunctionName,
153 const char* file,
154 const int32_t line,
155 const char* callingFunction) noexcept;
156
158 const char* posixFunctionName,
159 const char* file,
160 const int32_t line,
161 const char* callingFunction) noexcept;
162
163 private:
164 FunctionType_t m_IOX2_POSIX_CALL = nullptr;
166};
167} // namespace legacy
168} // namespace iox2
169
170#include "iox2/legacy/detail/posix_call.inl"
171
195// NOLINTJUSTIFICATION a template or constexpr function does not have access to source code origin file, line, function
196// name
197// NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
198#define IOX2_POSIX_CALL(f) \
199 iox2::legacy::detail::createPosixCallBuilder( \
200 &(f), \
201 (#f), \
202 __FILE__, \
203 __LINE__, \
204 __PRETTY_FUNCTION__) // NOLINT(cppcoreguidelines-pro-bounds-array-to-pointer-decay,hicpp-no-array-decay)
205// needed for source code location, safely wrapped in macro
206
207#endif // IOX2_BB_POSIX_DESIGN_POSIX_CALL_HPP
#define IOX2_NO_DISCARD
IOX2_NO_DISCARD adds the [[nodiscard]] keyword if it is available for the current compiler.
ReturnType(*)(FunctionArguments...) FunctionType_t
input function type
PosixCallVerificator< ReturnType > operator()(FunctionArguments... arguments) &&noexcept
Call the underlying function with the provided arguments. If the underlying function fails and sets t...
class which is created by the verificator to evaluate the result of a posix call
PosixCallEvaluator< ReturnType > ignoreErrnos(const IgnoredErrnos... ignoredErrnos) const &&noexcept
ignore specified errnos from the evaluation
expected< PosixCallResult< ReturnType >, PosixCallResult< ReturnType > > evaluate() const &&noexcept
evaluate the result of a posix call
PosixCallEvaluator< ReturnType > suppressErrorMessagesForErrnos(const SilentErrnos... silentErrnos) const &&noexcept
silence specified errnos from printing error messages in the evaluation
class which verifies the return value of a posix function call
PosixCallEvaluator< ReturnType > failureReturnValue(const FailureReturnValues... failureReturnValues) &&noexcept
the posix function call defines failure through a single value
PosixCallEvaluator< ReturnType > returnValueMatchesErrno() &&noexcept
the posix function call defines failure through return of the errno value instead of setting the errn...
PosixCallEvaluator< ReturnType > successReturnValue(const SuccessReturnValues... successReturnValues) &&noexcept
the posix function call defines success through a single value
Implementation of the C++23 expected class which can contain an error or a success value.
Definition expected.hpp:150
PosixCallBuilder< ReturnType, FunctionArguments... > createPosixCallBuilder(ReturnType(*IOX2_POSIX_CALL)(FunctionArguments...), const char *posixFunctionName, const char *file, const int32_t line, const char *callingFunction) noexcept
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
static constexpr int32_t POSIX_CALL_INVALID_ERRNO
static constexpr uint32_t POSIX_CALL_ERROR_STRING_SIZE
static constexpr uint64_t POSIX_CALL_EINTR_REPETITIONS
#define IOX2_POSIX_CALL(f)
Calling a posix function with automated error handling. If the posix function returns void you do not...
result of a posix call
PosixCallResult() noexcept=default
int32_t errnum
the errno value which was set by the posix function call
T value
the return value of the posix function call
PosixCallDetails(const char *posixFunctionName, const char *file, int line, const char *callingFunction) noexcept
PosixCallResult< ReturnType > result