//===- DirectCaller.h -----------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef ORC_RT_UNITTEST_DIRECTCALLER_H #define ORC_RT_UNITTEST_DIRECTCALLER_H #include "orc-rt/WrapperFunction.h" #include #include /// Make calls and call result handlers directly on the current thread. class DirectCaller { private: class DirectResultSender { public: virtual ~DirectResultSender() {} virtual void send(orc_rt_SessionRef S, orc_rt::WrapperFunctionBuffer ResultBytes) = 0; static void send(orc_rt_SessionRef S, uint64_t CallId, orc_rt_WrapperFunctionBuffer ResultBytes) { std::unique_ptr( reinterpret_cast( static_cast(CallId))) ->send(S, ResultBytes); } }; template class DirectResultSenderImpl : public DirectResultSender { public: DirectResultSenderImpl(ImplFn &&Fn) : Fn(std::forward(Fn)) {} void send(orc_rt_SessionRef S, orc_rt::WrapperFunctionBuffer ResultBytes) override { Fn(std::move(ResultBytes)); } private: std::decay_t Fn; }; template static std::unique_ptr makeDirectResultSender(ImplFn &&Fn) { return std::make_unique>( std::forward(Fn)); } public: DirectCaller(orc_rt_SessionRef S, orc_rt_WrapperFunction Fn) : S(S), Fn(Fn) {} template void operator()(HandleResultFn &&HandleResult, orc_rt::WrapperFunctionBuffer ArgBytes) { auto DR = makeDirectResultSender(std::forward(HandleResult)); Fn(S, static_cast(reinterpret_cast(DR.release())), DirectResultSender::send, ArgBytes.release()); } private: orc_rt_SessionRef S; orc_rt_WrapperFunction Fn; }; #endif // ORC_RT_UNITTEST_DIRECTCALLER_H