Skip to content

iceoryx_utils/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
struct iox::cxx::is_function_ref
struct iox::cxx::is_function_ref< function_ref< Targs... > >
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_UTILS_CXX_FUNCTION_REF_HPP
#define IOX_UTILS_CXX_FUNCTION_REF_HPP

#include "iceoryx_utils/cxx/type_traits.hpp"

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

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

template <typename...>
struct is_function_ref : std::false_type
{
};

template <typename... Targs>
struct is_function_ref<function_ref<Targs...>> : std::true_type
{
};

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

  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_ref<std::remove_reference_t<CallableType>>::value>,
              typename = std::enable_if_t<is_invocable<CallableType, ArgTypes...>::value>>
    function_ref(CallableType&& callable) 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_utils/internal/cxx/function_ref.inl"

#endif

Updated on 17 June 2021 at 11:15:26 CEST