diff --git a/Modules/Core/Functor/include/otbFunctorImageFilter.h b/Modules/Core/Functor/include/otbFunctorImageFilter.h index b5fb4a2ff9ed8ab67baff13db10ca676e2dddd1a..047a1e6ca84200c16079f491c07f39d95198ba48 100644 --- a/Modules/Core/Functor/include/otbFunctorImageFilter.h +++ b/Modules/Core/Functor/include/otbFunctorImageFilter.h @@ -298,6 +298,29 @@ public: /** Run-time type information (and related methods). */ itkTypeMacro(FunctorImageFilter, VariadicInputsImageFilter); + + /** New() macro defined only if TFunction is default constructible */ + template <typename F = TFunction> static std::enable_if_t<std::is_default_constructible<F>::value, Pointer> New() + { + // Explicit default construct + FunctorType f; + + // Create a filter out of it + Pointer p = new Self(f, {{0,0}}); + p->UnRegister(); + return p; + } + + /** New() macro defined only if TFunction is NOT default constructible + * This will yield an error message since New() can not be implemented in this case. */ + template <typename F = TFunction> static std::enable_if_t<!std::is_default_constructible<F>::value, Pointer> New() + { + static_assert(std::is_default_constructible<F>::value,"Cannot call New() " + "function as the functor used for the filter creation is not default " + "constructible"); + + return nullptr; + } /** Get the functor object. * @@ -417,7 +440,6 @@ template <typename Functor, typename TNameMap = void> auto NewFunctorFilter(Func return NewFunctorFilter<FunctorType,TNameMap>(decoratedF,radius); } - }// namespace otb #ifndef OTB_MANUAL_INSTANTIATION diff --git a/Modules/Core/Functor/test/otbFunctorImageFilter.cxx b/Modules/Core/Functor/test/otbFunctorImageFilter.cxx index bc3c5f624f848c90a5e4b25950477c0e38cac9e3..d2c803c252c9b53f0074aa3d689bc699050b6768 100644 --- a/Modules/Core/Functor/test/otbFunctorImageFilter.cxx +++ b/Modules/Core/Functor/test/otbFunctorImageFilter.cxx @@ -162,6 +162,11 @@ template <typename TOut,typename TIn> struct TestOperatorVoidReturn res = filter1->GetVariadicNamedInput(tag{}); filter1->Update(); + // Test static New() operator + auto oldStyleNewFilter = FunctorImageFilter<decltype(functor)>::New(); + oldStyleNewFilter->SetVariadicInputs(in); + oldStyleNewFilter->Update(); + // Hack to silent -Wunused-but-set-variable std::cout<<res<<std::endl;