/*************************************************************************************************** Copyright (C) 2023 The Qt Company Ltd. SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only ***************************************************************************************************/ #pragma once #include "qdotnetmarshal.h" #ifdef Q_OS_WINDOWS # define QDOTNETFUNCTION_CALLTYPE __stdcall #else # define QDOTNETFUNCTION_CALLTYPE #endif template class QDotNetFunction { public: QDotNetFunction(void *funcPtr = nullptr) : funcPtr(reinterpret_cast(funcPtr)) {} QDotNetFunction(const QDotNetFunction &cpySrc) : funcPtr(cpySrc.funcPtr) {} QDotNetFunction &operator=(const QDotNetFunction &cpySrc) { this->funcPtr = cpySrc.funcPtr; return *this; } void *ptr() const { return reinterpret_cast(funcPtr); } bool isValid() const { return funcPtr != nullptr; } typename QDotNetInbound::TargetType operator()( typename QDotNetOutbound::SourceType... arg) const { if (!isValid()) return QDotNetNull::value(); return QDotNetInbound::convert(funcPtr(QDotNetOutbound::convert(arg)...)); } typename QDotNetInbound::TargetType invoke(const QDotNetRef &obj, typename QDotNetOutbound::SourceType... arg) const { return operator()(arg...); } typename QDotNetInbound::TargetType invoke(nullptr_t nullObj, typename QDotNetOutbound::SourceType... arg) const { return operator()(arg...); } private: using Delegate = typename QDotNetInbound::InboundType(QDOTNETFUNCTION_CALLTYPE *)( typename QDotNetOutbound::OutboundType...); Delegate funcPtr = nullptr; }; template class QDotNetFunction { public: QDotNetFunction(void *funcPtr = nullptr) : funcPtr(reinterpret_cast(funcPtr)) {} void *ptr() const { return reinterpret_cast(funcPtr); } bool isValid() const { return funcPtr != nullptr; } void operator()(typename QDotNetOutbound::SourceType... arg) const { if (isValid()) funcPtr(QDotNetOutbound::convert(arg)...); } void invoke(const QDotNetRef &obj, typename QDotNetOutbound::SourceType... arg) const { operator()(arg...); } void invoke(nullptr_t nullObj, typename QDotNetOutbound::SourceType... arg) const { operator()(arg...); } private: using Delegate = void(QDOTNETFUNCTION_CALLTYPE *)( typename QDotNetOutbound::OutboundType...); Delegate funcPtr = nullptr; };