$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
from_float_to_value.hh
1 // Copyright (C) 2008, 2009, 2010, 2011, 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_CONVERT_IMPL_FROM_FLOAT_TO_VALUE_HH
28 # define MLN_CONVERT_IMPL_FROM_FLOAT_TO_VALUE_HH
29 
35 
36 # include <utility>
37 # include <mln/value/concept/integer.hh>
38 # include <mln/value/concept/floating.hh>
39 # include <mln/core/concept/value.hh>
40 # include <mln/math/round.hh>
41 
42 
43 
44 
45 namespace mln
46 {
47 
49  template <typename V>
50  void from_to_(const float& from, Value<V>& to);
51 
53  void from_to_(const float& from, unsigned& to);
54 
56  void from_to_(const float& from, int& to);
57 
59  void from_to_(const float& from, short& to);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  namespace convert
65  {
66 
67  namespace impl
68  {
69 
70  // Case 1:
71 
72  template <typename V>
73  inline
74  void
75  from_float_to_value(const float& from,
77  {
78  exact(to) = math::round<V>(from);
79  }
80 
81  // Case 2:
82 
83  template <typename V>
84  inline
85  void
86  from_float_to_value(const float& from,
88  {
89  exact(to) = from;
90  }
91 
92 
93  // Default: no conversion defined.
94 
95  template <typename V>
96  inline
97  void
98  from_float_to_value(const float& from,
99  Value<V>& to)
100  {
101  (void) from;
102  (void) to;
103  mlc_abort(V)::check();
104  }
105 
106  } // end of namespace mln::convert::impl
107 
108 
109  namespace internal
110  {
111 
112  template <typename V>
113  inline
114  void
115  from_float_to_value_dispatch(const float& from, Value<V>& to)
116  {
117  impl::from_float_to_value(from, exact(to));
118  }
119 
120  } // end of namespace mln::convert::internal
121 
122  } // end of namespace mln::convert
123 
124 
125  // float-> Value
126  template <typename V>
127  inline
128  void
129  from_to_(const float& from, Value<V>& to)
130  {
131  convert::internal::from_float_to_value_dispatch(from, to);
132  }
133 
134  // float-> unsigned
135  inline
136  void
137  from_to_(const float& from, unsigned& to)
138  {
139  mln_precondition(from >= 0);
140  to = math::round<unsigned>(from);
141  }
142 
143  // float-> int
144  inline
145  void
146  from_to_(const float& from, int& to)
147  {
148  to = math::round<int>(from);
149  }
150 
151  // float-> short
152  inline
153  void
154  from_to_(const float& from, short& to)
155  {
156  to = math::round<short>(from);
157  }
158 
159 
160 # endif // ! MLN_INCLUDE_ONLY
161 
162 } // end of namespace mln
163 
164 
165 #endif // ! MLN_CONVERT_IMPL_FROM_FLOAT_TO_VALUE_HH