aimstil  5.0.5
DynImage.h
Go to the documentation of this file.
1 #ifndef TIL_DYNIMAGE_H
2 #define TIL_DYNIMAGE_H
3 
4 // include from STL
5 #include <typeinfo>
6 
7 // include from BOOST
8 #include "boost/mpl/assert.hpp"
9 #include "boost/mpl/equal.hpp"
10 #include "boost/mpl/int.hpp"
11 #include "boost/mpl/placeholders.hpp"
12 #include "boost/mpl/transform.hpp"
13 #include "boost/mpl/vector.hpp"
14 
15 // include from TIL library
16 #include "til/til_common.h"
17 #include "til/templateTools.h"
18 
19 namespace til {
20 
49 
50  template < template < typename > class TImage >
51  class DynImage : public Image_label
52  {
53  public: // constructors & destructor
54  virtual ~DynImage() {};
55  };
56 
57 
58 
61  template < template < typename > class TImage, typename T >
62  class DynImage_typed : public DynImage<TImage>
63  {
64  public:
65  DynImage_typed(TImage<T> & im) : m_im(im) {}
66  public:
67  TImage<T> & image() { return m_im; }
68 
69  private:
70  TImage<T> & m_im;
71  };
72 
73  /*
74  template < template < typename > class TImage, typename value_type >
75  DynImage<TImage> * dynImageFactory(TImage<value_type> *im)
76  {
77  return new DynImage_typed<TImage, value_type>(im);
78  }
79  */
80 
81  /*
82  template < template <typename> class TImage, typename value_type>
83  DynImage_typed<TImage, value_type> *
84  typecast(DynImage<TImage> * dynim)
85  {
86  return dynamic_cast<DynImage_typed<TImage, value_type>*>((DynImage<TImage>*)dynim));
87  }
88  */
89 
90  // Note that the first template parameter is redundant and thus theoretically
91  // not needed, however I could not find a C++ way to deduce it from the other :(
92  template < template <typename> class TImage, class TImage0 >
94  dynImageFactory(TImage0 & im)
95  {
97  }
98 
100 #define TIL_DYNIM_BLOCKS(funcall) \
101 TIL_DYNIM_BLOCKS_BEGIN \
102 TIL_DYNIM_BLOCK(funcall, uchar) \
103 TIL_DYNIM_BLOCK(funcall, char) \
104 TIL_DYNIM_BLOCK(funcall, ushort) \
105 TIL_DYNIM_BLOCK(funcall, short) \
106 TIL_DYNIM_BLOCK(funcall, int) \
107 TIL_DYNIM_BLOCK(funcall, float) \
108 TIL_DYNIM_BLOCK(funcall, double) \
109 TIL_DYNIM_BLOCKS_END \
110 
111 #define TIL_DYNIM_BLOCKS_BEGIN if(0) {}
112 #define TIL_DYNIM_BLOCKS_END else { throw std::runtime_error("Unknown dynamic image type"); }
113 #define TIL_DYNIM_BLOCK(funcall, type) \
114 else if (DynImage_typed<TImage, type > *pim = dynamic_cast<DynImage_typed<TImage, type >*>(&dynim)) \
115 { \
116  typedef type PixelType; \
117  funcall \
118 } \
119 
120 
121 
130  template < typename Functor, typename TReturn = void >
131  class Dyn : public Functor
132  {
133  public: // constructors & destructor
134 
135  Dyn(const Functor &functor) : Functor(functor) {}
136 
137  public: // operators
138 
139  template < template < typename > class TImage >
140  TReturn operator()(DynImage<TImage> * dynim)
141  {
142  TIL_DYNIM_BLOCKS( return m_functor(dynim.image()); )
143  }
144  };
145 
147  template < typename TReturn, typename Functor >
148  Dyn<Functor, TReturn> dyn(const Functor &functor)
149  {
150  return Dyn<Functor, TReturn>(functor);
151  }
152 
154  template < typename Functor >
155  Dyn<Functor, void> dyn(const Functor &functor)
156  {
157  return Dyn<Functor, void>(functor);
158  }
159 
160  /*
161  template < int N >
162  struct MyDynCollection {};
163 
164  template <> struct MyDynCollection<0> { static const int N = 0; typedef Dyn<ImageC<int> > Type; };
165  template <> struct MyDynCollection<1> { static const int N = 1; typedef Dyn<ImageC<short> > Type; };
166  template <> struct MyDynCollection<2> { static const int N = 2; typedef Dyn<ImageC<double> > Type; };
167 
168  template < class TDynCollection >
169  struct DynCollectionTraits {};
170 
171  template <> struct DynCollectionTraits<MyDynCollection> { static int N = 3; };
172  */
173 
174  class DynBase {};
175 
176  template < typename T >
177  class DynTyped : public DynBase
178  {
179  public: // typedefs
180  typedef T Type;
181  public: // constructors & destructor
182  DynTyped(T elem) : m_elem(elem) {}
183  private: // data
184  T m_elem;
185  };
186 
187  typedef boost::mpl::vector<
188  unsigned char,
189  char,
190  unsigned short,
191  short,
192  int,
193  float,
194  double
196 
197  typedef boost::mpl::vector<
199  ImageC<char>,
202  ImageC<int>,
205  > imeu;
206 
207 
208  typedef boost::mpl::transform<StandardNumericTypes, ImageC<boost::mpl::_1> >::type toto;
209 
210  //boost::mpl::at<toto, boost::mpl::int_<0> >::type i(8);
211 
212  BOOST_MPL_ASSERT(( boost::mpl::equal<imeu,toto> ));
213 
214 
215  /*
216  template < typename T >
217  class DynamicCast
218  {
219  T *im = dynamic_cast<T*>()
220  DynImage_typed<TImage, type > *im = dynamic_cast<DynImage_typed<TImage, type >*>((DynImage<TImage>*)dynim)
221 
222  };
223  */
224 
225 
226  /*
227  template < int TN, template <int> typename TFunctor >
228  class Loop
229  {
230  public:
231  static const int N = TN;
232  static void execute()
233  {
234  TFunctor<N>();
235  Loop<N-1>::execute();
236  }
237  };
238 
239  template <> class loop<0>
240  {
241  public:
242  static void execute() {}
243  };
244  */
245 
246  /*
247  template <class TClass, class TFunctor>
248  class ExecuteIfIsA
249  {
250  public:
251  void operator()
252  {
253  if (
254  }
255  };
256  */
257 
258 
259  template < class TMPLContainer >
260  class Loop
261  {
262  public: // typedefs
264  typedef typename boost::mpl::begin<TMPLContainer> begin;
265  typedef typename boost::mpl::end<TMPLContainer> end;
266 
267  public: // static functions
268 
273  template < template <typename> class TFunctor >
274  static void execute()
275  {
276  Self::template execute<Self::begin, TFunctor>();
277  }
278 
282  template < class TFunctor >
283  static void execute(const TFunctor &functor = TFunctor())
284  {
285  Self::template execute<Self::begin, TFunctor>(functor);
286  }
287 
288  private:
289 
290  template < class TMPLIterator, template < typename > class TFunctor >
291  static void execute()
292  {
293  // Stop here if we reached the end of the container
294  if (boost::is_same<typename TMPLIterator::type, typename boost::mpl::end<TMPLContainer>::type>::value) return;
295 
296  // Call functor with current type as its template parameter
297  TFunctor<typename TMPLIterator::type> functor;
298  functor();
299 
300  // go to next element
301  execute<typename boost::mpl::next<TMPLIterator>::type, TFunctor>(functor);
302  }
303  template < class TMPLIterator, class TFunctor >
304  static void execute(const TFunctor &functor)
305  {
306  // Stop here if we reached the end of the container
307  if (boost::is_same<typename TMPLIterator::type, typename Self::end::type>::value) return;
308 
309  // Call functor with current type as its template parameter
310  functor.template operator()<typename TMPLIterator::type>();
311 
312  // go to next element
313  execute<typename boost::mpl::next<TMPLIterator>::type, TFunctor>(functor);
314  }
315  };
316 
317  template < typename TFunctor, typename T >
319  {
320  public:
321 
322  template < typename V >
323  void operator()()
324  {
325  if (V object = dynamic_cast<V>(m_object))
326  {
327  m_functor(object);
328  }
329  }
330 
331  private: // data
332 
333  T m_object;
334  TFunctor m_functor;
335  };
336 
337 
338  //boost::mpl::for_each<TypeCollection>(ExecuteIfDynCast(object, functor));
339 
340  template < class TTypeCollection, class TFunctor >
341  void dyn(const TFunctor & functor)
342  {
344  }
345 
346 
348  class Object
349  {
350  //virtual void operator()() = 0;
351 
352  /*
353  template < template < typename, typename > class TBinaryFunctor >
354  virtual TBinaryFunctor<Object, Object> makeFunctor(TBinaryFunctor f, Object i);
355 
356  template < class TFunctor, TTypeCollection >
357  virtual WrapFunctor<TFunctor, TTypeCollection>
358  peelFunctor(WrapFunctor<TFunctor, TTypeCollection>);
359  */
360  };
361 
363  template < typename T >
365  {
366  //virtual void operator()() { this->operator(); }
367 
368  /*
369  template < template < typename, typename > class TBinaryFunctor >
370  virtual TBinaryFunctor<Object, Object> makeFunctor(TBinaryFunctor f, Object i)
371  {
372  return i.makeFunctor(f, *this);
373  }
374 
375  template < template < typename, typename > class TBinaryFunctor, typename U >
376  virtual TBinaryFunctor<Object, Object> makeFunctor(TBinaryFunctor f, Object_typed<U> i)
377  {
378  return TBinaryFunctor<Object_typed<U>, Object_typed<T> >(f);
379  }
380 
381  template < class TFunctor, TTypeCollection >
382  virtual WrapFunctor<TFunctor, TTypeCollection>
383  peelFunctor(WrapFunctor<TFunctor, TTypeCollection>)
384  {
385  }
386  */
387  };
388 
389  //wrapFunctor(Mul)(i,j,k);
390 
391 /*
392  template < typename TFunctor, typename TTypeCollection = None >
393  class WrapFunctor
394  {
395  operator(Object i)
396  {
397  i->peelFunctor(*this);
398  };
399 
400  TFunctor m_functor;
401  };
402 
403 
404  mul(Image i, Image j)
405  {
406  any f = FunctorFactory(Mul, i, j);
407  f(i,j);
408  }
409 
410  mul(Object i, Object j)
411  {
412  GenericBinaryFunctor f = binaryFunctorFactory(Mul, i, j);
413  f(i,j);
414  };
415 
416  template < typename TBinaryFunctor >
417  binaryFunctorFactory(TBinaryFunctor f, Object i, Object j)
418  {
419  return i->makeFunctor(f, j);
420  };
421 */
422  /*
423  template < template <int> class TDynCollection, typename TFunctor >
424  void dyn(const TFunctor & functor)
425  {
426  Loop<DynCollectionTraits<TDynCollection>::N, DynCompareAndExecute>::execute();
427  }
428  */
429 
430 //#undef TIL_DYNIM_BLOCKS
431 //#undef TIL_DYNIM_BLOCK
432 //#undef TIL_DYNIM_BLOCK_BEGIN
433 //#undef TIL_DYNIM_BLOCK_END
434 } //namespace
435 
436 #endif
437 
438 
A concrete instance of a generic object, containing a real object of type T.
Definition: DynImage.h:364
static void execute(const TFunctor &functor=TFunctor())
Execute a functor.
Definition: DynImage.h:283
DynImage_typed(TImage< T > &im)
Definition: DynImage.h:65
boost::mpl::begin< TMPLContainer > begin
Definition: DynImage.h:264
DynTyped(T elem)
Definition: DynImage.h:182
Belongs to package Box Do not include directly, include til/Box.h instead.
Definition: Accumulator.h:10
General macros, definitions and functions.
Child class of dynamic image base class, implementing a specific type.
Definition: DynImage.h:62
boost::mpl::end< TMPLContainer > end
Definition: DynImage.h:265
A generic object.
Definition: DynImage.h:348
Loop< TMPLContainer > Self
Definition: DynImage.h:263
static void execute()
Execute a templated functor.
Definition: DynImage.h:274
boost::mpl::vector< unsigned char, char, unsigned short, short, int, float, double > StandardNumericTypes
Definition: DynImage.h:195
#define TIL_DYNIM_BLOCKS(funcall)
Macro for code to do dynamic type resolution.
Definition: DynImage.h:100
Base class for dynamic type images.
Definition: DynImage.h:51
DynImage< TImage > * dynImageFactory(TImage0 &im)
Definition: DynImage.h:94
virtual ~DynImage()
Definition: DynImage.h:54
boost::mpl::transform< StandardNumericTypes, ImageC< boost::mpl::_1 > >::type toto
Definition: DynImage.h:208
Dyn< Functor, TReturn > dyn(const Functor &functor)
Conveniance function to create dynamic functor wrapper with return type.
Definition: DynImage.h:148
BOOST_MPL_ASSERT((boost::mpl::equal< imeu, toto >))
Collects template tools used for library implementation.
Functor wrapper for dynamic type images.
Definition: DynImage.h:131
Dyn(const Functor &functor)
Definition: DynImage.h:135
Image class using contiguous memory.
TImage< T > & image()
Definition: DynImage.h:67
TReturn operator()(DynImage< TImage > *dynim)
Definition: DynImage.h:140
boost::mpl::vector< ImageC< unsigned char >, ImageC< char >, ImageC< unsigned short >, ImageC< short >, ImageC< int >, ImageC< float >, ImageC< double > > imeu
Definition: DynImage.h:205