/** * Tencent is pleased to support the open source community by making Tars available. * * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. * * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * https://opensource.org/licenses/BSD-3-Clause * * Unless required by applicable law or agreed to in writing, software distributed * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR * CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ #ifndef _TARS_BIND_INTERNAL_H_ #define _TARS_BIND_INTERNAL_H_ #include #include #include #include "promise/function.h" namespace tars { namespace bind { template struct IndexSequence {}; template struct MakeIndexSequenceImpl; template struct MakeIndexSequenceImpl<0, Ns...> { using Type = IndexSequence; }; template struct MakeIndexSequenceImpl : MakeIndexSequenceImpl {}; template using MakeIndexSequence = typename MakeIndexSequenceImpl::Type; // Used to implement MakeArgsStorage. template struct MakeArgsStorageImpl { using Type = std::tuple; }; template struct MakeArgsStorageImpl { using Type = std::tuple, BoundArgs...>; }; template using MakeArgsStorage = typename MakeArgsStorageImpl::type...>::Type; class BindStateBase : public std::enable_shared_from_this { public: virtual ~BindStateBase() {}; protected: BindStateBase() {} }; template class RunnableAdapter; template class RunnableAdapter { public: typedef R RunType(Args...); using IsMethod = std::false_type; explicit RunnableAdapter(R(*f)(Args...)) : m_function(f) { } template R run(RunArgs&&... args) { return m_function(std::forward(args)...); } private: R (*m_function)(Args...); }; template class RunnableAdapter { public: typedef R RunType(T*, Args...); using IsMethod = std::true_type; explicit RunnableAdapter(R(T::*f)(Args...)) : m_function(f) { } template R run(T* obj, RunArgs&&... args) { return (obj->*m_function)(std::forward(args)...); } private: R (T::*m_function)(Args...); }; template class RunnableAdapter { public: typedef R RunType(const T*, Args...); using IsMethod = std::true_type; explicit RunnableAdapter(R(T::*f)(Args...) const) : m_function(f) { } template R run(const T* obj, RunArgs&&... args) { return (obj->*m_function)(std::forward(args)...); } private: R (T::*m_function)(Args...) const; }; template struct FunctorTraits { using RunnableType = RunnableAdapter; using RunType = typename RunnableType::RunType; }; template typename FunctorTraits::RunnableType MakeRunnable(const T& t) { return RunnableAdapter(t); } template struct InvokeHelper; template struct InvokeHelper { template static R makeItSo(Runnable r, RunArgs&&... args) { return r.run(std::forward(args)...); } }; template struct InvokeHelper { template static void makeItSo(Runnable r, RunArgs&&... args) { r.run(std::forward(args)...); } }; template struct InvokeHelper { template static R makeItSo(Runnable r, RefPtr p, RunArgs&&... args) { return r.run(p.get(), std::forward(args)...); } }; template struct InvokeHelper { template static void makeItSo(Runnable r, RefPtr p, RunArgs&&... args) { r.run(p.get(), std::forward(args)...); } }; template const T& Unwrap(const T& o) { return o; } template struct IsMoveOnlyType : public std::false_type {}; template struct IsMoveOnlyType > : public std::true_type {}; template struct IsMoveOnlyType > : public IsMoveOnlyType {}; template struct ParamForwardTypeNonMoveOnly { using ForwardType = const T&; }; template struct ParamForwardTypeNonMoveOnly { using ForwardType = const T*; }; template struct ParamForwardTypeNonMoveOnly { using ForwardType = const T*; }; template struct ParamForwardTypeMoveOnly { using ForwardType = T; }; template enable_if_t::value, T>& ParamForward(T& t) { return t; } template enable_if_t::value, T>& ParamForward(T& t) { return std::move(t); } template struct ParamTraits : public std::conditional::value, ParamForwardTypeMoveOnly, ParamForwardTypeNonMoveOnly >::type {}; template struct Invoker; template struct Invoker, StorageType, InvokeHelperType, R(UnboundForwardArgs...)> { static R run(BindStateBase* base, UnboundForwardArgs... unbound_args) { StorageType* storage = static_cast(base); return InvokeHelperType::makeItSo(storage->m_runnable, Unwrap(std::get(storage->m_boundArgs))..., ParamForward(unbound_args)...); } }; template struct IsRefMethod : public std::false_type {}; template struct IsRefMethod, Args...> : public std::true_type {}; template struct BindState; template struct BindState final : public BindStateBase { private: using StorageType = BindState; using RunnableType = Runnable; using IsRefCall = IsRefMethod::type...>; using BoundIndices = MakeIndexSequence; using UnboundForwardArgs = tl::DropTypeListItem::ForwardType...> >; using UnboundForwardRunType = f::MakeFunctionType; using InvokeHelperType = InvokeHelper; using UnboundArgs = tl::DropTypeListItem >; public: using InvokerType = Invoker; using UnboundRunType = f::MakeFunctionType; template BindState(const Runnable& runnable, ForwardArgs&&... boundArgs) : m_runnable(runnable) , m_boundArgs(std::forward(boundArgs)...) { } RunnableType m_runnable; MakeArgsStorage m_boundArgs; }; } // end namespace bind } // end namespace tars #endif