Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 10981


Ignore:
Timestamp:
Dec 27, 2015, 11:04:09 PM (8 years ago)
Author:
landauf
Message:

not sure what's better: limiting functor to 5 arguments or having static arrays inside functions…
so here's a version that avoids the static arrays (but on the other hand is limited to 5 args).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/cpp11_v2/src/libraries/core/command/Functor.h

    r10980 r10981  
    415415        { enum { result = false }; };
    416416
     417        // Helper class to determine the N-th parameter of a variadic template (with 0 being the index of the first parameter)
     418        template <int n, typename T = void, typename... Other>
     419        struct GetNthParamType
     420        { typedef typename GetNthParamType<n - 1, Other...>::Type Type; };
     421        template <typename T, typename... Other>
     422        struct GetNthParamType<0, T, Other...>
     423        { typedef T Type; };
     424
    417425        //Barebones implementation of (make_)index_sequence for C++11
    418426        template <std::size_t...> struct index_sequence {};
     
    437445        };
    438446
     447        template <class T1>
     448        struct make_type_list_helper<T1>
     449        {
     450            template <std::size_t N, class... Types>
     451            struct make_type_list_impl : type_list<Types..., T1> {};
     452        };
     453
    439454        template <std::size_t N, class... Types>
    440455        struct make_type_list : make_type_list_helper<Types...>::template make_type_list_impl<N> {};
     
    442457        template <class... Types>
    443458        struct make_type_list<0u, Types...> : type_list<> {};
     459
     460        template <std::size_t N>
     461        struct make_type_list<N> : type_list<> {};
     462
     463        template <>
     464        struct make_type_list<0u> : type_list<> {};
    444465    }
    445466
     
    485506            virtual void evaluateArgument(unsigned int index, MultiType& argument) const override
    486507            {
    487                 static const std::array<std::function<bool(MultiType&)>, sizeof...(Params)> funcs = {&MultiType::convert<Params>...};
    488                 if (index < funcs.size())
     508                switch (index)
    489509                {
    490                     funcs[index](argument);
     510                    case 0: argument.convert<typename detail::GetNthParamType<0, Params...>::Type>(); break;
     511                    case 1: argument.convert<typename detail::GetNthParamType<1, Params...>::Type>(); break;
     512                    case 2: argument.convert<typename detail::GetNthParamType<2, Params...>::Type>(); break;
     513                    case 3: argument.convert<typename detail::GetNthParamType<3, Params...>::Type>(); break;
     514                    case 4: argument.convert<typename detail::GetNthParamType<4, Params...>::Type>(); break;
    491515                }
    492516            }
     
    507531            virtual std::string getTypenameParam(unsigned int index) const override
    508532            {
    509                 static const std::array<std::string, sizeof...(Params)> names = { typeToString<Params>()... };
    510                 if (index >= names.size())
     533                switch (index)
    511534                {
    512                     return "";
     535                    case 0:  return typeToString<typename detail::GetNthParamType<0, Params...>::Type>();
     536                    case 1:  return typeToString<typename detail::GetNthParamType<1, Params...>::Type>();
     537                    case 2:  return typeToString<typename detail::GetNthParamType<2, Params...>::Type>();
     538                    case 3:  return typeToString<typename detail::GetNthParamType<3, Params...>::Type>();
     539                    case 4:  return typeToString<typename detail::GetNthParamType<4, Params...>::Type>();
     540                    default: return "";
    513541                }
    514                 return names[index];
    515542            }
    516543
     
    530557            virtual const std::type_index getHeaderIdentifier(unsigned int params) const override
    531558            {
    532                 //+1 because e.g. for two parameters, we want typeids for zero, one, or two parameters
    533                 return getHeaderIdentifierHelper(params, detail::make_index_sequence<sizeof...(Params) + 1>{});
     559                switch (params)
     560                {
     561                    case 0:  return this->getTypelistIdentifier(detail::make_type_list<0, Params...>{});
     562                    case 1:  return this->getTypelistIdentifier(detail::make_type_list<1, Params...>{});
     563                    case 2:  return this->getTypelistIdentifier(detail::make_type_list<2, Params...>{});
     564                    case 3:  return this->getTypelistIdentifier(detail::make_type_list<3, Params...>{});
     565                    case 4:  return this->getTypelistIdentifier(detail::make_type_list<4, Params...>{});
     566                    default: return this->getTypelistIdentifier(detail::make_type_list<5, Params...>{});
     567                }
    534568            }
    535569
    536570    private:
    537 
    538571            /// Helper function that extracts index numbers of parameters from a tuple. Needed to call the function pointer with the correct amount of arguments.
    539572            template<typename Tup, std::size_t... index>
     
    541574            {
    542575                return detail::FunctorCaller<R, O, isconst, Params...>::call(this->functionPointer_, object, std::get<index>(std::forward<Tup>(tup))...);
    543             }
    544 
    545             /// Helper function to extract all identifiers of the function pointer using a deduced index sequence
    546             template<std::size_t... index>
    547             const std::type_index& getHeaderIdentifierHelper(unsigned int params, detail::index_sequence<index...>) const
    548             {
    549                 static const std::array<const std::type_index, sizeof...(index)> typeinfos = { getTypelistIdentifier(detail::make_type_list<index, Params...>{})... };
    550                 if (params >= typeinfos.size())
    551                 {
    552                     return typeinfos.back();
    553                 }
    554                 return typeinfos[params];
    555576            }
    556577
Note: See TracChangeset for help on using the changeset viewer.