DGtal  1.2.0
PointFunctorHolder.h
1 
17 #pragma once
18 
29 #if defined(PointFunctorHolder_RECURSES)
30 #error Recursive header files inclusion detected in PointFunctorHolder.h
31 #else // defined(PointFunctorHolder_RECURSES)
33 #define PointFunctorHolder_RECURSES
34 
35 #if !defined PointFunctorHolder_h
37 #define PointFunctorHolder_h
38 
40 // Inclusions
41 #include <iostream>
42 #include <type_traits>
43 #include <utility>
44 
45 #include "DGtal/base/FunctorHolder.h"
47 
48 namespace DGtal
49 {
50 namespace functors
51 {
52 
90 template <
91  typename TPoint,
92  typename TValue,
93  typename TFunctor
94 >
96 {
97  // ----------------------- Interface --------------------------------------
98 public:
99 
100  // DGtal types
102  using Point = TPoint;
103  using Value = TValue;
104  using Functor = TFunctor;
105 
106  // ----------------------- Standard services ------------------------------
107 public:
108 
114  template <
115  typename Function,
116  // SFINAE trick to disable this constructor in a copy/move construction context.
117  typename std::enable_if<!std::is_base_of<PointFunctorHolder, typename std::decay<Function>::type>::value, int>::type = 0
118  >
119  explicit PointFunctorHolder(Function && fn)
120  : myFunctor(std::forward<Function>(fn))
121  {
122  }
123 
124  // ----------------------- Interface --------------------------------------
125 public:
126 
131  inline
132  Value operator() ( Point const& aPoint ) const
133  {
134  return myFunctor( aPoint );
135  }
136 
141  inline
142  void selfDisplay ( std::ostream & out ) const
143  {
144  out << "[PointFunctorHolder] holding a " << myFunctor;
145  }
146 
151  inline constexpr
152  bool isValid() const
153  {
154  return true;
155  }
156 
157 
158  // ------------------------- Private Datas --------------------------------
159 private:
161 
162 }; // End of class PointFunctorHolder
163 
164 
171 template <typename TPoint, typename TValue, typename TFunctor>
172 std::ostream&
173 operator<< ( std::ostream & out, const PointFunctorHolder<TPoint, TValue, TFunctor> & object )
174 {
175  object.selfDisplay(out);
176  return out;
177 }
178 
179 
190 template <
191  typename TPoint,
192  typename TValue,
193  typename TFunctor
194 >
195 inline auto
196 holdPointFunctor( TFunctor && aFunctor )
198  {
200  holdFunctor(std::forward<TFunctor>(aFunctor))
201  };
202  }
203 
213 template <
214  typename TPoint,
215  typename TFunctor
216 >
217 inline auto
218 holdPointFunctor( TFunctor && aFunctor )
220  TPoint,
221  typename std::decay<decltype(aFunctor(std::declval<TPoint>()))>::type,
222  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
223  >
224  {
225  return PointFunctorHolder<
226  TPoint,
227  typename std::decay<decltype(aFunctor(std::declval<TPoint>()))>::type,
228  decltype(holdFunctor(std::forward<TFunctor>(aFunctor)))
229  >{ holdFunctor(std::forward<TFunctor>(aFunctor)) };
230  }
231 
232 } // namespace functors
233 } // namespace DGtal
234 
235 #endif // !defined PointFunctorHolder_h
236 
237 #undef PointFunctorHolder_RECURSES
238 #endif // else defined(PointFunctorHolder_RECURSES)
Aim: hold any object callable on points as a DGtal::concepts::CPointFunctor model.
void selfDisplay(std::ostream &out) const
Writes/Displays the object on an output stream.
constexpr bool isValid() const
Checks the validity/consistency of the object.
PointFunctorHolder(Function &&fn)
Constructor.
Value operator()(Point const &aPoint) const
Evaluates the functor at the given point.
auto holdPointFunctor(TFunctor &&aFunctor) -> PointFunctorHolder< TPoint, TValue, decltype(holdFunctor(std::forward< TFunctor >(aFunctor)))>
PointFunctorHolder construction helper with specification of the return type.
std::ostream & operator<<(std::ostream &out, const FunctorHolder< FunctorStorage, NeedDereference > &object)
Overloads 'operator<<' for displaying objects of class FunctorHolder.
auto holdFunctor(Function &&fn) -> decltype(holdFunctorImpl(std::forward< Function >(fn), typename std::is_lvalue_reference< Function >{}))
Hold any callable object (function, functor, lambda, ...) as a C(Unary)Functor model.
DGtal is the top-level namespace which contains all DGtal functions and types.
const Point aPoint(3, 4)