Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
core/concept/image.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_CORE_CONCEPT_IMAGE_HH
28 # define MLN_CORE_CONCEPT_IMAGE_HH
29 
33 
34 # include <mln/core/concept/site_set.hh>
35 # include <mln/core/concept/mesh.hh>
36 
37 # include <mln/core/trait/all.hh> // FIXME: Move out of core!
38 # include <mln/core/macros.hh>
39 # include <mln/core/site_set/box.hh>
40 
41 # include <mln/trait/concrete.hh> // FIXME: Should be in all.hh!
42 # include <mln/trait/images.hh>
43 
44 # include <mln/metal/abort.hh>
45 # include <mln/metal/is_a.hh>
46 # include <mln/metal/equal.hh>
47 
48 # include <mln/tag/init.hh>
49 
50 
51 namespace mln
52 {
53 
54 
55  // Forward declarations.
56  template <typename E> struct Image;
57  template <typename E> struct Literal;
58 
59 
60  // Image category flag type.
61  template <>
62  struct Image<void>
63  {
64  typedef Object<void> super;
65  };
66 
67 
72  template <typename E>
73  struct Image : public Object<E>
74  {
75  typedef Image<void> category;
76 
77  template <typename J>
78  Image<E>& operator=(const J& rhs);
79 
80  /*
81  // provided by internal::image_base:
82 
83  typedef domain_t;
84  typedef site;
85  typedef psite;
86 
87  typedef piter;
88  typedef fwd_piter;
89  typedef bkd_piter;
90 
91  bool has(const psite& p) const;
92  unsigned nsites() const; // If relevant.
93 
94  bool is_valid() const;
95 
96  typedef t_eligible_values_set;
97  const t_eligible_values_set& values_eligible() const;
98 
99  typedef t_values_space;
100  const t_values_space& values_space() const;
101 
102  typedef value;
103 
104  // to be provided in concrete image classes:
105 
106  typedef vset;
107  const vset& values() const;
108 
109  typedef rvalue;
110  typedef lvalue;
111  rvalue operator()(const psite& p) const;
112  lvalue operator()(const psite& p);
113 
114  const domain_t& domain() const;
115 
116  typedef skeleton;
117  */
118 
119  protected:
120  Image();
121  };
122 
123 
124  namespace convert
125  {
126 
127  namespace over_load
128  {
129 
130  template <typename V, unsigned S, typename I>
131  void
132  from_to_(const V (&values)[S], Image<I>& to);
133 
134  } // end of namespace mln::convert::over_load
135 
136  } // end of namespace mln::convert
137 
138 
139 
140 # ifndef MLN_INCLUDE_ONLY
141 
142 
143  namespace internal
144  {
145 
146  template <typename values_browsing_trait, typename E>
147  struct image_values_interface_check
148  {
149  static void run() { /* No Requirement */}
150  };
151 
152  template <typename E>
153  struct image_values_interface_check<
154  mln::trait::image::value_browsing::value_wise,
155  E>
156  {
157  static void run()
158  {
159  }
160  };
161 
162 
163  // check_init
164 
165  template < typename E,
166  typename A >
167  int check_init(void (E::*)(A))
168  {
169  return 0;
170  }
171 
172  template < typename E,
173  typename A1, typename A2 >
174  int check_init(void (E::*)(A1, A2))
175  {
176  return 0;
177  }
178 
179  template < typename E,
180  typename A1, typename A2, typename A3 >
181  int check_init(void (E::*)(A1, A2, A3))
182  {
183  return 0;
184  }
185 
186  } // end of namespace mln::internal
187 
188 
189  template <typename E>
190  template <typename J>
191  inline
192  Image<E>&
193  Image<E>::operator=(const J&)
194  {
199  mlc_abort(E)::check();
200  return *this;
201  }
202 
203 
204  template <typename E>
205  inline
206  Image<E>::Image()
207  {
208  // provided by internal::image_base:
209 
210  typedef mln_domain(E) domain_t;
211  typedef mln_site(E) site;
212  typedef mln_psite(E) psite;
213 
214  typedef mln_piter(E) piter;
215  typedef mln_fwd_piter(E) fwd_piter;
216  typedef mln_bkd_piter(E) bkd_piter;
217 
218  bool (E::*m1)(const psite& p) const = & E::has;
219  m1 = 0;
220 
221  // Only some image types feature the 'nsites' method.
222 // unsigned (E::*m2)() const = & E::nsites;
223 // m2 = 0;
224 
225  bool (E::*m3)() const = & E::is_valid;
226  m3 = 0;
227 
228  typedef typename E::t_eligible_values_set t_eligible_values_set;
229 
230  const t_eligible_values_set& (E::*m4)() const = & E::values_eligible;
231  m4 = 0;
232 
233  typedef typename E::t_values_space t_values_space;
234 
235  const t_values_space& (E::*m5)() const = & E::values_space;
236  m5 = 0;
237 
238  // to be provided in concrete image classes:
239 
240  typedef mln_value(E) value;
241  typedef mln_rvalue(E) rvalue;
242  typedef mln_lvalue(E) lvalue;
243 
244  mlc_is_not_a(value, Literal)::check();
245 
246  // FIXME Doc
247  //typedef mln_vset(E) vset;
248  //const vset& (E::*m5)() const = & E::values;
249  //m5 = 0;
250 
251  rvalue (E::*m6)(const psite& p) const = & E::operator();
252  m6 = 0;
253  lvalue (E::*m7)(const psite& p) = & E::operator();
254  m7 = 0;
255 
256  const domain_t& (E::*m8)() const = & E::domain;
257  m8 = 0;
258 
259  typedef typename E::skeleton skeleton;
260 
261  // Check E::init_ presence. Since its signature varies from an
262  // image type to another, that is the only thing we can ensure.
263  internal::check_init(& E::init_);
264 
266  internal::image_values_interface_check<mln_trait_image_value_browsing(E),
267  E>::run();
268  }
269 
270 
271  namespace convert
272  {
273 
274  namespace over_load
275  {
276 
277  template <typename V, unsigned S, typename I>
278  void
279  from_to_(const V (&values)[S], Image<I>& to_)
280  {
281  mlc_bool(S != 0)::check();
282  mlc_converts_to(V, mln_value(I))::check();
283  typedef mln_site(I) P;
284  enum { d = P::dim,
285  s = mlc_root(d, S)::value };
286  metal::bool_<(mlc_pow_int(s, d) == S)>::check();
287 
288  I& to = exact(to_);
289  mln_precondition(! to.is_valid());
290 
291  box<P> b(all_to(0), all_to(s - 1));
292  to.init_(b);
293  mln_fwd_piter(box<P>) p(b);
294  unsigned i = 0;
295  for_all(p)
296  to(p) = values[i++];
297  }
298 
299  } // end of namespace mln::convert::over_load
300 
301  } // end of namespace mln::convert
302 
303 # endif // ! MLN_INCLUDE_ONLY
304 
305 } // end of namespace mln
306 
307 
308 # include <mln/core/routine/initialize.hh>
309 
310 
311 #endif // ! MLN_CORE_CONCEPT_IMAGE_HH