$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
int_u.hxx
1 // Copyright (C) 2007, 2008, 2009, 2010, 2012, 2013 EPITA Research and
2 // 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_INT_U_HXX
28 # define MLN_VALUE_INT_U_HXX
29 
34 
35 # include <mln/value/int_u.hh>
36 
37 # include <mln/debug/format.hh>
38 
39 
40 namespace mln
41 {
42 
43  namespace value
44  {
45 
46  template <unsigned n>
47  inline
49  {
50  }
51 
52  template <unsigned n>
53  inline
55  {
56  mln_precondition(i >= 0);
57  mln_precondition(unsigned(i) <= mln_max(enc_));
58  this->v_ = static_cast<enc_>(i);
59  }
60 
61  template <unsigned n>
62  inline
64  {
65  this->v_ = 0;
66  }
67 
68  template <unsigned n>
69  inline
70  int_u<n>&
72  {
73  this->v_ = 0;
74  return *this;
75  }
76 
77  template <unsigned n>
78  inline
80  {
81  this->v_ = 1;
82  }
83 
84  template <unsigned n>
85  inline
86  int_u<n>&
88  {
89  this->v_ = 1;
90  return *this;
91  }
92 
93  template <unsigned n>
94  inline
96  {
97  return this->v_;
98  }
99 
100  template <unsigned n>
101  inline
102  int
104  {
105  return - int(this->v_);
106  }
107 
108  template <unsigned n>
109  inline
110  int_u<n>&
112  {
113  mln_precondition(i >= 0);
114  mln_precondition(unsigned(i) <= mln_max(enc_));
115  this->v_ = static_cast<enc_>(i);
116  return *this;
117  }
118 
119  template <unsigned n>
120  inline
121  int_u<n>
123  {
124  return this->v_ + 1;
125  }
126 
127  template <unsigned n>
128  inline
129  std::ostream& operator<<(std::ostream& ostr, const int_u<n>& i)
130  {
131  // FIXME: This code could be factored for almost every Value<*>...
132  return ostr << debug::format(i.to_equiv()); // FIXME: is to_equiv OK?
133  }
134 
135  template <unsigned n>
136  inline
137  std::istream& operator>>(std::istream& istr, int_u<n>& i)
138  {
139  return istr >> i.handle_();
140  }
141 
142 
143  // Conversions
144 
145  template <unsigned n>
146  inline
147  void
148  from_to_(const int_u<n>& from, unsigned& to_)
149  {
150  to_ = from;
151  }
152 
153  template <unsigned n>
154  inline
155  void
156  from_to_(const int_u<n>& from, bool& to_)
157  {
158  to_ = (from != 0u);
159  }
160 
161  template <unsigned n>
162  inline
163  void
164  from_to_(const int_u<n>& from, float& to_)
165  {
166  to_ = static_cast<float>(from);
167  }
168 
169  template <unsigned n>
170  inline
171  void
172  from_to_(const int_u<n>& from, double& to_)
173  {
174  to_ = static_cast<double>(from);
175  }
176 
177  template <unsigned m>
178  inline
179  void
180  from_to_(const int_u<m>& from, qt::rgb32& to)
181  {
182  mlc_bool(m <= 8)::check();
183  to = qt::rgb32(from, from, from);
184  }
185 
186  template <unsigned m>
187  inline
188  void
189  from_to_(const int_u<m>& from, rgb<m>& to)
190  {
191  to = rgb<m>(from, from, from);
192  }
193 
194  } // end of namespace mln::value
195 
196 } // end of namespace mln
197 
198 #endif // ! MLN_VALUE_INT_U_HXX