$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
label.hh
1 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 EPITA
2 // Research and Development 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_VALUE_LABEL_HH
28 # define MLN_VALUE_LABEL_HH
29 
33 
34 # include <mln/debug/format.hh>
35 # include <mln/metal/math/pow.hh>
36 # include <mln/trait/value_.hh>
37 # include <mln/value/concept/symbolic.hh>
38 # include <mln/value/internal/value_like.hh>
39 # include <mln/value/internal/convert.hh>
40 # include <mln/value/internal/encoding.hh>
41 
42 # include <mln/value/internal/make_generic_name.hh>
43 
44 namespace mln
45 {
46 
47  // Forward declarations.
48  namespace value {
49  template <unsigned n> struct label;
50  template <unsigned n> struct int_u;
51  }
52 
53  namespace literal {
54  struct zero_t;
55  }
56 
57 
58  namespace trait
59  {
60 
61  template <unsigned n>
62  struct value_< mln::value::label<n> >
63  {
64  private:
65  typedef mln::value::label<n> self_;
66 
67  public:
68 
69  enum {
70  dim = 1,
71  nbits = n,
72  card = mln_value_card_from_(n)
73  };
74 
75  typedef trait::value::nature::symbolic nature;
76  typedef trait::value::kind::label kind;
77  typedef mln_value_quant_from_(card) quant;
78 
79  static const self_ min() { return 0; }
80  static const self_ max() { return mlc_pow_int(2, n) - 1; }
81 
82  static const char* name()
83  {
84  static std::string
86  return s.c_str();
87  }
88 
89  typedef unsigned comp;
90  };
91 
92  } // end of namespace trait
93 
94  namespace value
95  {
96 
97 
105  template <unsigned n>
106  struct label
107  : public Symbolic< label<n> >,
108  public internal::value_like_< unsigned, // Equivalent.
109  typename internal::encoding_unsigned_<n>::ret, // Enc.
110  int, // Interoperation.
111  label<n> > // Exact.
112 
113  {
114  public:
117 
119  label();
120 
122  label(unsigned i);
123 
125  label(const literal::zero_t& v);
126 
128  operator unsigned() const;
129 
131  label<n>& operator=(unsigned i);
132 
135 
137  label<n>& operator++();
138 
140  label<n>& operator--();
141 
143  label<n> next() const;
144 
146  label<n> prev() const;
147 
148  };
149 
150 
151  // Safety.
152  template <> struct label<0>;
153  template <> struct label<1>;
154 
162  template <unsigned n>
163  std::ostream& operator<<(std::ostream& ostr, const label<n>& l);
164 
165 
167  template <unsigned n>
168  void from_to_(const value::int_u<n>& from, value::label<n>& to_);
169 
171  template <unsigned n>
172  void from_to_(const value::label<n>& from, value::int_u<n>& to_);
173 
174 
176  template <unsigned n, unsigned m>
177  void from_to_(const value::int_u<n>& from, value::label<m>& to_);
178 
180  template <unsigned n>
181  void from_to_(const value::label<n>& from, bool& to_);
182 
184  template <unsigned n>
185  void from_to_(const value::label<n>& from, unsigned& to_);
186 
187 
188  } // end of namespace mln::value
189 
190 
191 # ifndef MLN_INCLUDE_ONLY
192 
193 
194  namespace value
195  {
196 
197  template <unsigned n>
198  inline
200  {
201  }
202 
203  template <unsigned n>
204  inline
205  label<n>::label(unsigned i)
206  {
207  this->v_ = enc(i);
208  }
209 
210  template <unsigned n>
211  inline
212  label<n>::label(const literal::zero_t&)
213  {
214  this->v_ = 0;
215  }
216 
217  template <unsigned n>
218  inline
219  label<n>::operator unsigned() const
220  {
221  return this->to_enc();
222  }
223 
224  template <unsigned n>
225  inline
226  label<n>&
227  label<n>::operator=(unsigned i)
228  {
229  mln_precondition(i <= mln_max(enc));
230  this->v_ = enc(i);
231  return *this;
232  }
233 
234  template <unsigned n>
235  inline
236  label<n>&
237  label<n>::operator=(const literal::zero_t&)
238  {
239  this->v_ = 0;
240  return *this;
241  }
242 
243  template <unsigned n>
244  inline
245  label<n>&
247  {
248  mln_precondition(this->v_ < mln_max(enc));
249  ++this->v_;
250  return *this;
251  }
252 
253  template <unsigned n>
254  inline
255  label<n>&
257  {
258  mln_precondition(this->v_ != 0);
259  --this->v_;
260  return *this;
261  }
262 
263  template <unsigned n>
264  inline
265  label<n>
266  label<n>::next() const
267  {
268  return label<n>(this->v_ + 1);
269  }
270 
271  template <unsigned n>
272  inline
273  label<n>
274  label<n>::prev() const
275  {
276  return label<n>(this->v_ - 1);
277  }
278 
279  template <unsigned n>
280  inline
281  std::ostream& operator<<(std::ostream& ostr, const label<n>& i)
282  {
283  return ostr << debug::format(i.to_equiv());
284  }
285 
286  template <unsigned n>
287  inline
288  void
289  from_to_(const value::int_u<n>& from, value::label<n>& to_)
290  {
291  to_ = from;
292  }
293 
294  template <unsigned n>
295  void
296  from_to_(const value::label<n>& from, value::int_u<n>& to_)
297  {
298  to_ = from;
299  }
300 
301  template <unsigned n, unsigned m>
302  inline
303  void
304  from_to_(const value::int_u<n>& from, value::label<m>& to_)
305  {
306  enum { valid = n < m };
307  metal::bool_<valid>::check();
308  to_ = from;
309  }
310 
311  template <unsigned n>
312  inline
313  void
314  from_to_(const value::label<n>& from, bool& to_)
315  {
316  to_ = (from != 0u);
317  }
318 
319  template <unsigned n>
320  inline
321  void
322  from_to_(const value::label<n>& from, unsigned& to_)
323  {
324  to_ = from;
325  }
326 
327  } // end of namespace mln::value
328 
329 
330 # endif // ! MLN_INCLUDE_ONLY
331 
332 } // end of namespace mln
333 
334 
335 #endif // ! MLN_VALUE_LABEL_HH