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