Skip to content

iceoryx_utils/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::internal::GenericClass
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_UTILS_CXX_METHOD_CALLBACK_HPP
#define IOX_UTILS_CXX_METHOD_CALLBACK_HPP

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

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

enum class MethodCallbackError
{
    INVALID_STATE,
    UNINITIALIZED_CALLBACK
};

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

    ConstMethodCallback() = default;
    ConstMethodCallback(const ConstMethodCallback& rhs) = default;
    ConstMethodCallback& operator=(const ConstMethodCallback& rhs) = default;
    ~ConstMethodCallback() = 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() = default;
    MethodCallback(const MethodCallback& rhs) = default;
    MethodCallback& operator=(const MethodCallback& rhs) = default;
    ~MethodCallback() = 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_utils/internal/cxx/method_callback.inl"

#endif

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