Skip to content

iceoryx_hoofs/cxx/method_callback.hpp🔗

Namespaces🔗

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

Classes🔗

Name
class iox::cxx::ConstMethodCallback
class iox::cxx::MethodCallback

Source code🔗

// Copyright (c) 2020 - 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_METHOD_CALLBACK_HPP
#define IOX_HOOFS_CXX_METHOD_CALLBACK_HPP

#include "iceoryx_hoofs/cxx/expected.hpp"
#include "iceoryx_hoofs/cxx/function_ref.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"

namespace iox
{
namespace cxx
{
namespace internal
{
class GenericClass
{
};
} // namespace internal

enum class MethodCallbackError
{
    UNINITIALIZED_CALLBACK
};

template <typename ReturnValue, typename... Args>
class ConstMethodCallback
{
  public:
    template <typename T>
    using ConstMethodPointer = ReturnValue (T::*)(Args...) const;

    ConstMethodCallback() noexcept = default;
    ConstMethodCallback(const ConstMethodCallback& rhs) noexcept = default;
    ConstMethodCallback& operator=(const ConstMethodCallback& rhs) noexcept = default;
    ~ConstMethodCallback() noexcept = default;

    template <typename ClassType>
    ConstMethodCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> const methodPtr) noexcept;

    ConstMethodCallback(ConstMethodCallback&& rhs) noexcept;

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

    template <typename... MethodArguments>
    expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) const noexcept;

    bool operator==(const ConstMethodCallback& rhs) const noexcept;

    bool operator!=(const ConstMethodCallback& rhs) const noexcept;

    explicit operator bool() const noexcept;

    bool isValid() const noexcept;

    template <typename ClassType>
    void setCallback(const ClassType& objectRef, ConstMethodPointer<ClassType> methodPtr) noexcept;

    template <typename ClassType>
    const ClassType* getObjectPointer() const noexcept;

    template <typename ClassType>
    auto getMethodPointer() const noexcept -> ConstMethodPointer<ClassType>;

  private:
    const void* m_objectPtr{nullptr};
    ConstMethodPointer<internal::GenericClass> m_methodPtr{nullptr};
    cxx::function_ref<ReturnValue(const void*, ConstMethodPointer<internal::GenericClass>, Args...)> m_callback;
};

template <typename ReturnValue, typename... Args>
class MethodCallback
{
  public:
    template <typename T>
    using MethodPointer = ReturnValue (T::*)(Args...);

    MethodCallback() noexcept = default;
    MethodCallback(const MethodCallback& rhs) noexcept = default;
    MethodCallback& operator=(const MethodCallback& rhs) noexcept = default;
    ~MethodCallback() noexcept = default;

    template <typename ClassType>
    MethodCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;

    MethodCallback(MethodCallback&& rhs) noexcept;

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

    template <typename... MethodArguments>
    expected<ReturnValue, MethodCallbackError> operator()(MethodArguments&&... args) noexcept;

    bool operator==(const MethodCallback& rhs) const noexcept;

    bool operator!=(const MethodCallback& rhs) const noexcept;

    explicit operator bool() const noexcept;

    bool isValid() const noexcept;

    template <typename ClassType>
    void setCallback(ClassType& objectRef, MethodPointer<ClassType> methodPtr) noexcept;

    template <typename ClassType>
    ClassType* getObjectPointer() const noexcept;

    template <typename ClassType>
    auto getMethodPointer() const noexcept -> MethodPointer<ClassType>;

  private:
    void* m_objectPtr{nullptr};
    MethodPointer<internal::GenericClass> m_methodPtr{nullptr};
    cxx::function_ref<ReturnValue(void*, MethodPointer<internal::GenericClass>, Args...)> m_callback;
};

} // namespace cxx
} // namespace iox

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

#endif

Updated on 31 May 2022 at 11:34:55 CEST