iceoryx_doc  1.0.1
smart_c.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. 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_UTILS_CXX_SMART_C_HPP
17 #define IOX_UTILS_CXX_SMART_C_HPP
18 
19 #include "iceoryx_utils/cxx/string.hpp"
20 #include "iceoryx_utils/platform/platform_correction.hpp"
21 
22 #include <cstring>
23 #include <initializer_list>
24 #include <iostream>
25 
54 #define makeSmartC(f_function, f_returnMode, f_returnValues, f_ignoredValues, ...) \
55  makeSmartCImpl(__FILE__, \
56  __LINE__, \
57  __PRETTY_FUNCTION__, \
58  f_function, \
59  f_returnMode, \
60  f_returnValues, \
61  f_ignoredValues, \
62  __VA_ARGS__)
63 
65 
66 namespace iox
67 {
68 namespace cxx
69 {
71 static constexpr uint32_t ERRORSTRINGSIZE = 128u;
72 
77 enum class ReturnMode
78 {
80  PRE_DEFINED_SUCCESS_CODE,
82  PRE_DEFINED_ERROR_CODE
83 };
109 template <typename Function, typename ReturnType, typename... FunctionArguments>
110 class SmartC
111 {
112  public:
118  ReturnType getReturnValue() const noexcept;
119 
122  operator ReturnType() const noexcept;
123 
127  bool hasErrors() const noexcept;
128 
133  const char* getErrorString() const noexcept;
134 
137  int32_t getErrNum() const noexcept;
138 
139  template <typename Function_F, typename ReturnType_F, typename... FunctionArguments_F>
140  friend SmartC<Function_F, ReturnType_F, FunctionArguments_F...>
141  makeSmartCImpl(const char* file,
142  const int line,
143  const char* func,
144  const Function_F& f_function,
145  const ReturnMode& f_mode,
146  const std::initializer_list<ReturnType_F>& f_returnValues,
147  const std::initializer_list<int>& f_ignoredValues,
148  FunctionArguments_F... f_args) noexcept;
149 
150  private:
153  SmartC(const char* file,
154  const int line,
155  const char* func,
156  const Function& f_function,
157  const ReturnMode& f_mode,
158  const std::initializer_list<ReturnType>& f_returnValues,
159  const std::initializer_list<int>& f_ignoredValues,
160  FunctionArguments... f_args) noexcept;
161 
162  int resetErrnoAndInitErrnum() noexcept;
163 
164  private:
165  int32_t m_errnum{0};
166  ReturnType m_returnValue;
167  string<ERRORSTRINGSIZE> m_errorString;
168  bool m_hasErrors = false;
169 
170  struct
171  {
172  const char* file;
173  int line;
174  const char* func;
175  } m_errorSource;
176 };
177 } // namespace cxx
178 } // namespace iox
179 
180 #include "iceoryx_utils/internal/cxx/smart_c.inl"
181 
182 #endif // IOX_UTILS_CXX_SMART_C_HPP
C function call abstraction class which performs the error handling automatically.
Definition: smart_c.hpp:111
ReturnType getReturnValue() const noexcept
Returns the returnValue of the c function call. If an error has occurred the error code is returned....
Definition: smart_c.inl:170
int32_t getErrNum() const noexcept
Returns the errnum. 0 if no error has occurred, otherwise != 0.
Definition: smart_c.inl:194
bool hasErrors() const noexcept
If one of the given error codes was returned during the c function call and the c function failed.
Definition: smart_c.inl:182
const char * getErrorString() const noexcept
If no error occurred it returns a string like "no errors" (depending on the posix system) otherwise i...
Definition: smart_c.inl:188
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28