Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
from_float_to_value.hh
1 // Copyright (C) 2008, 2009, 2010, 2011 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_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 
48  namespace convert
49  {
50 
52  template <typename V>
53  void
54  from_to(const float& from, Value<V>& to);
55 
56 
57 # ifndef MLN_INCLUDE_ONLY
58 
59  namespace impl
60  {
61 
62  // Case 1:
63 
64  template <typename V>
65  inline
66  void
67  from_float_to_value(const float& from,
69  {
70  exact(to) = math::round<V>()(from);
71  }
72 
73  // Case 2:
74 
75  template <typename V>
76  inline
77  void
78  from_float_to_value(const float& from,
79  mln::value::Floating<V>& to)
80  {
81  exact(to) = from;
82  }
83 
84 
85  // Default: no conversion defined.
86 
87  template <typename V>
88  inline
89  void
90  from_float_to_value(const float& from,
91  Value<V>& to)
92  {
93  (void) from;
94  (void) to;
95  mlc_abort(V)::check();
96  }
97 
98  } // end of namespace mln::convert::impl
99 
100 
101  namespace internal
102  {
103 
104  template <typename V>
105  inline
106  void
107  from_float_to_value_dispatch(const float& from, Value<V>& to)
108  {
109  impl::from_float_to_value(from, exact(to));
110  }
111 
112  } // end of namespace mln::convert::internal
113 
114 
115  namespace over_load
116  {
117 
118  // Facades.
119 
120 
121  // float-> Value
122  template <typename V>
123  inline
124  void
125  from_to_(const float& from, Value<V>& to)
126  {
127  internal::from_float_to_value_dispatch(from, to);
128  }
129 
130  // float-> unsigned
131  inline
132  void
133  from_to_(const float& from,
134  unsigned& to)
135  {
136  mln_precondition(from >= 0);
137  to = math::round<unsigned>()(from);
138  }
139 
140  // float-> int
141  inline
142  void
143  from_to_(const float& from,
144  int& to)
145  {
146  to = math::round<int>()(from);
147  }
148 
149  // float-> short
150  inline
151  void
152  from_to_(const float& from,
153  short& to)
154  {
155  to = math::round<short>()(from);
156  }
157 
158 
159  } // end of namespace mln::convert::over_load
160 
161 # endif // ! MLN_INCLUDE_ONLY
162 
163  } // end of namespace mln::convert
164 
165 } // end of namespace mln
166 
167 
168 #endif // ! MLN_CONVERT_IMPL_FROM_FLOAT_TO_VALUE_HH