Milena (Olena)
User documentation 2.0a Id
|
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_DECORATED_IMAGE_HH 00027 # define MLN_CORE_IMAGE_IMORPH_DECORATED_IMAGE_HH 00028 00029 # include <mln/core/internal/image_identity.hh> 00030 00031 # include <mln/value/proxy.hh> 00032 00036 00037 00038 namespace mln 00039 { 00040 00041 // Forward declaration. 00042 template <typename I, typename D> struct decorated_image; 00043 00044 00045 namespace internal 00046 { 00047 00048 template <typename I, typename E> 00049 struct decorated_image_impl_ 00050 { 00051 typedef mln::value::proxy<E> lvalue; 00053 void write_(const mln_psite(I)& p, const mln_value(I)& v); 00054 }; 00055 00056 template <typename I, typename E> 00057 struct decorated_image_impl_< const I, E > 00058 { 00059 typedef mln::value::proxy<const E> lvalue; 00060 }; 00061 00063 template <typename I, typename D> 00064 struct data< decorated_image<I,D> > 00065 { 00066 data(I& ima, const D& deco); 00067 00068 I ima_; 00069 D deco_; 00070 }; 00071 00072 } // end of namespace mln::internal 00073 00074 00075 00079 // 00080 template <typename I, typename D> 00081 struct decorated_image : 00082 public internal::decorated_image_impl_< I, decorated_image<I,D> >, 00083 public internal::image_identity< I, mln_domain(I), decorated_image<I,D> > 00084 { 00085 public: 00086 typedef decorated_image<I, D> self_; 00087 typedef internal::decorated_image_impl_< I, self_ > impl_; 00088 00090 typedef mln_psite(I) psite; 00091 00093 typedef mln_rvalue(I) rvalue; 00095 typedef typename impl_::lvalue lvalue; 00096 00098 decorated_image(); 00099 decorated_image(I& ima, const D& deco); 00100 00102 void init_(I& ima, const D& deco); 00103 00105 ~decorated_image(); 00106 00108 typedef decorated_image< tag::image_<I>, tag::data_<D> > skeleton; 00109 00111 rvalue operator()(const psite& p) const; 00112 00114 lvalue operator()(const psite& p); 00115 00117 rvalue read_(const mln_psite(I)& p) const; 00118 00120 operator decorated_image<const I, D>() const; 00121 00123 const D& decoration() const; 00124 00126 D& decoration(); 00127 }; 00128 00129 00130 00131 template <typename I, typename D> 00132 decorated_image<I,D> decorate(Image<I>& ima, 00133 const D& decoration); 00134 00135 template <typename I, typename D> 00136 decorated_image<const I, D> decorate(const Image<I>& ima, 00137 const D& decoration); 00138 00139 00140 00141 # ifndef MLN_INCLUDE_ONLY 00142 00143 namespace internal 00144 { 00145 00147 template <typename I, typename D> 00148 inline 00149 data< decorated_image<I,D> >::data(I& ima, const D& deco) 00150 : ima_(ima), 00151 deco_(deco) 00152 { 00153 } 00154 00155 } // end of namespace mln::internal 00156 00157 // decorated_image<I,D> 00158 00159 template <typename I, typename D> 00160 inline 00161 decorated_image<I,D>::decorated_image() 00162 { 00163 } 00164 00165 template <typename I, typename D> 00166 inline 00167 decorated_image<I,D>::decorated_image(I& ima, const D& deco) 00168 { 00169 mln_precondition(exact(ima).is_valid()); 00170 this->data_ = new internal::data< decorated_image<I,D> >(ima, deco); 00171 } 00172 00173 template <typename I, typename D> 00174 inline 00175 void 00176 decorated_image<I,D>::init_(I& ima, const D& deco) 00177 { 00178 mln_precondition(exact(ima).is_valid()); 00179 this->data_ = new internal::data<decorated_image<I,D> >(ima, deco); 00180 } 00181 00182 template <typename I, typename D> 00183 inline 00184 decorated_image<I,D>::~decorated_image() 00185 { 00186 void (D::*mr)(const I&, const mln_psite(I)&) const = & D::reading; 00187 mr = 0; 00188 typedef mlc_unconst(I) I_; 00189 void (D::*mw)(I_&, const mln_psite(I_)&, const mln_value(I_)&) = 00190 & D::writing; 00191 mw = 0; 00192 } 00193 00194 template <typename I, typename D> 00195 inline 00196 typename decorated_image<I,D>::rvalue 00197 decorated_image<I,D>::operator()(const psite& p) const 00198 { 00199 mln_precondition(this->delegatee_() != 0); 00200 return read_(p); 00201 } 00202 00203 template <typename I, typename D> 00204 inline 00205 typename decorated_image<I,D>::lvalue 00206 decorated_image<I,D>::operator()(const psite& p) 00207 { 00208 mln_precondition(this->delegatee_() != 0); 00209 // Return a proxy. 00210 return lvalue(*this, p); 00211 } 00212 00213 template <typename I, typename D> 00214 inline 00215 mln_rvalue(I) 00216 decorated_image<I,D>::read_(const mln_psite(I)& p) const 00217 { 00218 this->data_->deco_.reading(this->data_->ima_, p); 00219 return this->data_->ima_(p); 00220 } 00221 00222 namespace internal 00223 { 00224 template <typename I, typename E> 00225 inline 00226 void 00227 decorated_image_impl_<I,E>::write_(const mln_psite(I)& p, 00228 const mln_value(I)& v) 00229 { 00230 E& ima = internal::force_exact<E>(*this); 00231 ima.decoration().writing(*ima.delegatee_(), p, v); 00232 (*ima.delegatee_())(p) = v; 00233 } 00234 00235 } // end of namespace mln::internal 00236 00237 template <typename I, typename D> 00238 inline 00239 decorated_image<I,D>::operator decorated_image<const I, D>() const 00240 { 00241 decorated_image<const I, D> tmp(this->data_->ima_, this->data_->deco_); 00242 return tmp; 00243 } 00244 00245 00246 template <typename I, typename D> 00247 inline 00248 const D& 00249 decorated_image<I,D>::decoration() const 00250 { 00251 return this->data_->deco_; 00252 } 00253 00254 template <typename I, typename D> 00255 inline 00256 D& 00257 decorated_image<I,D>::decoration() 00258 { 00259 return this->data_->deco_; 00260 } 00261 00262 // decorate 00263 00264 template <typename I, typename D> 00265 inline 00266 decorated_image<I, D> decorate(Image<I>& ima, 00267 const D& decoration) 00268 { 00269 decorated_image<I, D> tmp(exact(ima), decoration); 00270 return tmp; 00271 } 00272 00273 template <typename I, typename D> 00274 inline 00275 decorated_image<const I, D> decorate(const Image<I>& ima, 00276 const D& decoration) 00277 { 00278 decorated_image<const I, D> tmp(exact(ima), decoration); 00279 return tmp; 00280 } 00281 00282 # endif // ! MLN_INCLUDE_ONLY 00283 00284 } // end of namespace mln 00285 00286 00287 #endif // ! MLN_CORE_IMAGE_IMORPH_DECORATED_IMAGE_HH