Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
graph_window_piter.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 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_IMAGE_GRAPH_WINDOW_PITER_HH
28 # define MLN_CORE_IMAGE_GRAPH_WINDOW_PITER_HH
29 
36 
37 # include <mln/core/concept/site_set.hh>
38 # include <mln/core/concept/window.hh>
39 # include <mln/core/internal/site_relative_iterator_base.hh>
40 
41 
42 namespace mln
43 {
44 
45  // Forward declaration.
46  template <typename S, typename I> class p_graph_piter;
47 
48 
49  namespace internal
50  {
51 
54  template <typename C, typename P, typename E>
55  struct impl_selector
56  {
57  void do_start_()
58  {
59  internal::force_exact<E>(*this).iter_.start();
60  // Nothing else to do. A pointer to the window element's site
61  // set has been already stored in the constructor.
62  }
63 
64  };
65 
68  template <typename C, typename E>
69  struct impl_selector<C,C,E>
70  {
71  void do_start_()
72  {
73  E& this_ = internal::force_exact<E>(*this);
74  this_.iter_.start();
75 
76  // We need to store a pointer on the site set of the window
77  // center.
78  // We cannot do this in the constructor since the center may
79  // not be initialized.
80  // Here we suppose that if we start this iterator, the
81  // potential iterator used as center has been started just
82  // before calling this method.
83  //
84  this_.change_target_site_set(this_.center().site_set());
85  }
86 
87  };
88 
89  } // end of namespace mln::internal
90 
91 
97  //
98  template <typename S, typename W, typename I>
100  : public internal::site_relative_iterator_base< W,
101  graph_window_piter<S,W,I>,
102  typename W::center_t >,
103  public internal::impl_selector<typename W::center_t, mln_psite(W),
104  graph_window_piter<S,W,I> >
105  {
107  typedef
108  internal::site_relative_iterator_base<W,self_,mln_psite(S)> super_;
109  typedef
110  internal::impl_selector<typename W::center_t, mln_psite(W),
111  graph_window_piter<S,W,I> > super_impl_;
112 
113 
114  public:
118  typedef mln_result(S::fun_t) P;
120  typedef typename W::center_t center_t;
122  typedef typename W::graph_element graph_element;
124 
128 
129 
130  // ---------------------------------------------------------
131  // FIXME: probably ugly to provide constructors usable for
132  // specific cases only...
133  // ---------------------------------------------------------
134 
140  //
141  template <typename Pref>
142  graph_window_piter(const Window<W>& win,
143  const Pref& p_ref);
144 
152  //
153  template <typename Pref>
154  graph_window_piter(const Window<W>& win,
155  const Site_Set<S>& target_site_set,
156  const Pref& p_ref);
158 
161  template <typename Pref>
162  void init_(const Window<W>& win, const Pref& p_ref);
163 
164  template <typename Pref>
165  void init_(const Window<W>& win,
166  const Site_Set<S>& target_site_set,
167  const Pref& p_ref);
169 
173  bool is_valid_() const;
175  void invalidate_();
176 
178  void do_next_();
179 
181  template <typename Pref>
182  void center_at_(const Pref& c);
183 
185  template <typename S2, typename I2>
186  void center_at_(const p_graph_piter<S2, I2>& c);
187 
189  const graph_element& element() const;
190 
192  mln_psite(W) compute_p_() const;
193 
198  unsigned id() const;
200 
204  void change_target_site_set(const S& s);
208  const S& target_site_set() const;
209 
210  using super_impl_::do_start_;
211 
212  friend struct internal::impl_selector<typename W::center_t, mln_psite(W),
213  graph_window_piter<S,W,I> > ;
214 
215  private:
216  I iter_;
217  const S* s_;
218  };
219 
220 
221 # ifndef MLN_INCLUDE_ONLY
222 
223 
224  template <typename S, typename W, typename I>
225  inline
227  : s_(0)
228  {
229  }
230 
231 
232  template <typename S, typename W, typename I>
233  template <typename Pref>
234  inline
236  const Pref& p_ref)
237  : s_(0)
238  {
239  // Center and neighbor sites have the same type and belong to
240  // the same site set.
241  mlc_is(center_t, mln_psite(W))::check();
242  init_(win, p_ref);
243  }
244 
245 
246  template <typename S, typename W, typename I>
247  template <typename Pref>
248  inline
250  const Site_Set<S>& target_site_set,
251  const Pref& p_ref)
252  : s_(0)
253  {
254  // Center and neighbors sites do not have the same type and do
255  // not belong to the same site set.
256  mlc_is_not(center_t, mln_psite(W))::check();
257  init_(win, target_site_set, p_ref);
258  }
259 
260  template <typename S, typename W, typename I>
261  template <typename Pref>
262  inline
263  void
265  const Pref& p_ref)
266  {
267  this->center_at(p_ref);
268  this->change_target(exact(win));
269 
270  mln_postcondition(!this->is_valid());
271  }
272 
273  template <typename S, typename W, typename I>
274  template <typename Pref>
275  inline
276  void
277  graph_window_piter<S,W,I>::init_(const Window<W>& win,
278  const Site_Set<S>& target_site_set,
279  const Pref& p_ref)
280  {
281  this->center_at(p_ref);
282  this->change_target(exact(win));
283  change_target_site_set(exact(target_site_set));
284  mln_postcondition(!this->is_valid());
285  }
286 
287 
288  template <typename S, typename W, typename I>
289  inline
290  bool
291  graph_window_piter<S,W,I>::is_valid_() const
292  {
293  return s_ != 0 && s_->is_valid() && iter_.is_valid();
294  }
295 
296  template <typename S, typename W, typename I>
297  inline
298  void
299  graph_window_piter<S,W,I>::invalidate_()
300  {
301  iter_.invalidate();
302  }
303 
304  template <typename S, typename W, typename I>
305  inline
306  void
307  graph_window_piter<S,W,I>::do_next_()
308  {
309  iter_.next();
310  }
311 
312  template <typename S, typename W, typename I>
313  template <typename Pref>
314  inline
315  void
316  graph_window_piter<S, W, I>::center_at_(const Pref& c)
317  {
318  iter_.center_at(c.p_hook_());
319  }
320 
321  template <typename S, typename W, typename I>
322  template <typename S2, typename I2>
323  inline
324  void
325  graph_window_piter<S, W, I>::center_at_(const p_graph_piter<S2, I2>& c)
326  {
327  iter_.center_at(c.hook_elt_());
328  }
329 
330  template <typename S, typename W, typename I>
331  inline
332  mln_psite(W)
333  graph_window_piter<S,W,I>::compute_p_() const
334  {
335  return mln_psite(S)(target_site_set(), iter_.id());
336  }
337 
338  template <typename S, typename W, typename I>
339  inline
340  const typename graph_window_piter<S,W,I>::graph_element&
342  {
343  return iter_;
344  }
345 
346  template <typename S, typename W, typename I>
347  inline
348  unsigned
350  {
351  return iter_.id();
352  }
353 
354  template <typename S, typename W, typename I>
355  inline
356  void
358  {
359  s_ = & s;
360  mln_assertion(s_ != 0);
361  }
362 
363  template <typename S, typename W, typename I>
364  inline
365  const S&
367  {
368  mln_precondition(s_ != 0);
369  mln_precondition(s_->is_valid());
370  return *s_;
371  }
372 
373 
374 # endif // ! MLN_INCLUDE_ONLY
375 
376 } // end of namespace mln
377 
378 #endif // ! MLN_CORE_IMAGE_GRAPH_WINDOW_PITER_HH