Skip to content

iceoryx_hoofs/cxx/function_ref.hpp🔗

Namespaces🔗

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

Classes🔗

Name
class iox::cxx::function_ref
class iox::cxx::function_ref< ReturnType(ArgTypes...)>
cxx::function_ref is a non-owning reference to a callable.

Source code🔗

// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. 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_HOOFS_CXX_FUNCTION_REF_HPP
#define IOX_HOOFS_CXX_FUNCTION_REF_HPP

#include "iceoryx_hoofs/cxx/requires.hpp"
#include "iceoryx_hoofs/cxx/type_traits.hpp"

#include <cstddef>
#include <iostream>
#include <memory>
#include <type_traits>

namespace iox
{
namespace cxx
{
template <typename SignatureType>
class function_ref;


template <class ReturnType, class... ArgTypes>
class function_ref<ReturnType(ArgTypes...)>
{
    using SignatureType = ReturnType(ArgTypes...);

    template <typename T1, typename T2>
    using has_same_decayed_type = typename std::integral_constant<
        bool,
        bool(std::is_same<typename std::decay<T1>::type, typename std::decay<T2>::type>::value)>;

  public:
    function_ref() noexcept;

    ~function_ref() noexcept = default;

    function_ref(const function_ref&) noexcept = default;

    function_ref& operator=(const function_ref&) noexcept = default;

    template <typename CallableType,
              typename = std::enable_if_t<!is_function_pointer<CallableType>::value
                                          && !has_same_decayed_type<CallableType, function_ref>::value
                                          && is_invocable<CallableType, ArgTypes...>::value>>
    function_ref(CallableType&& callable) noexcept;

    function_ref(ReturnType (*function)(ArgTypes...)) noexcept;

    function_ref(function_ref&& rhs) noexcept;

    function_ref& operator=(function_ref&& rhs) noexcept;

    ReturnType operator()(ArgTypes... args) const noexcept;

    explicit operator bool() const noexcept;

    void swap(function_ref& rhs) noexcept;

  private:
    void* m_pointerToCallable{nullptr};
    ReturnType (*m_functionPointer)(void*, ArgTypes...){nullptr};
};

} // namespace cxx
} // namespace iox

#include "iceoryx_hoofs/internal/cxx/function_ref.inl"

#endif

Updated on 18 December 2023 at 13:02:34 CET