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

extension_val.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_CORE_IMAGE_DMORPH_EXTENSION_VAL_HH
00027 # define MLN_CORE_IMAGE_DMORPH_EXTENSION_VAL_HH
00028 
00035 
00036 # include <mln/core/internal/image_identity.hh>
00037 
00038 
00039 
00040 namespace mln
00041 {
00042 
00043   // Forward declaration.
00044   template <typename I> struct extension_val;
00045 
00046 
00047   namespace internal
00048   {
00049 
00051     template <typename I>
00052     struct data< extension_val<I> >
00053     {
00054       data(I& ima, const mln_value(I)& val_);
00055 
00056       I ima_;
00057       mln_value(I) val_;
00058     };
00059 
00060   } // end of namespace mln::internal
00061 
00062 
00063   namespace trait
00064   {
00065 
00066     template <typename I>
00067     struct image_< extension_val<I> > : image_< I > // Same as I except...
00068     {
00069       // ...these changes.
00070       typedef trait::image::category::identity_morpher category;
00071       typedef mln_internal_trait_image_speed_from(I) speed; // Un-fastest.
00072       typedef trait::image::value_access::indirect value_access;
00073 
00074       // extended domain
00075       typedef trait::image::ext_domain::infinite ext_domain;
00076       typedef trait::image::ext_value::single    ext_value;
00077       typedef trait::image::ext_io::read_write   ext_io;
00078     };
00079 
00080     template <typename I, typename V>
00081     struct ch_value< extension_val<I>, V >
00082     {
00083       typedef mlc_converts_to(mln_value(I), V) keep_ext;
00084       typedef mln_ch_value(I, V) Iv;
00085       typedef extension_val<Iv>  Iv_ext;
00086       typedef mlc_if(keep_ext, Iv_ext, Iv) ret;
00087     };
00088 
00089   } // end of namespace mln::trait
00090 
00091 
00092 
00096   //
00097   template <typename I>
00098   class extension_val :
00099     public internal::image_identity< I, mln_domain(I), extension_val<I> >
00100   {
00101   public:
00102 
00104     typedef extension_val< tag::image_<I> > skeleton;
00105 
00107     typedef mln_value(I) value;
00108 
00110     typedef mln_value(I) rvalue;
00111 
00112 
00114     extension_val();
00115 
00117     extension_val(I& ima, const mln_value(I)& val);
00118 
00121     void init_(I& ima, const mln_value(I)& val);
00122 
00123 
00125     // Tech note: the 'template' allows for multiple input.
00126     template <typename P>
00127     bool has(const P& p) const;
00128 
00129 
00131     mln_value(I) operator()(const mln_psite(I)& p) const;
00132 
00134     mln_morpher_lvalue(I) operator()(const mln_psite(I)& p);
00135 
00136 
00138     const mln_value(I)& extension() const;
00139 
00141     void change_extension(const mln_value(I)& val);
00142   };
00143 
00144 
00145   // init_
00146 
00147   template <typename I, typename J>
00148   void init_(tag::image_t, extension_val<I>& target, const J& model);
00149 
00150   template <typename V, typename I>
00151   void init_(tag::extension_t, V& target, const extension_val<I>& model);
00152 
00153 
00154 
00155 # ifndef MLN_INCLUDE_ONLY
00156 
00157   // internal::data< extension_val<I,S> >
00158 
00159   namespace internal
00160   {
00161 
00162     template <typename I>
00163     inline
00164     data< extension_val<I> >::data(I& ima, const mln_value(I)& val)
00165       : ima_(ima),
00166         val_(val)
00167     {
00168     }
00169 
00170   } // end of namespace mln::internal
00171 
00172   // extension_val<I>
00173 
00174   template <typename I>
00175   inline
00176   extension_val<I>::extension_val()
00177   {
00178   }
00179 
00180   template <typename I>
00181   inline
00182   extension_val<I>::extension_val(I& ima, const mln_value(I)& val)
00183   {
00184     init_(ima, val);
00185   }
00186 
00187   template <typename I>
00188   inline
00189   void
00190   extension_val<I>::init_(I& ima, const mln_value(I)& val)
00191   {
00192     this->data_ = new internal::data< extension_val<I> >(ima, val);
00193   }
00194 
00195   template <typename I>
00196   template <typename P>
00197   inline
00198   bool
00199   extension_val<I>::has(const P&) const
00200   {
00201     return true;
00202   }
00203 
00204   template <typename I>
00205   inline
00206   mln_value(I)
00207   extension_val<I>::operator()(const mln_psite(I)& p) const
00208   {
00209     mln_precondition(this->is_valid());
00210     // if-else is preferred to the ternary op to allow conversions.
00211     if (this->data_->ima_.domain().has(p))
00212       return this->data_->ima_(p);
00213     else
00214       return this->data_->val_;
00215   }
00216 
00217   template <typename I>
00218   inline
00219   mln_morpher_lvalue(I)
00220   extension_val<I>::operator()(const mln_psite(I)& p)
00221   {
00222     static mln_value(I) cpy;
00223     mln_precondition(this->is_valid());
00224     // See the above comment about if-else v. ternary.
00225     if (this->data_->ima_.domain().has(p))
00226       return this->data_->ima_(p);
00227     else
00228       {
00229         // This hack makes the extension value non-modifiable.
00230         cpy = this->data_->val_;
00231         return cpy;
00232       }
00233   }
00234 
00235   template <typename I>
00236   inline
00237   const mln_value(I)&
00238   extension_val<I>::extension() const
00239   {
00240     mln_precondition(this->is_valid());
00241     return this->data_->val_;
00242   }
00243 
00244   template <typename I>
00245   inline
00246   void
00247   extension_val<I>::change_extension(const mln_value(I)& val)
00248   {
00249     mln_precondition(this->is_valid());
00250     this->data_->val_ = val;
00251   }
00252 
00253 
00254   // init_
00255 
00256   template <typename I, typename J>
00257   void init_(tag::image_t, extension_val<I>& target, const J& model)
00258   {
00259     I ima;
00260     init_(tag::image, ima, model);
00261     mln_value(I) val;
00262     init_(tag::extension, val, model);
00263     target.init_(ima, val);
00264   }
00265 
00266   template <typename V, typename I>
00267   void init_(tag::extension_t, V& target, const extension_val<I>& model)
00268   {
00269     mlc_converts_to(mln_value(I), V)::check();
00270     target = static_cast<V>(model.extension());
00271   }
00272 
00273 # endif // ! MLN_INCLUDE_ONLY
00274 
00275 } // end of namespace mln
00276 
00277 
00278 #endif // ! MLN_CORE_IMAGE_DMORPH_EXTENSION_VAL_HH

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