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( from != static_cast<From>(static_cast<To>( from))){
200  throw RangeException("Floating point truncated");
201  }
202 }
203 
204 
205 //----------------------- Implementation ----------------------------------------------
206 
207 template<typename SRC,typename DST,
209 inline void convert_impl( const SRC& from, DST& target )
210 {
211  target = from;
212 }
213 
214 
215 template<typename SRC,typename DST,
217 inline void convert_impl( const SRC& from, DST& target )
218 {
219  //std::cout << "is_safe_integer_conversion" << std::endl;
220  target = static_cast<DST>( from);
221 }
222 
223 template<typename SRC,typename DST,
225 inline void convert_impl( const SRC& from, DST& target )
226 {
227  //std::cout << "float_conversion" << std::endl;
228  checkTruncation<SRC,DST>(from);
229  target = static_cast<DST>( from );
230 }
231 
232 
233 template<typename SRC,typename DST,
235 inline void convert_impl( const SRC& from, DST& target )
236 {
237  //std::cout << "unsigned_to_smaller_conversion" << std::endl;
238 
239  checkUpperLimit<SRC,DST>(from);
240  target = static_cast<DST>( from);
241 }
242 
243 template<typename SRC,typename DST,
245 inline void convert_impl( const SRC& from, DST& target )
246 {
247  //std::cout << "signed_to_smaller_conversion" << std::endl;
248  checkLowerLimit<SRC,DST>(from);
249  checkUpperLimit<SRC,DST>(from);
250  target = static_cast<DST>( from);
251 }
252 
253 
254 template<typename SRC,typename DST,
256 inline void convert_impl( const SRC& from, DST& target )
257 {
258  //std::cout << "signed_to_smaller_unsigned_conversion" << std::endl;
259  if (from < 0 )
260  throw RangeException("Value is negative and can't be converted to signed");
261 
262  checkUpperLimit<SRC,DST>(from);
263 
264  target = static_cast<DST>( from);
265 }
266 
267 
268 template<typename SRC,typename DST,
270 inline void convert_impl( const SRC& from, DST& target )
271 {
272  //std::cout << "signed_to_larger_unsigned_conversion" << std::endl;
273 
274  if ( from < 0 )
275  throw RangeException("Value is negative and can't be converted to signed");
276 
277  target = static_cast<DST>( from);
278 }
279 
280 template<typename SRC,typename DST,
282 inline void convert_impl( const SRC& from, DST& target )
283 {
284  //std::cout << "unsigned_to_larger_signed_conversion" << std::endl;
285  target = static_cast<DST>( from);
286 }
287 
288 template<typename SRC,typename DST,
290 inline void convert_impl( const SRC& from, DST& target )
291 {
292  //std::cout << "unsigned_to_smaller_signed_conversion" << std::endl;
293 
294  checkUpperLimit<SRC,DST>(from);
295  target = static_cast<DST>( from);
296 }
297 
298 template<typename SRC,typename DST,
300 inline void convert_impl( const SRC& from, DST& target )
301 {
302  //std::cout << "floating_to_signed_conversion" << std::endl;
303 
304  checkLowerLimitFloat<SRC,DST>(from);
305  checkLowerLimitFloat<SRC,DST>(from);
306 
307  if( from != static_cast<SRC>(static_cast<DST>( from)))
308  throw RangeException("Floating point truncated");
309 
310  target = static_cast<DST>( from);
311 }
312 
313 template<typename SRC,typename DST,
315 inline void convert_impl( const SRC& from, DST& target )
316 {
317  //std::cout << "floating_to_unsigned_conversion" << std::endl;
318  if ( from < 0 )
319  throw RangeException("Value is negative and can't be converted to signed");
320 
321  checkLowerLimitFloat<SRC,DST>(from);
322 
323  if( from != static_cast<SRC>(static_cast<DST>( from)))
324  throw RangeException("Floating point truncated");
325 
326  target = static_cast<DST>( from);
327 }
328 
329 template<typename SRC,typename DST,
331 inline void convert_impl( const SRC& from, DST& target )
332 {
333  //std::cout << "floating_to_unsigned_conversion" << std::endl;
334 
335  checkTruncation<SRC,DST>(from);
336  target = static_cast<DST>( from);
337 }
338 
339 } //end namespace details
340 
341 } //end namespace
342 
343 #endif // VARIANT_H
344 
Invoke< std::enable_if< BoolCondition::value > > EnableIf
size_t from


ros_type_introspection
Author(s): Davide Faconti
autogenerated on Thu May 16 2019 02:39:09