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

safe.hh

00001 // Copyright (C) 2007, 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_IMORPH_SAFE_HH
00027 # define MLN_CORE_IMAGE_IMORPH_SAFE_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 safe_image;
00045 
00046 
00047   namespace internal
00048   {
00049 
00051     template <typename I>
00052     struct data< safe_image<I> >
00053     {
00054       data(I& ima, const mln_value(I)& default_value);
00055 
00056       I ima_;
00057       mln_value(I) default_value_;
00058     };
00059 
00060   } // end of namespace mln::internal
00061 
00062 
00063   namespace trait
00064   {
00065 
00066     template <typename I>
00067     struct image_< safe_image<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     };
00073 
00074   } // end of namespace mln::trait
00075 
00076 
00077 
00081   //
00082   template <typename I>
00083   class safe_image : public internal::image_identity< I, mln_domain(I), safe_image<I> >
00084   {
00085   public:
00086 
00088     typedef safe_image< tag::image_<I> > skeleton;
00089 
00090     safe_image();
00091     safe_image(I& ima);
00092     safe_image(I& ima, const mln_value(I)& default_value);
00093 
00094     // Initialize an empty image.
00095     void init_(I& ima, const mln_value(I)& default_value);
00096 
00097     mln_rvalue(I) operator()(const mln_psite(I)& p) const;
00098 
00099     mln_morpher_lvalue(I) operator()(const mln_psite(I)& p);
00100 
00102     operator safe_image<const I>() const;
00103   };
00104 
00105 
00106 
00107   template <typename I>
00108   safe_image<I> safe(Image<I>& ima,
00109                      mln_value(I) default_value = mln_value(I)());
00110 
00111   template <typename I>
00112   safe_image<const I> safe(const Image<I>& ima,
00113                            mln_value(I) default_value = mln_value(I)());
00114 
00115 
00116 
00117 # ifndef MLN_INCLUDE_ONLY
00118 
00119   // internal::data< safe_image<I,S> >
00120 
00121   namespace internal
00122   {
00123 
00124     template <typename I>
00125     inline
00126     data< safe_image<I> >::data(I& ima, const mln_value(I)& default_value)
00127       : ima_(ima),
00128         default_value_(default_value)
00129     {
00130     }
00131 
00132   } // end of namespace mln::internal
00133 
00134   // safe_image<I>
00135 
00136   template <typename I>
00137   inline
00138   safe_image<I>::safe_image()
00139   {
00140   }
00141 
00142   template <typename I>
00143   inline
00144   safe_image<I>::safe_image(I& ima, const mln_value(I)& default_value)
00145   {
00146     mln_precondition(ima.is_valid());
00147     init_(ima, default_value);
00148   }
00149 
00150   template <typename I>
00151   inline
00152   safe_image<I>::safe_image(I& ima)
00153   {
00154     mln_precondition(ima.is_valid());
00155     init_(ima, mln_value(I)());
00156   }
00157 
00158   template <typename I>
00159   inline
00160   void
00161   safe_image<I>::init_(I& ima, const mln_value(I)& default_value)
00162   {
00163     mln_precondition(! this->is_valid());
00164     mln_precondition(ima.is_valid());
00165     this->data_ = new internal::data< safe_image<I> >(ima, default_value);
00166   }
00167 
00168   template <typename I>
00169   inline
00170   mln_rvalue(I)
00171   safe_image<I>::operator()(const mln_psite(I)& p) const
00172   {
00173     mln_precondition(this->is_valid());
00174     if (! this->has(p))
00175       return this->data_->default_value_;
00176     return this->data_->ima_(p);
00177   }
00178 
00179   template <typename I>
00180   inline
00181   mln_morpher_lvalue(I)
00182   safe_image<I>::operator()(const mln_psite(I)& p)
00183   {
00184     mln_precondition(this->is_valid());
00185     static mln_value(I) forget_it_;
00186     if (this->has(p))
00187       return this->data_->ima_(p);
00188     else
00189       // A copy of data_->default_value_ is returned.
00190       return forget_it_ = this->data_->default_value_;
00191   }
00192 
00193   template <typename I>
00194   inline
00195   safe_image<I>::operator safe_image<const I>() const
00196   {
00197     safe_image<const I> tmp(this->data_->ima_, this->data_->default_value_);
00198     return tmp;
00199   }
00200 
00201   // safe
00202 
00203   template <typename I>
00204   inline
00205   safe_image<I> safe(Image<I>& ima,
00206                      mln_value(I) default_value)
00207   {
00208     safe_image<I> tmp(exact(ima), default_value);
00209     return tmp;
00210   }
00211 
00212   template <typename I>
00213   inline
00214   safe_image<const I> safe(const Image<I>& ima,
00215                            mln_value(I) default_value)
00216   {
00217     safe_image<const I> tmp(exact(ima), default_value);
00218     return tmp;
00219   }
00220 
00221 # endif // ! MLN_INCLUDE_ONLY
00222 
00223 } // end of namespace mln
00224 
00225 
00226 #endif // ! MLN_CORE_IMAGE_IMORPH_SAFE_HH

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