iceoryx_hoofs 2.0.3
posix_call.hpp
1// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16#ifndef IOX_HOOFS_POSIX_WRAPPER_POSIX_CALL_HPP
17#define IOX_HOOFS_POSIX_WRAPPER_POSIX_CALL_HPP
18
19#include "iceoryx_hoofs/cxx/algorithm.hpp"
20#include "iceoryx_hoofs/cxx/attributes.hpp"
21#include "iceoryx_hoofs/cxx/expected.hpp"
22#include "iceoryx_hoofs/cxx/string.hpp"
23
24#include <cstdint>
25#include <cstring>
26
27namespace iox
28{
29namespace posix
30{
31static constexpr uint32_t POSIX_CALL_ERROR_STRING_SIZE = 128U;
32static constexpr uint64_t POSIX_CALL_EINTR_REPETITIONS = 5U;
33static constexpr int32_t POSIX_CALL_INVALID_ERRNO = -1;
34
35template <typename ReturnType, typename... FunctionArguments>
36class PosixCallBuilder;
37
39template <typename T>
41{
42 PosixCallResult() noexcept = default;
43
47
49 T value{};
50
52 int32_t errnum = POSIX_CALL_INVALID_ERRNO;
53};
54
55namespace internal
56{
57template <typename ReturnType, typename... FunctionArguments>
58PosixCallBuilder<ReturnType, FunctionArguments...> createPosixCallBuilder(ReturnType (*posixCall)(FunctionArguments...),
59 const char* posixFunctionName,
60 const char* file,
61 const int32_t line,
62 const char* callingFunction) noexcept;
63
64template <typename ReturnType>
65struct PosixCallDetails
66{
67 PosixCallDetails(const char* posixFunctionName, const char* file, int line, const char* callingFunction) noexcept;
68 const char* posixFunctionName = nullptr;
69 const char* file = nullptr;
70 const char* callingFunction = nullptr;
71 int32_t line = 0;
72 bool hasSuccess = true;
73 bool hasIgnoredErrno = false;
74 bool hasSilentErrno = false;
75
77};
78} // namespace internal
79
105#define posixCall(f) internal::createPosixCallBuilder(f, #f, __FILE__, __LINE__, __PRETTY_FUNCTION__)
106
108template <typename ReturnType>
109class IOX_NO_DISCARD PosixCallEvaluator
110{
111 public:
116 template <typename... IgnoredErrnos>
117 PosixCallEvaluator<ReturnType> ignoreErrnos(const IgnoredErrnos... ignoredErrnos) const&& noexcept;
118
123 template <typename... SilentErrnos>
124 PosixCallEvaluator<ReturnType> suppressErrorMessagesForErrnos(const SilentErrnos... silentErrnos) const&& noexcept;
125
129 cxx::expected<PosixCallResult<ReturnType>, PosixCallResult<ReturnType>> evaluate() const&& noexcept;
130
131 private:
132 template <typename>
133 friend class PosixCallVerificator;
134
135 explicit PosixCallEvaluator(internal::PosixCallDetails<ReturnType>& details) noexcept;
136
137 private:
138 internal::PosixCallDetails<ReturnType>& m_details;
139};
140
142template <typename ReturnType>
143class IOX_NO_DISCARD PosixCallVerificator
144{
145 public:
149 template <typename... SuccessReturnValues>
150 PosixCallEvaluator<ReturnType> successReturnValue(const SuccessReturnValues... successReturnValues) && noexcept;
151
155 template <typename... FailureReturnValues>
156 PosixCallEvaluator<ReturnType> failureReturnValue(const FailureReturnValues... failureReturnValues) && noexcept;
157
161
162 private:
163 template <typename, typename...>
164 friend class PosixCallBuilder;
165
166 explicit PosixCallVerificator(internal::PosixCallDetails<ReturnType>& details) noexcept;
167
168 private:
169 internal::PosixCallDetails<ReturnType>& m_details;
170};
171
172template <typename ReturnType, typename... FunctionArguments>
173class IOX_NO_DISCARD PosixCallBuilder
174{
175 public:
177 using FunctionType_t = ReturnType (*)(FunctionArguments...);
178
183 PosixCallVerificator<ReturnType> operator()(FunctionArguments... arguments) && noexcept;
184
185 private:
186 template <typename ReturnTypeFriend, typename... FunctionArgumentsFriend>
187 friend PosixCallBuilder<ReturnTypeFriend, FunctionArgumentsFriend...>
188 internal::createPosixCallBuilder(ReturnTypeFriend (*posixCall)(FunctionArgumentsFriend...),
189 const char* posixFunctionName,
190 const char* file,
191 const int32_t line,
192 const char* callingFunction) noexcept;
193
195 const char* posixFunctionName,
196 const char* file,
197 const int32_t line,
198 const char* callingFunction) noexcept;
199
200 private:
201 FunctionType_t m_posixCall = nullptr;
202 internal::PosixCallDetails<ReturnType> m_details;
203};
204} // namespace posix
205} // namespace iox
206
207#include "iceoryx_hoofs/internal/posix_wrapper/posix_call.inl"
208
209#endif // IOX_HOOFS_POSIX_WRAPPER_POSIX_CALL_HPP
string implementation with some adjustments in the API, because we are not allowed to throw exception...
Definition: string.hpp:90
Definition: posix_call.hpp:174
PosixCallVerificator< ReturnType > operator()(FunctionArguments... arguments) &&noexcept
Call the underlying function with the provided arguments. If the underlying function fails and sets t...
ReturnType(*)(FunctionArguments...) FunctionType_t
input function type
Definition: posix_call.hpp:177
class which is created by the verificator to evaluate the result of a posix call
Definition: posix_call.hpp:110
PosixCallEvaluator< ReturnType > ignoreErrnos(const IgnoredErrnos... ignoredErrnos) const &&noexcept
ignore specified errnos from the evaluation
PosixCallEvaluator< ReturnType > suppressErrorMessagesForErrnos(const SilentErrnos... silentErrnos) const &&noexcept
silence specified errnos from printing error messages in the evaluation
cxx::expected< PosixCallResult< ReturnType >, PosixCallResult< ReturnType > > evaluate() const &&noexcept
evaluate the result of a posix call
class which verifies the return value of a posix function call
Definition: posix_call.hpp:144
PosixCallEvaluator< ReturnType > returnValueMatchesErrno() &&noexcept
the posix function call defines failure through return of the errno value instead of setting the errn...
PosixCallEvaluator< ReturnType > failureReturnValue(const FailureReturnValues... failureReturnValues) &&noexcept
the posix function call defines failure through a single value
PosixCallEvaluator< ReturnType > successReturnValue(const SuccessReturnValues... successReturnValues) &&noexcept
the posix function call defines success through a single value
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29
result of a posix call
Definition: posix_call.hpp:41
int32_t errnum
the errno value which was set by the posix function call
Definition: posix_call.hpp:52
cxx::string< POSIX_CALL_ERROR_STRING_SIZE > getHumanReadableErrnum() const noexcept
returns the result of std::strerror(errnum) which acquires a human readable error string
T value
the return value of the posix function call
Definition: posix_call.hpp:49