From 7e766f848d4f075fce815711b201d983aa8989dd Mon Sep 17 00:00:00 2001 From: nojhan Date: Sun, 22 Mar 2020 18:57:50 +0100 Subject: [PATCH] add eoStoreFunctor::pack to allocate & store in one line Instead of calling `new`, then `state.storeFunctor`, the user can just call `Class& inst = state.pack< Class >( params )` in one line. Use C++11's variadic templates. --- eo/src/eoFunctorStore.h | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/eo/src/eoFunctorStore.h b/eo/src/eoFunctorStore.h index ffcbe90d7..889b832de 100644 --- a/eo/src/eoFunctorStore.h +++ b/eo/src/eoFunctorStore.h @@ -54,20 +54,36 @@ public: /// Add an eoFunctorBase to the store template Functor& storeFunctor(Functor* r) - { + { #ifndef NDEBUG - unsigned int existing = std::count( vec.begin(), vec.end(), r ); - if( existing > 0 ) { - eo::log << eo::warnings << "WARNING: you asked eoFunctorStore to store the functor " << r << " " - << existing + 1 << " times, a segmentation fault may occur in the destructor." << std::endl; - } + unsigned int existing = std::count( vec.begin(), vec.end(), r ); + if( existing > 0 ) { + eo::log << eo::warnings << "WARNING: you asked eoFunctorStore to store the functor " << r << " " + << existing + 1 << " times, a segmentation fault may occur in the destructor." << std::endl; + } #endif - // If the compiler complains about the following line, - // check if you really are giving it a pointer to an - // eoFunctorBase derived object - vec.push_back(r); - return *r; - } + // If the compiler complains about the following line, + // check if you really are giving it a pointer to an + // eoFunctorBase derived object + vec.push_back(r); + return *r; + } + + /** Allocate the given functor, store it and return its reference. + * + * Indicate the class to instanciate as template paramater, + * and pass its constructor arguments. + * + * @code + * eoSelect& selector = state.pack< eoRankMuSelect >( mu ); + * @endcode + */ + template + Functor& pack( Args&&... args ) + { + Functor* f = new Functor(args...); + return this->storeFunctor(f); + } private :