// eventpp library // Copyright (C) 2018 Wang Qi (wqking) // Github: https://github.com/wqking/eventpp // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // 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 HETERCALLBACKLIST_I_H #define HETERCALLBACKLIST_I_H #include "typeutil_i.h" #include namespace eventpp { /* // We can't use std::tuple as HeterTuple because std::tuple is instantialized // somewhere in the Map in HeterEventDispatcherBase, that causes compile error // for that function type can't be declared. template using HeterTuple = std::tuple; template struct HeterTupleSize; template struct HeterTupleSize > { enum { value = std::tuple_size >::value }; }; */ template struct HeterTuple { }; template struct HeterTupleSize; template struct HeterTupleSize > { enum { value = sizeof...(Types) }; }; namespace internal_ { template struct FindPrototypeDefaultArgTransformer { using type = T; }; template < int N, typename PrototypeList_, typename Callable, template class ArgTransformer = FindPrototypeDefaultArgTransformer, int M = HeterTupleSize::value, typename Enabled = void > struct FindPrototypeByCallableFromIndex; template < int N, typename RT, typename ...Args, typename ...Others, typename Callable, template class ArgTransformer, int M > struct FindPrototypeByCallableFromIndex < N, HeterTuple, Callable, ArgTransformer, M, typename std::enable_if<(N < M)>::type > { enum { canInvoke = CanInvoke::type...>::value }; using Prototype = typename std::conditional< canInvoke, RT (Args...), typename FindPrototypeByCallableFromIndex, Callable, ArgTransformer, M>::Prototype >::type; using ArgsTuple = typename std::conditional< canInvoke, std::tuple::type>::type...>, typename FindPrototypeByCallableFromIndex, Callable, ArgTransformer, M>::ArgsTuple >::type; enum { index = canInvoke ? N : FindPrototypeByCallableFromIndex, Callable, ArgTransformer, M>::index }; }; template < int N, typename PrototypeList_, typename Callable, template class ArgTransformer, int M > struct FindPrototypeByCallableFromIndex < N, PrototypeList_, Callable, ArgTransformer, M, typename std::enable_if<(N >= M)>::type > { using Prototype = void; using ArgsTuple = std::tuple<>; enum { index = -1 }; }; template class ArgTransformer = FindPrototypeDefaultArgTransformer> struct FindPrototypeByCallable : public FindPrototypeByCallableFromIndex <0, PrototypeList_, Callable, ArgTransformer> { }; template struct FindPrototypeByArgsFromIndex; template struct FindPrototypeByArgsFromIndex , InArgs...> { enum { canInvoke = CanInvoke::value }; using Prototype = typename std::conditional< canInvoke, RT (Args...), typename FindPrototypeByArgsFromIndex, InArgs...>::Prototype >::type; using ArgsTuple = typename std::conditional< canInvoke, std::tuple::type>::type...>, typename FindPrototypeByArgsFromIndex, InArgs...>::ArgsTuple >::type; enum { index = canInvoke ? N : FindPrototypeByArgsFromIndex, InArgs...>::index }; }; template struct FindPrototypeByArgsFromIndex , InArgs...> { using Prototype = void; using ArgsTuple = std::tuple<>; enum { index = -1 }; }; template struct FindPrototypeByArgs : public FindPrototypeByArgsFromIndex <0, PrototypeList_, InArgs...> { }; template struct FindPrototypeByIndexHelper; template struct FindPrototypeByIndexHelper > { using NextType = FindPrototypeByIndexHelper >; using Prototype = typename NextType::Prototype; using ArgsTuple = typename NextType::ArgsTuple; enum { index = NextType::index }; }; template struct FindPrototypeByIndexHelper > { using Prototype = RT (Args...); using ArgsTuple = HeterTuple::type>::type...>; enum { index = N }; }; template struct FindPrototypeByIndexHelper > { using Prototype = void; using ArgsTuple = HeterTuple<>; enum { index = -1 }; }; template struct FindPrototypeByIndex : public FindPrototypeByIndexHelper <0, N, PrototypeList_> { }; template class Record> struct GetCallablePrototypeMaxSize; template class Record> struct GetCallablePrototypeMaxSize , Record> { enum { my = sizeof(Record), other = GetCallablePrototypeMaxSize, Record>::value }; enum { value = (my > other ? (int)my : (int)other) }; }; template