$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
from_double_to_value.hh
1 // Copyright (C) 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_DOUBLE_TO_VALUE_HH
28 # define MLN_CONVERT_IMPL_FROM_DOUBLE_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 double& from, Value<V>& to);
51 
53  void from_to_(const double& from, unsigned& to);
54 
56  void from_to_(const double& from, int& to);
57 
58 
59 # ifndef MLN_INCLUDE_ONLY
60 
61  namespace convert
62  {
63 
64  namespace impl
65  {
66 
67  // Case 1:
68 
69  template <typename V>
70  inline
71  void
72  from_double_to_value(const double& from,
74  {
75  exact(to) = math::round<V>(from);
76  }
77 
78  // Case 2:
79 
80  template <typename V>
81  inline
82  void
83  from_double_to_value(const double& from,
85  {
86  exact(to) = from;
87  }
88 
89 
90  // Default: no conversion defined.
91 
92  template <typename V>
93  inline
94  void
95  from_double_to_value(const double& from,
96  Value<V>& to)
97  {
98  (void) from;
99  (void) to;
100  mlc_abort(V)::check();
101  }
102 
103  } // end of namespace mln::convert::impl
104 
105 
106  namespace internal
107  {
108 
109  template <typename V>
110  inline
111  void
112  from_double_to_value_dispatch(const double& from, Value<V>& to)
113  {
114  impl::from_double_to_value(from, exact(to));
115  }
116 
117  } // end of namespace mln::convert::internal
118 
119  } // end of namespace mln::convert
120 
121 
122  // double -> Value
123  template <typename V>
124  inline
125  void
126  from_to_(const double& from, Value<V>& to)
127  {
128  convert::internal::from_double_to_value_dispatch(from, to);
129  }
130 
131  // double -> unsigned
132  inline
133  void
134  from_to_(const double& from, unsigned& to)
135  {
136  mln_precondition(from >= 0);
137  to = math::round<unsigned>(from);
138  }
139 
140  // double -> int
141  inline
142  void
143  from_to_(const double& from, int& to)
144  {
145  to = math::round<int>(from);
146  }
147 
148 # endif // ! MLN_INCLUDE_ONLY
149 
150 } // end of namespace mln
151 
152 
153 #endif // ! MLN_CONVERT_IMPL_FROM_DOUBLE_TO_VALUE_HH