OTB  6.7.0
Orfeo Toolbox
otbVariadicInputsImageFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2019 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 
56 template<class TOuptut, class ... TInputs> class VariadicInputsImageFilter : public itk::ImageSource<TOuptut>
57 {
58 public:
59  using Self = VariadicInputsImageFilter<TOuptut, TInputs...>;
64 
65  using InputTypesTupleType = std::tuple<TInputs...>;
66 
67  template <size_t I> using InputImageType = typename std::tuple_element<I,InputTypesTupleType>::type;
68  static constexpr size_t NumberOfInputs = std::tuple_size<InputTypesTupleType>::value;
69 
70  // Good old new macro
71  itkNewMacro(Self);
72 
73  // Import definitions for SetInput and GetInput to avoid shadowing
74  // by SetInput<> and GetIntput<> defined in this method
77 
81  template <std::size_t I = 0>
82  void SetInput(const InputImageType<I>* inputPtr)
83  {
84  static_assert(NumberOfInputs > I, "Template value I is out of range.");
85  this->SetNthInput(I, const_cast<InputImageType<I>*>(inputPtr));
86  }
88 
89 #define DefineLegacySetInputMacro(n) \
90  template <typename Tuple = InputTypesTupleType, typename Check = typename std::enable_if<n <= std::tuple_size<Tuple>::value>::type> \
91  void SetInput##n(const typename std::tuple_element<n - 1, Tuple>::type* img) \
92  { \
93  this->template SetInput<n - 1>(img); \
94  }
95 
96  // The following defines legacy setters SetInput1()
97  // ... SetInput10(), only if the number of input type is sufficient
108 
109 #undef DefineLegacySetInputMacro
110 
111  template <std::size_t I = 0>
113  {
114  static_assert(NumberOfInputs > I, "Template value I is out of range.");
115  return dynamic_cast<const InputImageType<I>*>(this->GetInput(I));
116  }
117 
121  void SetInputs(TInputs*... inputs)
122  {
123  auto inTuple = std::make_tuple(inputs...);
124  SetInputsImpl(inTuple,std::make_index_sequence<sizeof...(inputs)>{});
125  }
127 
131  auto GetInputs()
132  {
133  return GetInputsImpl(std::make_index_sequence<sizeof...(TInputs)>{});
134  }
136 
137 protected:
139  {
140  this->SetNumberOfRequiredInputs(sizeof...(TInputs));
141  };
142 
143  ~VariadicInputsImageFilter() = default;
144 
145 private:
146  template<class Tuple, size_t...Is> auto SetInputsImpl(Tuple& t, std::index_sequence<Is...>)
147  {
148  return std::initializer_list<int>{(this->SetInput<Is>(std::get<Is>(t)), 0)...};
149  }
150 
151  template <size_t...Is> auto GetInputsImpl(std::index_sequence<Is...>)
152  {
153  return std::make_tuple(this->GetInput<Is>()...);
154  }
155 
156  VariadicInputsImageFilter(const Self&) = delete;
157  void operator=(const Self&) = delete;
158 };
159 
160 }
161 
162 #endif
virtual void SetNumberOfRequiredInputs(DataObjectPointerArraySizeType)
void operator=(const Self &)=delete
auto GetInputsImpl(std::index_sequence< Is...>)
void SetInput(const InputImageType< I > *inputPtr)
static constexpr vcl_size_t NumberOfInputs
virtual void SetInput(const DataObjectIdentifierType &key, DataObject *input)
typename std::tuple_element< I, InputTypesTupleType >::type InputImageType
auto SetInputsImpl(Tuple &t, std::index_sequence< Is...>)
VariadicInputsImageFilter< TOuptut, TInputs...> Self
Base class for image filter with variadic inputs.
DataObject * GetInput(const DataObjectIdentifierType &key)
const InputImageType< I > * GetInput()
virtual void SetNthInput(DataObjectPointerArraySizeType num, DataObject *input)