Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
image_morpher.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_CORE_INTERNAL_IMAGE_MORPHER_HH
27 # define MLN_CORE_INTERNAL_IMAGE_MORPHER_HH
28 
34 
35 # include <mln/core/internal/image_base.hh>
36 # include <mln/metal/const.hh>
37 # include <mln/metal/is_const.hh>
38 # include <mln/metal/is_not_const.hh>
39 
40 
41 namespace mln
42 {
43 
44  namespace internal
45  {
46 
50  //
51  template <typename I, typename T, typename S, typename E>
52  class image_morpher : public image_base<T, S, E>
53  {
54  public:
55 
57  typedef I delegatee;
58 
60  mlc_const(I)* delegatee_() const;
61 
63  I* delegatee_();
64 
65 
66 
68  typedef I unmorph;
69 
71  I& unmorph_();
72 
74  mlc_const(I)& unmorph_() const;
75 
76 
77  /* \brief Test if this image has been initialized; default impl.
78  *
79  * This default impl is stronger than the one inherited from
80  * image_base because it also tests that the morphed image is
81  * also initialized.
82  */
83  bool is_valid() const;
84 
85 
88  E& rw();
89 
90 
91  protected:
92  image_morpher();
93  };
94 
95  } // end of namespace mln::internal
96 
97 
98 
99  namespace impl
100  {
101 
102  // Default is delegation for morphers.
103 
104 // template <typename Subject, typename T,
105 // typename I, typename S, typename E>
106 // void init_(Subject s, T& target,
107 // const internal::image_morpher<I,S,E>& model);
108 
109 // FIXME: Lines above have been inactivated because they are either
110 // prioritary or ambiguous.
111 
112  template <typename Subject, typename T,
113  typename J>
114  void init_(Subject s, T& target, const Image<J>& model);
115 
116  } // end of namespace mln::impl
117 
118 
119 
120 # ifndef MLN_INCLUDE_ONLY
121 
122  namespace internal
123  {
124 
125  template <typename I, typename T, typename S, typename E>
126  inline
127  image_morpher<I, T, S, E>::image_morpher()
128  {
129  }
130 
131  template <typename I, typename T, typename S, typename E>
132  inline
133  mlc_const(I)*
134  image_morpher<I, T, S, E>::delegatee_() const
135  {
136  return this->data_ == 0 ? 0 : & this->data_->ima_;
137  }
138 
139  template <typename I, typename T, typename S, typename E>
140  inline
141  I*
142  image_morpher<I, T, S, E>::delegatee_()
143  {
144  return this->data_ == 0 ? 0 : & this->data_->ima_;
145  }
146 
147  template <typename I, typename T, typename S, typename E>
148  inline
149  I&
150  image_morpher<I, T, S, E>::unmorph_()
151  {
152  I* ptr = delegatee_();
153  mln_assertion(ptr != 0);
154  return *ptr;
155  }
156 
157  template <typename I, typename T, typename S, typename E>
158  inline
159  mlc_const(I)&
160  image_morpher<I, T, S, E>::unmorph_() const
161  {
162  mlc_const(I)* ptr = delegatee_();
163  mln_assertion(ptr != 0);
164  return *ptr;
165  }
166 
167  template <typename I, typename T, typename S, typename E>
168  inline
169  bool
170  image_morpher<I, T, S, E>::is_valid() const
171  {
172  return
173  this->data_ != 0 &&
174  this->delegatee_() != 0 &&
175  this->delegatee_()->is_valid();
176  }
177 
178  template <typename I, typename T, typename S, typename E>
179  inline
180  E&
181  image_morpher<I, T, S, E>::rw()
182  {
183  mlc_is_not_const(I)::check();
184  mlc_equal(mln_trait_image_value_io(I),
185  mln::trait::image::value_io::read_write)::check();
186  // FIXME Nicolas: pw_io == read_write OR vw_io == read_write...
187  return exact(*this);
188  }
189 
190  } // end of namespace mln::internal
191 
192 
193 
194 // template <typename Subject, typename T,
195 // typename I, typename S, typename E>
196 // void init_(Subject s, T& target,
197 // const internal::image_morpher<I,S,E>& model)
198 // {
199 // std::cout << "deleg... ";
200 // // FIXME: Precondition.
201 // init_(s, target, * model.delegatee_());
202 // }
203 
204  template <typename Subject, typename T,
205  typename J>
206  inline
207  void init_(Subject s, T& target, const Image<J>& model_)
208  {
209  mlc_is(mln_trait_image_category(J),
210  trait::image::category::morpher)::check();
211  const J& model = exact(model_);
212  init_(s, target, * model.delegatee_());
213  }
214 
215 # endif // ! MLN_INCLUDE_ONLY
216 
217 } // end of namespace mln
218 
219 
220 #endif // ! MLN_CORE_INTERNAL_IMAGE_MORPHER_HH