|
| 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-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#ifndef ORC_RT_UNITTEST_DIRECTCALLER_H |
| 10 | +#define ORC_RT_UNITTEST_DIRECTCALLER_H |
| 11 | + |
| 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. |
| 18 | +class DirectCaller { |
| 19 | +private: |
| 20 | + class DirectResultSender { |
| 21 | + public: |
| 22 | + virtual ~DirectResultSender() {} |
| 23 | + virtual void send(orc_rt_SessionRef Session, |
| 24 | + orc_rt::WrapperFunctionBuffer ResultBytes) = 0; |
| 25 | + static void send(orc_rt_SessionRef Session, void *CallCtx, |
| 26 | + orc_rt_WrapperFunctionBuffer ResultBytes) { |
| 27 | + std::unique_ptr<DirectResultSender>( |
| 28 | + reinterpret_cast<DirectResultSender *>(CallCtx)) |
| 29 | + ->send(Session, ResultBytes); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + template <typename ImplFn> |
| 34 | + class DirectResultSenderImpl : public DirectResultSender { |
| 35 | + public: |
| 36 | + DirectResultSenderImpl(ImplFn &&Fn) : Fn(std::forward<ImplFn>(Fn)) {} |
| 37 | + void send(orc_rt_SessionRef Session, |
| 38 | + orc_rt::WrapperFunctionBuffer ResultBytes) override { |
| 39 | + Fn(Session, std::move(ResultBytes)); |
| 40 | + } |
| 41 | + |
| 42 | + private: |
| 43 | + std::decay_t<ImplFn> Fn; |
| 44 | + }; |
| 45 | + |
| 46 | + template <typename ImplFn> |
| 47 | + static std::unique_ptr<DirectResultSender> |
| 48 | + makeDirectResultSender(ImplFn &&Fn) { |
| 49 | + return std::make_unique<DirectResultSenderImpl<ImplFn>>( |
| 50 | + std::forward<ImplFn>(Fn)); |
| 51 | + } |
| 52 | + |
| 53 | +public: |
| 54 | + DirectCaller(orc_rt_SessionRef Session, orc_rt_WrapperFunction Fn) |
| 55 | + : Session(Session), 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(Session, reinterpret_cast<void *>(DR.release()), |
| 63 | + DirectResultSender::send, ArgBytes.release()); |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + orc_rt_SessionRef Session; |
| 68 | + orc_rt_WrapperFunction Fn; |
| 69 | +}; |
| 70 | + |
| 71 | +#endif // ORC_RT_UNITTEST_DIRECTCALLER_H |
0 commit comments