• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

thru_image.hh

00001 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_CORE_IMAGE_VMORPH_THRU_IMAGE_HH
00028 # define MLN_CORE_IMAGE_VMORPH_THRU_IMAGE_HH
00029 
00035 
00036 # include <mln/core/internal/image_value_morpher.hh>
00037 # include <mln/core/concept/meta_function.hh>
00038 # include <mln/metal/bexpr.hh>
00039 # include <mln/trait/functions.hh>
00040 
00041 
00042 namespace mln
00043 {
00044 
00045   // Forward declaration.
00046   template <typename I, typename F> struct thru_image;
00047 
00048   namespace internal
00049   {
00050     template <typename I, typename F> struct thru_image_write;
00051     template <typename I, typename F> struct thru_image_read;
00052 
00054     template <typename I, typename F>
00055     struct thru_find_impl
00056     {
00057       typedef thru_image_write<I, F> write;
00058       typedef thru_image_read<I, F> read;
00059       typedef mlc_if(mlc_and(mln_trait_fun_is_assignable(F),
00060                      mlc_and(mlc_not(mlc_is_const(I)),
00061                              mlc_equal(mln_trait_image_pw_io(I),
00062                                        trait::image::pw_io::read_write))),
00063                      write, read) ret;
00064     };
00065 
00067     template <typename I, typename F>
00068     struct data< thru_image<I, F> >
00069     {
00070       data(I& ima, const F& f);
00071 
00072       I ima_;
00073       F f_;
00074     };
00075 
00076   } // end of namespace mln::internal
00077 
00078 
00079   namespace trait
00080   {
00081 
00082     template <typename I, typename F>
00083     struct image_< thru_image<I, F> > : image_< typename mln::internal::thru_find_impl<I, F>::ret > // Same as I except...
00084     {
00085       // ...these changes.
00086       typedef trait::image::category::value_morpher category;
00087       typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
00088       typedef trait::image::value_access::computed value_access;
00089     };
00090 
00091     template <typename I, typename F>
00092     struct image_< mln::internal::thru_image_write<I, F> > : image_< I > // Same as I except...
00093     {
00094       typedef trait::image::vw_io::read_write vw_io;
00095     };
00096 
00097     template <typename I, typename F>
00098     struct image_< mln::internal::thru_image_read<I, F> > : image_< I > // Same as I except...
00099     {
00100       typedef trait::image::vw_io::read vw_io;
00101     };
00102 
00103   } // end of namespace mln::trait
00104 
00105 
00106 
00107   // FIXME: Doc!
00108 
00109   namespace internal
00110   {
00111 
00112     template <typename I, typename F>
00113     class thru_image_read : public internal::image_value_morpher< I, typename F::result, thru_image<I,F> >
00114     {
00115     public:
00116 
00118       typedef thru_image<tag::image_<I>, F> skeleton;
00119 
00121       typedef mln_psite(I) psite;
00122 
00124       typedef mln_result(F) value;
00125 
00127       typedef value rvalue;
00128       typedef value lvalue; // Workaround for write operator()
00129 
00130       rvalue operator()(const mln_psite(I)& p) const;
00131       rvalue operator()(const mln_psite(I)& p);
00132 
00133     };
00134 
00135     // Inheritance from read ?!
00136     template <typename I, typename F>
00137     class thru_image_write : public thru_image_read<I,F>
00138     {
00139       public:
00140 
00142 //      typedef typename F::template lresult<typename F::argument>::ret lvalue;
00143         typedef typename F::lresult lvalue;
00144 
00145         using thru_image_read<I,F>::operator();
00146         lvalue operator()(const mln_psite(I)& p);
00147 
00148     };
00149   }
00150 
00154   //
00155   template <typename I, typename F>
00156   class thru_image : public internal::thru_find_impl<I, F>::ret
00157   {
00158   public:
00159 
00160     thru_image();
00161     thru_image(I& ima);
00162     thru_image(I& ima, const F& f);
00163 
00164     void init_(I& ima, const F& f);
00165 
00167     operator thru_image<const I, F>() const;
00168   };
00169 
00170   template <typename I, typename F>
00171   thru_image<I, F> thru(const mln::Function<F>& f,
00172                         Image<I>& ima);
00173 
00174   template <typename I, typename F>
00175   const thru_image<const I, F> thru(const mln::Function<F>& f,
00176                                     const Image<I>& ima);
00177 
00178   template <typename I, typename M>
00179   thru_image<I, mln_fun_with(M, mln_value(I))>
00180   thru(const mln::Meta_Function<M>& f, Image<I>& ima);
00181 
00182   template <typename I, typename M>
00183   const thru_image<const I, mln_fun_with(M, mln_value(I))>
00184   thru(const mln::Meta_Function<M>& f, const Image<I>& ima);
00185 
00186 # ifndef MLN_INCLUDE_ONLY
00187 
00188   // internal::data< thru_image<I,S> >
00189 
00190   namespace internal
00191   {
00192 
00193     template <typename I, typename F>
00194     inline
00195     data< thru_image<I, F> >::data(I& ima, const F& f)
00196       : ima_(ima),
00197         f_(f)
00198     {
00199     }
00200 
00201   } // end of namespace mln::internal
00202 
00203   // thru_image<I>
00204 
00205   template <typename I, typename F>
00206   inline
00207   thru_image<I, F>::thru_image()
00208   {
00209   }
00210 
00211   template <typename I, typename F>
00212   inline
00213   thru_image<I, F>::thru_image(I& ima, const F& f)
00214   {
00215     mln_precondition(ima.is_valid());
00216     init_(ima, f);
00217   }
00218 
00219   template <typename I, typename F>
00220   inline
00221   thru_image<I, F>::thru_image(I& ima)
00222   {
00223     mln_precondition(ima.is_valid());
00224     init_(ima, F());
00225   }
00226 
00227   template <typename I, typename F>
00228   inline
00229   void
00230   thru_image<I, F>::init_(I& ima, const F& f)
00231   {
00232     mln_precondition(! this->is_valid());
00233     mln_precondition(ima.is_valid());
00234     this->data_ = new internal::data< thru_image<I, F> >(ima, f);
00235   }
00236 
00237   template <typename I, typename F>
00238   inline
00239   thru_image<I, F>::operator thru_image<const I, F>() const
00240   {
00241     thru_image<const I, F> tmp(this->data_->ima_, this->data_->f_);
00242     return tmp;
00243   }
00244 
00245   namespace internal
00246   {
00247 
00248     template <typename I, typename F>
00249     inline
00250     typename thru_image_read<I, F>::rvalue
00251     thru_image_read<I, F>::operator()(const mln_psite(I)& p) const
00252     {
00253       mln_precondition(this->is_valid());
00254       return this->data_->f_(this->data_->ima_(p));
00255     }
00256 
00257     template <typename I, typename F>
00258     inline
00259     typename thru_image_read<I, F>::rvalue
00260     thru_image_read<I, F>::operator()(const mln_psite(I)& p)
00261     {
00262       mln_precondition(this->is_valid());
00263       return this->data_->f_(this->data_->ima_(p));
00264     }
00265 
00266     template <typename I, typename F>
00267     inline
00268     typename thru_image_write<I, F>::lvalue
00269     thru_image_write<I, F>::operator()(const mln_psite(I)& p)
00270     {
00271       mln_precondition(this->is_valid());
00272       return this->data_->f_(this->data_->ima_(p));
00273     }
00274 
00275   }
00276 
00277   // thru
00278   template <typename I, typename F>
00279   thru_image<I, F> thru(const mln::Function<F>& f,
00280                         Image<I>& ima)
00281   {
00282     thru_image<I, F> tmp(exact(ima), exact(f));
00283     return tmp;
00284   }
00285 
00286   template <typename I, typename F>
00287   thru_image<const I, F> thru(const mln::Function<F>& f,
00288                               const Image<I>& ima)
00289   {
00290     thru_image<const I, F> tmp(exact(ima), exact(f));
00291     return tmp;
00292   }
00293 
00294   template <typename I, typename M>
00295   thru_image<I, mln_fun_with(M, mln_value(I))>
00296   thru(const mln::Meta_Function<M>& f, Image<I>& ima)
00297   {
00298     typedef mln_fun_with(M, mln_value(I)) F;
00299     thru_image<I, F> tmp(exact(ima), F(exact(f).state()));
00300 
00301     return tmp;
00302   }
00303 
00304   template <typename I, typename M>
00305   thru_image<const I, mln_fun_with(M, mln_value(I))>
00306   thru(const mln::Meta_Function<M>& f, const Image<I>& ima)
00307   {
00308     typedef mln_fun_with(M, mln_value(I)) F;
00309     thru_image<const I, F> tmp(exact(ima), F(exact(f).state()));
00310 
00311     return tmp;
00312   }
00313 
00314 # endif // ! MLN_INCLUDE_ONLY
00315 
00316 } // end of namespace mln
00317 
00318 
00319 #endif // ! MLN_CORE_IMAGE_VMORPH_THRU_IMAGE_HH

Generated on Tue Oct 4 2011 15:24:41 for Milena (Olena) by  doxygen 1.7.1