$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
graph_psite_base.hh
1 // Copyright (C) 2008, 2009, 2012, 2013 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_INTERNAL_GRAPH_PSITE_BASE_HH
28 # define MLN_CORE_INTERNAL_GRAPH_PSITE_BASE_HH
29 
36 
37 # include <mln/core/internal/pseudo_site_base.hh>
38 
39 
40 
41 namespace mln
42 {
43 
44  namespace internal
45  {
46 
47  template <typename S, typename E>
48  class graph_psite_base : public internal::pseudo_site_base_< const mln_site(S)&,
49  E >
50  {
51  public:
52 
53  // This associated type is important to know that this particular
54  // pseudo site knows the site set it refers to.
55  typedef S target;
56 
57  // As a Proxy:
58  const mln_site(S)& subj_();
59 
60  typedef typename S::graph_element::id_t id_t;
61 
65  void change_target(const S& new_target);
68  void update_id(unsigned elt_id);
70 
74  const S* target_() const; // Hook to the target.
75 
77  const S& site_set() const;
78 
80  const typename S::graph_t& graph() const;
81 
83  id_t id() const;
84 
86 
88  bool is_valid() const;
90  void invalidate();
91 
93  operator unsigned () const;
94 
96  operator typename S::graph_element::id_t () const;
97 
99  operator const typename S::graph_element&() const;
100 
102  const typename S::graph_element& element() const;
103 
106  const typename S::graph_element& p_hook_() const;
108 
109  protected:
110 
112 
117  graph_psite_base(const S& s);
121  graph_psite_base(const S& , unsigned id);
123 
124  const S* s_;
125  mln_site(S) site_;
126  typename S::graph_element elt_;
127  };
128 
129 
130  /* FIXME: Shouldn't those comparisons be part of a much general
131  mechanism? */
132 
139  template <typename S, typename E>
140  bool
141  operator==(const graph_psite_base<S,E>& lhs, const graph_psite_base<S,E>& rhs);
142 
147  template <typename S, typename E>
148  bool
149  operator!=(const graph_psite_base<S,E>& lhs, const graph_psite_base<S,E>& rhs);
150 
157  template <typename S, typename E>
158  bool
159  operator< (const graph_psite_base<S,E>& lhs, const graph_psite_base<S,E>& rhs);
161 
162 
164 
167  template <typename S, typename P, typename E>
168  struct subject_impl< const graph_psite_base<S,P>&, E >
169  {
170  const S* target_() const;
171  const S& site_set() const;
172  const typename S::graph_t& graph() const;
173  unsigned id() const;
174  bool is_valid() const;
175  // operator unsigned() const;
176  // operator const typename S::graph_element&() const;
177  const typename S::graph_element& element() const;
178  const typename S::graph_element& p_hook_() const;
179 
180  private:
181  const E& exact_() const;
182  };
183 
184  template <typename S, typename P, typename E>
185  struct subject_impl< graph_psite_base<S,P>&, E > :
187  {
188  void change_target(const S& new_target);
189  void update_id(unsigned elt_id);
190  void invalidate();
191 
192  private:
193  E& exact_();
194  };
196 
197 
198 
199 # ifndef MLN_INCLUDE_ONLY
200 
201 
202  template <typename S, typename E>
203  inline
205  : s_(0)
206  {
207  }
208 
209  template <typename S, typename E>
210  inline
211  graph_psite_base<S,E>::graph_psite_base(const S& s)
212  {
213  change_target(s);
214  }
215 
216  template <typename S, typename E>
217  inline
218  graph_psite_base<S,E>::graph_psite_base(const S& s, unsigned id)
219  {
220  change_target(s);
221  update_id(id);
222  }
223 
224  // The lines below are dedicated/local to this file.
225  template <typename E, typename S, typename G>
226  inline
227  void local_change_graph(E& elt_, S& site_, const G& g)
228  {
229  (void) site_;
230  elt_.change_graph(g);
231  }
232  template <typename E, typename G>
233  inline
234  void local_change_graph(E& elt_, E& site_, const G& g)
235  {
236  elt_.change_graph(g);
237  site_.change_graph(g);
238  }
239  // End of local stuff.
240 
241  template <typename S, typename E>
242  inline
243  void
244  graph_psite_base<S,E>::change_target(const S& new_target)
245  {
246  s_ = & new_target;
247  local_change_graph(elt_, site_, new_target.graph());
248  }
249 
250  template <typename S, typename E>
251  inline
252  void
253  graph_psite_base<S,E>::update_id(unsigned id)
254  {
255  mln_precondition(s_ != 0);
256  elt_.update_id(id);
257  site_ = s_->function()(elt_.id());
258  }
259 
260  template <typename S, typename E>
261  inline
262  const S*
263  graph_psite_base<S,E>::target_() const
264  {
265  return s_;
266  }
267 
268  template <typename S, typename E>
269  inline
270  const S&
272  {
273  mln_precondition(s_ != 0);
274  return *s_;
275  }
276 
277  template <typename S, typename E>
278  inline
279  const typename S::graph_t&
280  graph_psite_base<S,E>::graph() const
281  {
282  mln_precondition(s_ != 0);
283  return s_->graph();
284  }
285 
286  template <typename S, typename E>
287  inline
288  typename graph_psite_base<S,E>::id_t
289  graph_psite_base<S,E>::id() const
290  {
291  return elt_.id();
292  }
293 
294  template <typename S, typename E>
295  inline
296  bool
297  graph_psite_base<S,E>::is_valid() const
298  {
299  return s_ != 0 && s_->is_valid() && elt_.is_valid();
300  }
301 
302  template <typename S, typename E>
303  inline
304  void
305  graph_psite_base<S,E>::invalidate()
306  {
307  s_ = 0;
308  elt_.invalidate();
309  }
310 
311  template <typename S, typename E>
312  inline
313  const mln_site(S)&
314  graph_psite_base<S,E>::subj_()
315  {
316  /*FIXME: we may like to enable the following precondition, however, we
317  ** can't do that since neighb_*_niters update the psite target after having
318  ** taken the adress of the subject.
319  */
320  // mln_precondition(is_valid());
321  return site_;
322  }
323 
324  template <typename S, typename E>
325  inline
326  graph_psite_base<S,E>::operator unsigned () const
327  {
328  mln_precondition(is_valid());
329  return elt_.id();
330  }
331 
332  template <typename S, typename E>
333  inline
334  graph_psite_base<S,E>::operator typename S::graph_element::id_t () const
335  {
336  mln_precondition(is_valid());
337  return elt_.id();
338  }
339 
340  template <typename S, typename E>
341  inline
342  graph_psite_base<S,E>::operator const typename S::graph_element&() const
343  {
344  //mln_precondition(is_valid());
345  return elt_;
346  }
347 
348  template <typename S, typename E>
349  inline
350  const typename S::graph_element&
351  graph_psite_base<S,E>::element() const
352  {
353  /*FIXME: we may like to enable the following precondition, however, we
354  ** can't do that since neighb_*_niters update the psite target after having
355  ** taken the adress of the subject.
356  */
357  // mln_precondition(is_valid());
358  return elt_;
359  }
360 
361  template <typename S, typename E>
362  inline
363  const typename S::graph_element&
364  graph_psite_base<S,E>::p_hook_() const
365  {
366  return elt_;
367  }
368 
369 
370  template <typename S, typename P, typename E>
371  inline
372  const E&
373  subject_impl< const graph_psite_base<S,P>&, E >::exact_() const
374  {
375  return internal::force_exact<const E>(*this);
376  }
377 
378  template <typename S, typename P, typename E>
379  inline
380  const S*
381  subject_impl< const graph_psite_base<S,P>&, E >::target_() const
382  {
383  return exact_().get_subject().target();
384  }
385 
386  template <typename S, typename P, typename E>
387  inline
388  const S&
390  {
391  return exact_().get_subject().site_set();
392  }
393 
394 
395  template <typename S, typename P, typename E>
396  inline
397  const typename S::graph_t&
398  subject_impl< const graph_psite_base<S,P>&, E >::graph() const
399  {
400  return exact_().get_subject().graph();
401  }
402 
403  template <typename S, typename P, typename E>
404  inline
405  unsigned
407  {
408  return exact_().get_subject().id();
409  };
410 
411  template <typename S, typename P, typename E>
412  inline
413  bool
414  subject_impl< const graph_psite_base<S,P>&, E >::is_valid() const
415  {
416  return exact_().get_subject().is_valid();
417  }
418 
419  template <typename S, typename P, typename E>
420  inline
421  const typename S::graph_element&
422  subject_impl< const graph_psite_base<S,P>&, E >::element() const
423  {
424  return exact_().get_subject().element();
425  }
426 
427  template <typename S, typename P, typename E>
428  inline
429  const typename S::graph_element&
430  subject_impl< const graph_psite_base<S,P>&, E >::p_hook_() const
431  {
432  return exact_().get_subject().p_hook_();
433  }
434 
435 
436  template <typename S, typename P, typename E>
437  inline
438  E&
440  {
441  return internal::force_exact<E>(*this);
442  }
443 
444  template <typename S, typename P, typename E>
445  inline
446  void
447  subject_impl< graph_psite_base<S,P>&, E >::change_target(const S& new_target)
448  {
449  exact_().get_subject().change_target(new_target);
450  }
451 
452  template <typename S, typename P, typename E>
453  inline
454  void
455  subject_impl< graph_psite_base<S,P>&, E >::update_id(unsigned id)
456  {
457  exact_().get_subject().update_id(id);
458  };
459 
460  template <typename S, typename P, typename E>
461  inline
462  void
463  subject_impl< graph_psite_base<S,P>&, E >::invalidate()
464  {
465  exact_().get_subject().invalidate();
466  }
467 
468 # endif // ! MLN_INCLUDE_ONLY
469 
470  } // end of namespace internal
471 
472 } // end of namespace mln
473 
474 
475 #endif // ! MLN_CORE_INTERNAL_GRAPH_PSITE_BASE_HH