$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
site_pair.hh
1 // Copyright (C) 2008, 2009, 2012 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_UTIL_SITE_PAIR_HH
28 # define MLN_UTIL_SITE_PAIR_HH
29 
30 # include <mln/core/concept/proxy.hh>
31 # include <mln/util/ord_pair.hh>
32 
36 
37 namespace mln
38 {
39 
40  namespace util
41  {
42 
43  /* FIXME: The class mln::internal::image_base requires its site
44  types to have a `coord' typedef, but util::ord_pair has none.
45  Hence this small workaround. Use ord_pair directly as soon as
46  image_base is refurbished. */
51  //
52  template <typename P>
53  class site_pair : public mln::Object< site_pair<P> >
54  {
55  public:
56  /* FIXME: We should not need to define this typedef
57  (see. mln::internal::image_base's site `coord' typedef). */
58  typedef mln_coord(P) coord;
59 
60  site_pair();
61  site_pair(const P& first, const P& second);
62 
64  const P& first() const;
66  const P& second() const;
67 
69  const util::ord_pair<P>& pair() const;
70 
71  private:
72  util::ord_pair<P> pair_;
73  };
74 
75 
76  /* FIXME: The proxy mechanism requires operator== and operator<= for
77  line_graph_psite to compile (note: line_graph_psite aggregates a
78  site_pair); why? It seems its static assertions do not check the
79  right guy(s). IMHO (Roland's), it should look for
80  line_graph_psite's operators, not site_pair's. */
81  template <typename P>
82  bool operator==(const site_pair<P>& lhs, const site_pair<P>& rhs);
83 
84  template <typename P>
85  bool operator< (const site_pair<P>& lhs, const site_pair<P>& rhs);
86 
87  template <typename P>
88  bool operator< (const site_pair<P>& lhs, const site_pair<P>& rhs);
89 
90  template <typename P>
91  std::ostream&
92  operator<<(std::ostream& ostr, const site_pair<P>& p);
93 
94  } // end of namespace mln::util
95 
96 
97  namespace internal
98  {
99 
101 
105  template <typename P, typename E>
106  struct subject_impl< const util::site_pair<P>, E >
107  {
108  const P& first() const;
109  const P& second() const;
110  const util::ord_pair<P>& pair() const;
111 
112  private:
113  const E& exact_() const;
114  };
115 
117 
118  } // end of namespace mln::internal
119 
120 
121 
122 # ifndef MLN_INCLUDE_ONLY
123 
124  namespace util
125  {
126 
127  /*---------------.
128  | Construction. |
129  `---------------*/
130 
131  template <typename P>
133  {
134  }
135 
136  template <typename P>
137  site_pair<P>::site_pair(const P& first, const P& second)
138  : pair_(first, second)
139  {
140  }
141 
142  template <typename P>
143  inline
144  const P&
145  site_pair<P>::first() const
146  {
147  return pair_.first();
148  }
149 
150  template <typename P>
151  inline
152  const P&
153  site_pair<P>::second() const
154  {
155  return pair_.second();
156  }
157 
158  template <typename P>
159  inline
160  const util::ord_pair<P>&
161  site_pair<P>::pair() const
162  {
163  return pair_;
164  }
165 
166  /*-------------.
167  | Comparison. |
168  `-------------*/
169 
170  template <typename P>
171  inline
172  bool
173  operator==(const site_pair<P>& lhs, const site_pair<P>& rhs)
174  {
175  return lhs.pair() == rhs.pair();
176  }
177 
178  template <typename P>
179  inline
180  bool
181  operator< (const site_pair<P>& lhs, const site_pair<P>& rhs)
182  {
183  return lhs.pair() < rhs.pair();
184  }
185 
186  template <typename P>
187  inline
188  bool
189  operator<=(const site_pair<P>& lhs, const site_pair<P>& rhs)
190  {
191  return lhs.pair() <= rhs.pair();
192  }
193 
194  template <typename P>
195  inline
196  std::ostream&
197  operator<<(std::ostream& ostr, const site_pair<P>& p)
198  {
199  ostr << "(" << p.first() << ", " << p.second() << ")";
200  return ostr;
201  }
202 
203  } // end of mln::util
204 
205  namespace internal
206  {
207 
208  template <typename P, typename E>
209  inline
210  const E&
211  subject_impl< const util::site_pair<P>, E >::exact_() const
212  {
213  return internal::force_exact<const E>(*this);
214  }
215 
216  template <typename P, typename E>
217  inline
218  const P&
219  subject_impl< const util::site_pair<P>, E >::first() const
220  {
221  return exact_().get_subject().first();
222  }
223 
224  template <typename P, typename E>
225  inline
226  const P&
227  subject_impl< const util::site_pair<P>, E >::second() const
228  {
229  return exact_().get_subject().second();
230  }
231 
232  } // end of namespace mln::internal
233 
234 # endif // ! MLN_INCLUDE_ONLY
235 
236 } // end of mln
237 
238 
239 #endif // ! MLN_UTIL_SITE_PAIR_HH