72 lines · c
1//===- DirectCaller.h -----------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef ORC_RT_UNITTEST_DIRECTCALLER_H10#define ORC_RT_UNITTEST_DIRECTCALLER_H11 12#include "orc-rt/WrapperFunction.h"13 14#include <memory>15#include <utility>16 17/// Make calls and call result handlers directly on the current thread.18class DirectCaller {19private:20 class DirectResultSender {21 public:22 virtual ~DirectResultSender() {}23 virtual void send(orc_rt_SessionRef S,24 orc_rt::WrapperFunctionBuffer ResultBytes) = 0;25 static void send(orc_rt_SessionRef S, uint64_t CallId,26 orc_rt_WrapperFunctionBuffer ResultBytes) {27 std::unique_ptr<DirectResultSender>(28 reinterpret_cast<DirectResultSender *>(29 static_cast<uintptr_t>(CallId)))30 ->send(S, ResultBytes);31 }32 };33 34 template <typename ImplFn>35 class DirectResultSenderImpl : public DirectResultSender {36 public:37 DirectResultSenderImpl(ImplFn &&Fn) : Fn(std::forward<ImplFn>(Fn)) {}38 void send(orc_rt_SessionRef S,39 orc_rt::WrapperFunctionBuffer ResultBytes) override {40 Fn(std::move(ResultBytes));41 }42 43 private:44 std::decay_t<ImplFn> Fn;45 };46 47 template <typename ImplFn>48 static std::unique_ptr<DirectResultSender>49 makeDirectResultSender(ImplFn &&Fn) {50 return std::make_unique<DirectResultSenderImpl<ImplFn>>(51 std::forward<ImplFn>(Fn));52 }53 54public:55 DirectCaller(orc_rt_SessionRef S, orc_rt_WrapperFunction Fn) : S(S), Fn(Fn) {}56 57 template <typename HandleResultFn>58 void operator()(HandleResultFn &&HandleResult,59 orc_rt::WrapperFunctionBuffer ArgBytes) {60 auto DR =61 makeDirectResultSender(std::forward<HandleResultFn>(HandleResult));62 Fn(S, static_cast<uint64_t>(reinterpret_cast<uintptr_t>(DR.release())),63 DirectResultSender::send, ArgBytes.release());64 }65 66private:67 orc_rt_SessionRef S;68 orc_rt_WrapperFunction Fn;69};70 71#endif // ORC_RT_UNITTEST_DIRECTCALLER_H72