[Orxonox-commit 6324] r10981 - code/branches/cpp11_v2/src/libraries/core/command

landauf at orxonox.net landauf at orxonox.net
Sun Dec 27 23:04:09 CET 2015


Author: landauf
Date: 2015-12-27 23:04:09 +0100 (Sun, 27 Dec 2015)
New Revision: 10981

Modified:
   code/branches/cpp11_v2/src/libraries/core/command/Functor.h
Log:
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).

Modified: code/branches/cpp11_v2/src/libraries/core/command/Functor.h
===================================================================
--- code/branches/cpp11_v2/src/libraries/core/command/Functor.h	2015-12-27 19:36:21 UTC (rev 10980)
+++ code/branches/cpp11_v2/src/libraries/core/command/Functor.h	2015-12-27 22:04:09 UTC (rev 10981)
@@ -414,6 +414,14 @@
         struct FunctorHasReturnvalue<void>
         { enum { result = false }; };
 
+        // Helper class to determine the N-th parameter of a variadic template (with 0 being the index of the first parameter)
+        template <int n, typename T = void, typename... Other>
+        struct GetNthParamType
+        { typedef typename GetNthParamType<n - 1, Other...>::Type Type; };
+        template <typename T, typename... Other>
+        struct GetNthParamType<0, T, Other...>
+        { typedef T Type; };
+
         //Barebones implementation of (make_)index_sequence for C++11
         template <std::size_t...> struct index_sequence {};
 
@@ -436,11 +444,24 @@
             struct make_type_list_impl<1u, Types...> : type_list<Types..., T1> {};
         };
 
+        template <class T1>
+        struct make_type_list_helper<T1>
+        {
+            template <std::size_t N, class... Types>
+            struct make_type_list_impl : type_list<Types..., T1> {};
+        };
+
         template <std::size_t N, class... Types>
         struct make_type_list : make_type_list_helper<Types...>::template make_type_list_impl<N> {};
 
         template <class... Types>
         struct make_type_list<0u, Types...> : type_list<> {};
+
+        template <std::size_t N>
+        struct make_type_list<N> : type_list<> {};
+
+        template <>
+        struct make_type_list<0u> : type_list<> {};
     }
 
     /**
@@ -484,10 +505,13 @@
             // see Functor::evaluateArgument()
             virtual void evaluateArgument(unsigned int index, MultiType& argument) const override
             {
-                static const std::array<std::function<bool(MultiType&)>, sizeof...(Params)> funcs = {&MultiType::convert<Params>...};
-                if (index < funcs.size())
+                switch (index)
                 {
-                    funcs[index](argument);
+                    case 0: argument.convert<typename detail::GetNthParamType<0, Params...>::Type>(); break;
+                    case 1: argument.convert<typename detail::GetNthParamType<1, Params...>::Type>(); break;
+                    case 2: argument.convert<typename detail::GetNthParamType<2, Params...>::Type>(); break;
+                    case 3: argument.convert<typename detail::GetNthParamType<3, Params...>::Type>(); break;
+                    case 4: argument.convert<typename detail::GetNthParamType<4, Params...>::Type>(); break;
                 }
             }
 
@@ -506,12 +530,15 @@
             // see Functor::getTypenameParam()
             virtual std::string getTypenameParam(unsigned int index) const override
             {
-                static const std::array<std::string, sizeof...(Params)> names = { typeToString<Params>()... };
-                if (index >= names.size())
+                switch (index)
                 {
-                    return "";
+                    case 0:  return typeToString<typename detail::GetNthParamType<0, Params...>::Type>();
+                    case 1:  return typeToString<typename detail::GetNthParamType<1, Params...>::Type>();
+                    case 2:  return typeToString<typename detail::GetNthParamType<2, Params...>::Type>();
+                    case 3:  return typeToString<typename detail::GetNthParamType<3, Params...>::Type>();
+                    case 4:  return typeToString<typename detail::GetNthParamType<4, Params...>::Type>();
+                    default: return "";
                 }
-                return names[index];
             }
 
             // see Functor::getTypenameReturnvalue()
@@ -529,12 +556,18 @@
             // see Functor::getHeaderIdentifier(unsigned int)
             virtual const std::type_index getHeaderIdentifier(unsigned int params) const override
             {
-                //+1 because e.g. for two parameters, we want typeids for zero, one, or two parameters
-                return getHeaderIdentifierHelper(params, detail::make_index_sequence<sizeof...(Params) + 1>{});
+                switch (params)
+                {
+                    case 0:  return this->getTypelistIdentifier(detail::make_type_list<0, Params...>{});
+                    case 1:  return this->getTypelistIdentifier(detail::make_type_list<1, Params...>{});
+                    case 2:  return this->getTypelistIdentifier(detail::make_type_list<2, Params...>{});
+                    case 3:  return this->getTypelistIdentifier(detail::make_type_list<3, Params...>{});
+                    case 4:  return this->getTypelistIdentifier(detail::make_type_list<4, Params...>{});
+                    default: return this->getTypelistIdentifier(detail::make_type_list<5, Params...>{});
+                }
             }
 
     private:
-
             /// Helper function that extracts index numbers of parameters from a tuple. Needed to call the function pointer with the correct amount of arguments.
             template<typename Tup, std::size_t... index>
             MultiType callHelper(O* object, Tup&& tup, detail::index_sequence<index...>)
@@ -542,18 +575,6 @@
                 return detail::FunctorCaller<R, O, isconst, Params...>::call(this->functionPointer_, object, std::get<index>(std::forward<Tup>(tup))...);
             }
 
-            /// Helper function to extract all identifiers of the function pointer using a deduced index sequence
-            template<std::size_t... index>
-            const std::type_index& getHeaderIdentifierHelper(unsigned int params, detail::index_sequence<index...>) const
-            {
-                static const std::array<const std::type_index, sizeof...(index)> typeinfos = { getTypelistIdentifier(detail::make_type_list<index, Params...>{})... };
-                if (params >= typeinfos.size())
-                {
-                    return typeinfos.back();
-                }
-                return typeinfos[params];
-            }
-
             ///Helper function that deduces a parameter pack of types and returns the corresponding identifier
             template<class... Types>
             const std::type_index getTypelistIdentifier(detail::type_list<Types...>) const




More information about the Orxonox-commit mailing list