conversion_impl.hpp
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright 2016-2017 Davide Faconti
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 * * Neither the name of Willow Garage, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 * *******************************************************************/
34 
35 #ifndef VARIANT_IMPL_H
36 #define VARIANT_IMPL_H
37 
38 #include <type_traits>
39 #include <limits>
40 #include <iostream>
43 
44 namespace RosIntrospection
45 {
46 
47 namespace details{
48 
49 template <typename T>
50 using Invoke = typename T::type;
51 
52 
53 template <typename BoolCondition>
55 
56 
57 template <typename T>
58 struct is_integer : std::integral_constant<bool, std::is_integral<T>::value
59  && !std::is_same<T, bool>::value
60  && !std::is_same<T, char>::value>
61 {};
62 
63 template <typename From, typename To>
64 struct is_same_real : std::integral_constant<bool,
65  std::is_same<From, To>::value
66  && std::is_floating_point<To>::value >
67 {};
68 
69 
70 template <typename From, typename To>
72  : std::integral_constant<bool, is_integer<From>::value
73  && is_integer<To>::value
74  && sizeof(From) <= sizeof(To)
75  && std::is_signed<From>::value == std::is_signed<To>::value>
76 {};
77 
78 template <typename From, typename To>
79 struct float_conversion
80  : std::integral_constant<bool, std::is_floating_point<From>::value
81  && std::is_floating_point<To>::value
82  && !std::is_same<From,To>::value >
83 {};
84 
85 template <typename From, typename To>
86 struct unsigned_to_smaller_conversion
87  : std::integral_constant<bool, is_integer<From>::value
88  && is_integer<To>::value
89  && (sizeof(From) > sizeof(To))
90  && !std::is_signed<From>::value
91  && !std::is_signed<To>::value >
92 {};
93 
94 template <typename From, typename To>
95 struct signed_to_smaller_conversion
96  : std::integral_constant<bool, is_integer<From>::value
97  && is_integer<To>::value
98  && (sizeof(From) > sizeof(To))
99 && std::is_signed<From>::value
100 && std::is_signed<To>::value >
101 {};
102 
103 //---------------------------
104 template <typename From, typename To>
105 struct signed_to_smaller_unsigned_conversion
106  : std::integral_constant<bool, is_integer<From>::value
107  && is_integer<To>::value
108  && sizeof(From) >= sizeof(To)
109  && std::is_signed<From>::value
110  && !std::is_signed<To>::value >
111 {};
112 
113 template <typename From, typename To>
114 struct signed_to_larger_unsigned_conversion
115  : std::integral_constant<bool, is_integer<From>::value
116  && is_integer<To>::value
117  && sizeof(From) < sizeof(To)
118  && std::is_signed<From>::value
119  && !std::is_signed<To>::value >
120 {};
121 
122 template <typename From, typename To>
123 struct unsigned_to_smaller_signed_conversion
124  : std::integral_constant<bool, is_integer<From>::value
125  && is_integer<To>::value
126  && (sizeof(From) >= sizeof(To))
127  && !std::is_signed<From>::value
128  && std::is_signed<To>::value >
129 {};
130 
131 template <typename From, typename To>
132 struct unsigned_to_larger_signed_conversion
133  : std::integral_constant<bool, is_integer<From>::value
134  && is_integer<To>::value
135  && sizeof(From) < sizeof(To)
136  && !std::is_signed<From>::value
137  && std::is_signed<To>::value >
138 {};
139 
140 template <typename From, typename To>
141 struct floating_to_signed_conversion
142  : std::integral_constant<bool, std::is_floating_point<From>::value
143  && is_integer<To>::value
144  && std::is_signed<To>::value >
145 {};
146 
147 template <typename From, typename To>
148 struct floating_to_unsigned_conversion
149  : std::integral_constant<bool, std::is_floating_point<From>::value
150  && is_integer<To>::value
151  && !std::is_signed<To>::value >
152 {};
153 
154 template <typename From, typename To>
155 struct integer_to_floating_conversion
156  : std::integral_constant<bool, is_integer<From>::value
157  && std::is_floating_point<To>::value >
158 {};
159 
160 template <typename From, typename To>
161 inline void checkUpperLimit(const From& from)
162 {
163  if ((sizeof(To) < sizeof(From)) &&
164  (from > static_cast<From>(std::numeric_limits<To>::max()))) {
165  throw RangeException("Value too large.");
166  }
167  else if (static_cast<To>(from) > std::numeric_limits<To>::max()) {
168  throw RangeException("Value too large.");
169  }
170 }
171 
172 template <typename From, typename To>
173 inline void checkUpperLimitFloat(const From& from)
174 {
175  if (from > std::numeric_limits<To>::max()){
176  throw RangeException("Value too large.");
177  }
178 }
179 
180 template <typename From, typename To>
181 inline void checkLowerLimitFloat(const From& from)
182 {
183  if (from < -std::numeric_limits<To>::max()){
184  throw RangeException("Value too small.");
185  }
186 }
187 
188 template <typename From, typename To>
189 inline void checkLowerLimit(const From& from)
190 {
191  if (from < std::numeric_limits<To>::min()){
192  throw RangeException("Value too small.");
193  }
194 }
195 
196 template <typename From, typename To>
197 inline void checkTruncation(const From& from)
198 {
199  if (!std::isnan(static_cast<double>(from)) && from != static_cast<From>(static_cast<To>(from)))
200  {
201  throw RangeException("Floating point truncated 1");
202  }
203 }
204 
205 
206 //----------------------- Implementation ----------------------------------------------
207 
208 template<typename SRC,typename DST,
210 inline void convert_impl( const SRC& from, DST& target )
211 {
212  target = from;
213 }
214 
215 
216 template<typename SRC,typename DST,
218 inline void convert_impl( const SRC& from, DST& target )
219 {
220  //std::cout << "is_safe_integer_conversion" << std::endl;
221  target = static_cast<DST>( from);
222 }
223 
224 template<typename SRC,typename DST,
226 inline void convert_impl( const SRC& from, DST& target )
227 {
228  //std::cout << "float_conversion" << std::endl;
229 // checkTruncation<SRC,DST>(from);
230  target = static_cast<DST>( from );
231 }
232 
233 
234 template<typename SRC,typename DST,
236 inline void convert_impl( const SRC& from, DST& target )
237 {
238  //std::cout << "unsigned_to_smaller_conversion" << std::endl;
239 
240  checkUpperLimit<SRC,DST>(from);
241  target = static_cast<DST>( from);
242 }
243 
244 template<typename SRC,typename DST,
246 inline void convert_impl( const SRC& from, DST& target )
247 {
248  //std::cout << "signed_to_smaller_conversion" << std::endl;
249  checkLowerLimit<SRC,DST>(from);
250  checkUpperLimit<SRC,DST>(from);
251  target = static_cast<DST>( from);
252 }
253 
254 
255 template<typename SRC,typename DST,
257 inline void convert_impl( const SRC& from, DST& target )
258 {
259  //std::cout << "signed_to_smaller_unsigned_conversion" << std::endl;
260  if (from < 0 )
261  throw RangeException("Value is negative and can't be converted to signed");
262 
263  checkUpperLimit<SRC,DST>(from);
264 
265  target = static_cast<DST>( from);
266 }
267 
268 
269 template<typename SRC,typename DST,
271 inline void convert_impl( const SRC& from, DST& target )
272 {
273  //std::cout << "signed_to_larger_unsigned_conversion" << std::endl;
274 
275  if ( from < 0 )
276  throw RangeException("Value is negative and can't be converted to signed");
277 
278  target = static_cast<DST>( from);
279 }
280 
281 template<typename SRC,typename DST,
283 inline void convert_impl( const SRC& from, DST& target )
284 {
285  //std::cout << "unsigned_to_larger_signed_conversion" << std::endl;
286  target = static_cast<DST>( from);
287 }
288 
289 template<typename SRC,typename DST,
291 inline void convert_impl( const SRC& from, DST& target )
292 {
293  //std::cout << "unsigned_to_smaller_signed_conversion" << std::endl;
294 
295  checkUpperLimit<SRC,DST>(from);
296  target = static_cast<DST>( from);
297 }
298 
299 template<typename SRC,typename DST,
301 inline void convert_impl( const SRC& from, DST& target )
302 {
303  //std::cout << "floating_to_signed_conversion" << std::endl;
304 
305  checkLowerLimitFloat<SRC,DST>(from);
306  checkLowerLimitFloat<SRC,DST>(from);
307 
308  if( from != static_cast<SRC>(static_cast<DST>( from)))
309  throw RangeException("Floating point truncated");
310 
311  target = static_cast<DST>( from);
312 }
313 
314 template<typename SRC,typename DST,
316 inline void convert_impl( const SRC& from, DST& target )
317 {
318  //std::cout << "floating_to_unsigned_conversion" << std::endl;
319  if ( from < 0 )
320  throw RangeException("Value is negative and can't be converted to signed");
321 
322  checkLowerLimitFloat<SRC,DST>(from);
323 
324  if( from != static_cast<SRC>(static_cast<DST>( from)))
325  throw RangeException("Floating point truncated");
326 
327  target = static_cast<DST>( from);
328 }
329 
330 template<typename SRC,typename DST,
332 inline void convert_impl( const SRC& from, DST& target )
333 {
334  //std::cout << "floating_to_unsigned_conversion" << std::endl;
335 
336  checkTruncation<SRC,DST>(from);
337  target = static_cast<DST>( from);
338 }
339 
340 } //end namespace details
341 
342 } //end namespace
343 
344 #endif // VARIANT_H
345 
type
Definition: core.h:969
Invoke< std::enable_if< BoolCondition::value > > EnableIf


plotjuggler_ros
Author(s): Davide Faconti
autogenerated on Fri Jun 23 2023 02:28:02