Skip to content

iceoryx_utils/cxx/smart_c.hpp🔗

Namespaces🔗

Name
iox
building block to easily create free function for logging in a library context
iox::cxx

Classes🔗

Name
class iox::cxx::SmartC
C function call abstraction class which performs the error handling automatically.

Defines🔗

Name
makeSmartC(f_function, f_returnMode, f_returnValues, f_ignoredValues, ...)
Always use this macro to create smart_c objects.

Macro Documentation🔗

define makeSmartC🔗

#define makeSmartC(
    f_function,
    f_returnMode,
    f_returnValues,
    f_ignoredValues,
    ...
)     makeSmartCImpl(__FILE__,                                                                                           \
                   __LINE__,                                                                                           \
                   __PRETTY_FUNCTION__,                                                                                \
                   f_function,                                                                                         \
                   f_returnMode,                                                                                       \
                   f_returnValues,                                                                                     \
                   f_ignoredValues,                                                                                    \
                   __VA_ARGS__)

Always use this macro to create smart_c objects.

Parameters:

  • f_function Function you wish to call
  • f_returnValues Possible return values that this function can return
  • f_ignoredValues Possible return values that this function can return, but is not blaming them in the log.
  • ... Arguments which will be passed to the function

Use the macro instead of this direct function call. The macro fills automatically the source of the function call via the FILE, LINE and PRETTY_FUNCTION defines. Creates a smart_c c function call and executes the call. Depending on how successful the c function call was it either returns via std::cerr an error message containing the source of this makeSmartC or it stores the return value. We can retrieve it later by calling getReturnValue() or casting since smart_c has an casting operator to the returnValue.

auto memoryCall =
    cxx::makeSmartC(malloc, cxx::returnMode::PRE_DEFINED_ERROR_CODE, {static_cast<void*>(nullptr)}, 10);

void * pointer;
if ( !memoryCall.hasErrors() ) {
    pointer = memoryCall.getReturnValue();

    // it is also possible to assign it directly since it has an
    // cast operator to the ReturnType
    pointer = memoryCall;
}

Source code🔗

// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_UTILS_CXX_SMART_C_HPP
#define IOX_UTILS_CXX_SMART_C_HPP

#include "iceoryx_utils/cxx/string.hpp"
#include "iceoryx_utils/platform/platform_correction.hpp"

#include <cstring>
#include <initializer_list>
#include <iostream>

#define makeSmartC(f_function, f_returnMode, f_returnValues, f_ignoredValues, ...)                                     \
    makeSmartCImpl(__FILE__,                                                                                           \
                   __LINE__,                                                                                           \
                   __PRETTY_FUNCTION__,                                                                                \
                   f_function,                                                                                         \
                   f_returnMode,                                                                                       \
                   f_returnValues,                                                                                     \
                   f_ignoredValues,                                                                                    \
                   __VA_ARGS__)


namespace iox
{
namespace cxx
{
static constexpr uint32_t ERRORSTRINGSIZE = 128u;

enum class ReturnMode
{
    PRE_DEFINED_SUCCESS_CODE,
    PRE_DEFINED_ERROR_CODE
};
template <typename Function, typename ReturnType, typename... FunctionArguments>
class SmartC
{
  public:
    ReturnType getReturnValue() const noexcept;

    operator ReturnType() const noexcept;

    bool hasErrors() const noexcept;

    const char* getErrorString() const noexcept;

    int32_t getErrNum() const noexcept;

    template <typename Function_F, typename ReturnType_F, typename... FunctionArguments_F>
    friend SmartC<Function_F, ReturnType_F, FunctionArguments_F...>
    makeSmartCImpl(const char* file,
                   const int line,
                   const char* func,
                   const Function_F& f_function,
                   const ReturnMode& f_mode,
                   const std::initializer_list<ReturnType_F>& f_returnValues,
                   const std::initializer_list<int>& f_ignoredValues,
                   FunctionArguments_F... f_args) noexcept;

  private:
    SmartC(const char* file,
           const int line,
           const char* func,
           const Function& f_function,
           const ReturnMode& f_mode,
           const std::initializer_list<ReturnType>& f_returnValues,
           const std::initializer_list<int>& f_ignoredValues,
           FunctionArguments... f_args) noexcept;

    int resetErrnoAndInitErrnum() noexcept;

  private:
    int32_t m_errnum{0};
    ReturnType m_returnValue;
    string<ERRORSTRINGSIZE> m_errorString;
    bool m_hasErrors = false;

    struct
    {
        const char* file;
        int line;
        const char* func;
    } m_errorSource;
};
} // namespace cxx
} // namespace iox

#include "iceoryx_utils/internal/cxx/smart_c.inl"

#endif // IOX_UTILS_CXX_SMART_C_HPP

Updated on 26 April 2021 at 15:31:01 CEST