Changeset 10985 for code/branches/cpp11_v2/src/libraries/core
- Timestamp:
- Dec 27, 2015, 11:43:12 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/cpp11_v2/src/libraries/core/command/Functor.h
r10982 r10985 116 116 #include "core/CorePrereqs.h" 117 117 118 #include <array> 119 #include <tuple> 118 #include <typeinfo> 120 119 121 120 #include "util/Output.h" … … 385 384 386 385 // see Functor::getFullIdentifier() 387 const std::type_info& getFullIdentifier() const386 virtual const std::type_info& getFullIdentifier() const override 388 387 { return typeid(F); } 389 388 … … 395 394 { 396 395 // Helper class to get the type of the function pointer with the given class, parameters, return-value, and constness 397 template <class R, class O, bool isconst, class... Params> struct FunctionPointer 398 template <class R, class O, class... Params> struct FunctionPointer<R, O, true, Params...> 399 template <class R, class... Params> struct FunctionPointer<R, void, false, Params...> 396 template <class R, class O, bool isconst, class... Params> struct FunctionPointer { typedef R (O::*Type)(Params...); }; 397 template <class R, class O, class... Params> struct FunctionPointer<R, O, true, Params...> { typedef R (O::*Type)(Params...) const; }; 398 template <class R, class... Params> struct FunctionPointer<R, void, false, Params...> { typedef R (*Type)(Params...); }; 400 399 401 400 // Helper class, used to call a function-pointer with a given object and parameters and to return its return-value (if available) 402 template <class R, class O, bool isconst, class... Params> struct FunctorCaller { static inline MultiType call(typename detail::FunctionPointer<R, O, isconst, Params...>::Type functionPointer, O* object, const Params&... parameters){ return (object->*functionPointer)(parameters...); } };403 template <class O, bool isconst, class... Params> struct FunctorCaller<void, O, isconst, Params...> { static inline MultiType call(typename detail::FunctionPointer<void, O, isconst, Params...>::Type functionPointer, O* object, const Params&... parameters) { (object->*functionPointer)(parameters...); return MultiType::Null; } };404 template <class R, bool isconst, class... Params> struct FunctorCaller<R, void, isconst, Params...> { static inline MultiType call(typename detail::FunctionPointer<R, void, isconst, Params...>::Type functionPointer, void*, const Params&... parameters){ return (*functionPointer)(parameters...); } };405 template <bool isconst, class... Params> struct FunctorCaller<void, void, isconst, Params...> { static inline MultiType call(typename detail::FunctionPointer<void, void, isconst, Params...>::Type functionPointer, void*, const Params&... parameters){ (*functionPointer)(parameters...); return MultiType::Null; } };401 template <class R, class O, bool isconst, class... Params> struct FunctorCaller { template <class... UnusedParams> static inline MultiType call(typename detail::FunctionPointer<R, O, isconst, Params...>::Type functionPointer, O* object, const Params&... parameters, const UnusedParams&...) { return (object->*functionPointer)(parameters...); } }; 402 template <class O, bool isconst, class... Params> struct FunctorCaller<void, O, isconst, Params...> { template <class... UnusedParams> static inline MultiType call(typename detail::FunctionPointer<void, O, isconst, Params...>::Type functionPointer, O* object, const Params&... parameters, const UnusedParams&...) { (object->*functionPointer)(parameters...); return MultiType::Null; } }; 403 template <class R, bool isconst, class... Params> struct FunctorCaller<R, void, isconst, Params...> { template <class... UnusedParams> static inline MultiType call(typename detail::FunctionPointer<R, void, isconst, Params...>::Type functionPointer, void*, const Params&... parameters, const UnusedParams&...) { return (*functionPointer)(parameters...); } }; 404 template <bool isconst, class... Params> struct FunctorCaller<void, void, isconst, Params...> { template <class... UnusedParams> static inline MultiType call(typename detail::FunctionPointer<void, void, isconst, Params...>::Type functionPointer, void*, const Params&... parameters, const UnusedParams&...) { (*functionPointer)(parameters...); return MultiType::Null; } }; 406 405 407 406 // Helper class to determine if a function has a returnvalue … … 421 420 { typedef T Type; }; 422 421 423 //Barebones implementation of (make_)index_sequence for C++11424 template <std::size_t...> struct index_sequence {};425 426 template <std::size_t N, std::size_t... Is>427 struct make_index_sequence : make_index_sequence<N - 1, N - 1, Is...> {};428 429 template <std::size_t... Is>430 struct make_index_sequence<0u, Is...> : index_sequence<Is...> {};431 432 422 //Helper structs to deduce the first N types of a parameter pack 433 423 template<class... Types> struct type_list {}; … … 491 481 virtual MultiType operator()(O* object, const MultiType& param1 = MultiType::Null, const MultiType& param2 = MultiType::Null, const MultiType& param3 = MultiType::Null, const MultiType& param4 = MultiType::Null, const MultiType& param5 = MultiType::Null) override 492 482 { 493 auto multis = std::make_tuple(param1, param2, param3, param4, param5); 494 return callHelper(object, multis, detail::make_index_sequence<sizeof...(Params)>{}); 483 return detail::FunctorCaller<R, O, isconst, Params...>::call(this->functionPointer_, object, param1, param2, param3, param4, param5); 495 484 } 496 485 … … 567 556 568 557 private: 569 /// Helper function that extracts index numbers of parameters from a tuple. Needed to call the function pointer with the correct amount of arguments.570 template<typename Tup, std::size_t... index>571 MultiType callHelper(O* object, Tup&& tup, detail::index_sequence<index...>)572 {573 return detail::FunctorCaller<R, O, isconst, Params...>::call(this->functionPointer_, object, std::get<index>(std::forward<Tup>(tup))...);574 }575 576 558 ///Helper function that deduces a parameter pack of types and returns the corresponding identifier 577 559 template<class... Types> … … 617 599 } 618 600 619 template <class R, class O, class OO, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...), OO* object) { return std::make_shared<FunctorTemplate<R, O, false, Params...>>(functionPointer, object); }///< Creates a new FunctorMember with the given function-pointer and an assigned object620 template <class R, class O, class OO, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...) const, OO* object) { return std::make_shared<FunctorTemplate<R, O, true, Params...>>(functionPointer, object); }///< Creates a new FunctorMember with the given function-pointer and an assigned object621 622 template <class R, class O, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...)) { return std::make_shared<FunctorTemplate<R, O, false, Params...>>(functionPointer); }///< Creates a new FunctorMember with the given function-pointer623 template <class R, class O, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...) const) { return std::make_shared<FunctorTemplate<R, O, true, Params...>>(functionPointer); }///< Creates a new FunctorMember with the given function-pointer624 625 template <class R, class... Params> inline FunctorStaticPtr createFunctor(R (*functionPointer)(Params...)) { return std::make_shared<FunctorTemplate<R, void, false, Params...>>(functionPointer); } ///< Creates a new Functorwith the given function-pointer601 template <class R, class O, class OO, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...), OO* object) { return std::make_shared<FunctorTemplate<R, O, false, Params...>>(functionPointer, object); } ///< Creates a new FunctorMember with the given function-pointer and an assigned object 602 template <class R, class O, class OO, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...) const, OO* object) { return std::make_shared<FunctorTemplate<R, O, true, Params...>>(functionPointer, object); } ///< Creates a new FunctorMember with the given function-pointer and an assigned object 603 604 template <class R, class O, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...)) { return std::make_shared<FunctorTemplate<R, O, false, Params...>>(functionPointer); } ///< Creates a new FunctorMember with the given function-pointer 605 template <class R, class O, class... Params> inline FunctorMemberPtr<O> createFunctor(R (O::*functionPointer)(Params...) const) { return std::make_shared<FunctorTemplate<R, O, true, Params...>>(functionPointer); } ///< Creates a new FunctorMember with the given function-pointer 606 607 template <class R, class... Params> inline FunctorStaticPtr createFunctor(R (*functionPointer)(Params...)) { return std::make_shared<FunctorTemplate<R, void, false, Params...>>(functionPointer); } ///< Creates a new FunctorStatic with the given function-pointer 626 608 627 609 template <class F> inline FunctorMemberPtr<F> createFunctor(const F& obj) { return detail::callableHelper(obj, &F::operator()); } ///< Creates a new Functor with a callable object
Note: See TracChangeset
for help on using the changeset viewer.