OTB  10.0.0
Orfeo Toolbox
otbVariadicInputsImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef otb_VariadicInputsImageFilter_h
21 #define otb_VariadicInputsImageFilter_h
22 
23 #include "itkImageSource.h"
24 
25 namespace otb
26 {
27 
57 template <class TOuptut, class... TInputs>
58 class VariadicInputsImageFilter : public itk::ImageSource<TOuptut>
59 {
60 public:
61  using Self = VariadicInputsImageFilter<TOuptut, TInputs...>;
62  using Pointer = itk::SmartPointer<Self>;
63  using ConstPointer = itk::SmartPointer<const Self>;
64  using Superclass = itk::ImageSource<TOuptut>;
66 
67  using InputTypesTupleType = std::tuple<TInputs...>;
68 
69  template <size_t I>
70  using InputImageType = typename std::tuple_element<I, InputTypesTupleType>::type;
71  static constexpr size_t NumberOfInputs = std::tuple_size<InputTypesTupleType>::value;
72 
73  // Good old new macro
74  itkNewMacro(Self);
75 
76  // Import definitions for SetInput and GetInput to avoid shadowing
77  // by SetInput<> and GetInput<> defined in this method
78  using Superclass::GetInput;
79  using Superclass::SetInput;
80 
84  template <std::size_t I = 0>
85  void SetInput(const InputImageType<I>* inputPtr)
86  {
87  static_assert(NumberOfInputs > I, "Template value I is out of range.");
88  this->SetNthInput(I, const_cast<InputImageType<I>*>(inputPtr));
89  }
91 
92 #define DefineLegacySetInputMacro(n) \
93  template <typename Tuple = InputTypesTupleType, typename Check = typename std::enable_if<n <= std::tuple_size<Tuple>::value>::type> \
94  void SetInput##n(const typename std::tuple_element<n - 1, Tuple>::type* img) \
95  { \
96  this->template SetInput<n - 1>(img); \
97  }
98 
99  // The following defines legacy setters SetInput1()
100  // ... SetInput10(), only if the number of input type is sufficient
111 
112 #undef DefineLegacySetInputMacro
113 
114  template <std::size_t I = 0>
116  {
117  static_assert(NumberOfInputs > I, "Template value I is out of range.");
118  return dynamic_cast<const InputImageType<I>*>(this->GetInput(I));
119  }
120 
124  void SetInputs(TInputs*... inputs)
125  {
126  auto inTuple = std::make_tuple(inputs...);
127  SetInputsImpl(inTuple, std::make_index_sequence<sizeof...(inputs)>{});
128  }
130 
134  auto GetInputs()
135  {
136  return GetInputsImpl(std::make_index_sequence<sizeof...(TInputs)>{});
137  }
139 
140 protected:
142  {
143  this->SetNumberOfRequiredInputs(sizeof...(TInputs));
144  };
145 
147 
148 private:
149  template <class Tuple, size_t... Is>
150  void SetInputsImpl(Tuple& t, std::index_sequence<Is...>)
151  {
152  // Will be easier to write in c++17 with fold expressions
153  // (this->template SetInput<Is>(std::get<Is>(t)),...);
154  (void) std::initializer_list<int>{(this->template SetInput<Is>(std::get<Is>(t)), 0)...};
155  }
156 
157  template <size_t... Is>
158  auto GetInputsImpl(std::index_sequence<Is...>)
159  {
160  return std::make_tuple(this->template GetInput<Is>()...);
161  }
162 
163  VariadicInputsImageFilter(const Self&) = delete;
164  void operator=(const Self&) = delete;
165 };
166 } // namespace otb
167 
168 #endif
Base class for image filter with variadic inputs.
const InputImageType< I > * GetInput()
VariadicInputsImageFilter(const Self &)=delete
void SetInput(const InputImageType< I > *inputPtr)
std::tuple< TInputs... > InputTypesTupleType
void operator=(const Self &)=delete
itk::ImageSource< TOuptut > Superclass
static constexpr vcl_size_t NumberOfInputs
auto GetInputsImpl(std::index_sequence< Is... >)
typename std::tuple_element< I, InputTypesTupleType >::type InputImageType
void SetInputsImpl(Tuple &t, std::index_sequence< Is... >)
itk::SmartPointer< const Self > ConstPointer
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.