iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConversionImpl.H
Go to the documentation of this file.
1 /*! @file
2  @author Laurent Itti
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 #ifndef INCLUDE_NRT_CORE_TYPING_DETAILS_CONVERSIONIMPL_H
36 #define INCLUDE_NRT_CORE_TYPING_DETAILS_CONVERSIONIMPL_H
37 
38 template <class dest_type, class source_type> inline
39 typename std::enable_if<nrt::not_subranged<dest_type,source_type>::value, dest_type>::type
40 nrt::clamped_convert(source_type source)
41 { return dest_type(source); }
42 
43 template <class dest_type, class source_type> inline
44 typename std::enable_if<nrt::not_subranged_round<dest_type,source_type>::value, dest_type>::type
45 nrt::clamped_convert(source_type source)
46 { return dest_type(std::round(source)); }
47 
48 template <class dest_type, class source_type> inline
49 typename std::enable_if<nrt::subranged_signed<dest_type, source_type>::value, dest_type>::type
50 nrt::clamped_convert(source_type source)
51 {
52  if (source > boost::numeric::bounds<dest_type>::highest())
53  { return boost::numeric::bounds<dest_type>::highest(); }
54  else if (source < boost::numeric::bounds<dest_type>::lowest())
55  { return boost::numeric::bounds<dest_type>::lowest(); }
56  return dest_type(source);
57 }
58 
59 template <class dest_type, class source_type> inline
60 typename std::enable_if<nrt::subranged_unsigned<dest_type, source_type>::value, dest_type>::type
61 nrt::clamped_convert(source_type source)
62 {
63  if (source > boost::numeric::bounds<dest_type>::highest())
64  { return boost::numeric::bounds<dest_type>::highest(); }
65  else if (source < boost::numeric::bounds<dest_type>::lowest())
66  { return boost::numeric::bounds<dest_type>::lowest(); }
67  return dest_type(source);
68 }
69 
70 template <class dest_type, class source_type> inline
71 typename std::enable_if<nrt::subranged_signed_unsigned<dest_type, source_type>::value, dest_type>::type
72 nrt::clamped_convert(source_type source)
73 {
74  if ((source > 0) &&
75  (typename std::make_unsigned<source_type>::type(source) > boost::numeric::bounds<dest_type>::highest()))
76  { return boost::numeric::bounds<dest_type>::highest(); }
77  else if (source < 0)
78  { return 0; }
79  return dest_type(source);
80 }
81 
82 template <class dest_type, class source_type> inline
83 typename std::enable_if<nrt::subranged_unsigned_signed<dest_type, source_type>::value, dest_type>::type
84 nrt::clamped_convert(source_type source)
85 {
86  if (source > typename std::make_unsigned<dest_type>::type(boost::numeric::bounds<dest_type>::highest()))
87  { return boost::numeric::bounds<dest_type>::highest(); }
88  return dest_type(source);
89 }
90 
91 template <class dest_type, class source_type> inline
92 typename std::enable_if<nrt::subranged_float<dest_type, source_type>::value, dest_type>::type
93 nrt::clamped_convert(source_type source)
94 {
95  if (source > boost::numeric::bounds<dest_type>::highest())
96  { return boost::numeric::bounds<dest_type>::highest(); }
97  else if (source < boost::numeric::bounds<dest_type>::lowest())
98  { return boost::numeric::bounds<dest_type>::lowest(); }
99  return dest_type(source);
100 }
101 
102 template <class dest_type, class source_type> inline
103 typename std::enable_if<nrt::subranged_float_round<dest_type, source_type>::value, dest_type>::type
104 nrt::clamped_convert(source_type source)
105 {
106  if (source > boost::numeric::bounds<dest_type>::highest())
107  { return boost::numeric::bounds<dest_type>::highest(); }
108  else if (source < boost::numeric::bounds<dest_type>::lowest())
109  { return boost::numeric::bounds<dest_type>::lowest(); }
110  return dest_type(std::round(source));
111 }
112 
113 #endif // INCLUDE_NRT_CORE_TYPING_DETAILS_CONVERSIONIMPL_H