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.
This commit is contained in:
parent
38e3f40bad
commit
7e766f848d
1 changed files with 28 additions and 12 deletions
|
|
@ -54,20 +54,36 @@ public:
|
||||||
/// Add an eoFunctorBase to the store
|
/// Add an eoFunctorBase to the store
|
||||||
template <class Functor>
|
template <class Functor>
|
||||||
Functor& storeFunctor(Functor* r)
|
Functor& storeFunctor(Functor* r)
|
||||||
{
|
{
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
unsigned int existing = std::count( vec.begin(), vec.end(), r );
|
unsigned int existing = std::count( vec.begin(), vec.end(), r );
|
||||||
if( existing > 0 ) {
|
if( existing > 0 ) {
|
||||||
eo::log << eo::warnings << "WARNING: you asked eoFunctorStore to store the functor " << r << " "
|
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;
|
<< existing + 1 << " times, a segmentation fault may occur in the destructor." << std::endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// If the compiler complains about the following line,
|
// If the compiler complains about the following line,
|
||||||
// check if you really are giving it a pointer to an
|
// check if you really are giving it a pointer to an
|
||||||
// eoFunctorBase derived object
|
// eoFunctorBase derived object
|
||||||
vec.push_back(r);
|
vec.push_back(r);
|
||||||
return *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<EOT>& selector = state.pack< eoRankMuSelect<EOT> >( mu );
|
||||||
|
* @endcode
|
||||||
|
*/
|
||||||
|
template<class Functor, class... Args>
|
||||||
|
Functor& pack( Args&&... args )
|
||||||
|
{
|
||||||
|
Functor* f = new Functor(args...);
|
||||||
|
return this->storeFunctor(f);
|
||||||
|
}
|
||||||
|
|
||||||
private :
|
private :
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue