// Copyright (c) 2025 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef MP_PROXY_TYPE_FUNCTION_H #define MP_PROXY_TYPE_FUNCTION_H #include namespace mp { //! Adapter to convert ProxyCallback object call to function object call. template class ProxyCallbackImpl final : public ProxyCallback> { using Fn = std::function; Fn m_fn; public: ProxyCallbackImpl(Fn fn) : m_fn(std::move(fn)) {} Result call(Args&&... args) override { return m_fn(std::forward(args)...); } }; template void CustomBuildField(TypeList>, Priority<1>, InvokeContext& invoke_context, Value& value, Output&& output) { if (value) { using Interface = typename decltype(output.get())::Calls; using Callback = ProxyCallbackImpl; output.set(kj::heap>( std::make_shared(std::forward(value)), invoke_context.connection)); } } // ProxyCallFn class is needed because c++11 doesn't support auto lambda parameters. // It's equivalent c++14: [invoke_context](auto&& params) { // invoke_context->call(std::forward(params)...) template struct ProxyCallFn { InvokeContext m_proxy; template decltype(auto) operator()(CallParams&&... params) { return this->m_proxy->call(std::forward(params)...); } }; template decltype(auto) CustomReadField(TypeList>, Priority<1>, InvokeContext& invoke_context, Input&& input, ReadDest&& read_dest) { if (input.has()) { using Interface = typename Decay::Calls; auto client = std::make_shared>( input.get(), &invoke_context.connection, /* destroy_connection= */ false); return read_dest.construct(ProxyCallFn{std::move(client)}); } return read_dest.construct(); }; } // namespace mp #endif // MP_PROXY_TYPE_FUNCTION_H