convert_impl.hpp
Go to the documentation of this file.
1 /* Copyright (C) 2022 Davide Faconti, Eurecat - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #pragma once
14 
15 #include <type_traits>
16 #include "simple_string.hpp"
17 
18 #undef max
19 #undef min
20 
21 namespace SafeAny
22 {
23 
24 namespace details
25 {
26 
27 template <typename BoolCondition>
28 using EnableIf = typename std::enable_if<BoolCondition::value, void>::type;
29 
30 template <typename T>
31 constexpr bool is_integer()
32 {
35 }
36 
37 template <typename T>
38 constexpr bool is_convertible_type()
39 {
40  return is_integer<T>() || std::is_floating_point<T>::value ||
43 }
44 
45 template <typename T>
46 constexpr bool is_convertible_to_bool()
47 {
48  return is_integer<T>() || std::is_floating_point<T>::value || std::is_enum<T>::value;
49 }
50 
51 template <typename T>
52 constexpr bool is_signed()
53 {
55 }
56 
57 template <typename T1, typename T2>
58 constexpr bool is_same()
59 {
61 }
62 
63 template <typename From, typename To>
64 inline void checkUpperLimit(const From& from)
65 {
66  if(from > static_cast<From>(std::numeric_limits<To>::max()))
67  {
68  throw std::runtime_error("Value outside the max numerical limit.");
69  }
70 }
71 
72 template <typename From, typename To>
73 inline void checkLowerLimit(const From& from)
74 {
75  if constexpr(std::is_same<To, bool>::value)
76  {
77  if(from != 0 && from != 1)
78  {
79  throw std::runtime_error("Implicit casting to bool is not allowed");
80  }
81  }
82  else if(from < std::numeric_limits<To>::min())
83  {
84  throw std::runtime_error("Value outside the lovest numerical limit.");
85  }
86 }
87 
88 template <typename From, typename To>
89 inline void checkTruncation(const From& from)
90 {
91  if(from != static_cast<From>(static_cast<To>(from)))
92  {
93  throw std::runtime_error("Floating point truncated");
94  }
95 }
96 
97 //----------------------- Implementation ----------------------------------------------
98 
99 template <typename SRC, typename DST>
100 void convertNumber(const SRC& source, DST& target)
101 {
102  static_assert(is_convertible_type<SRC>() && is_convertible_type<DST>(), "Not "
103  "convertible");
104 
105  constexpr bool both_integers = is_integer<SRC>() && is_integer<DST>();
106 
107  if constexpr(is_signed<SRC>() && !is_signed<DST>())
108  {
109  if(source < 0)
110  {
111  throw std::runtime_error("Value is negative and can't be converted to unsigned");
112  }
113  }
114  // these conversions are always safe:
115  // - same type
116  // - float -> double
117  if constexpr(is_same<SRC, DST>() || (is_same<SRC, float>() && is_same<DST, double>()))
118  {
119  // No check needed
120  target = static_cast<DST>(source);
121  }
122  else if constexpr(both_integers)
123  {
124  if constexpr(sizeof(SRC) == sizeof(DST) && !is_signed<SRC>() && is_signed<DST>())
125  {
126  checkUpperLimit<SRC, DST>(source);
127  }
128  // casting to a smaller number need to be check
129  else if constexpr(sizeof(SRC) > sizeof(DST))
130  {
131  if constexpr(is_signed<SRC>())
132  {
133  checkLowerLimit<SRC, DST>(source);
134  }
135  checkUpperLimit<SRC, DST>(source);
136  }
137  target = static_cast<DST>(source);
138  }
139  // special case: bool accept truncation
140  else if constexpr(is_convertible_to_bool<SRC>() && is_same<DST, bool>())
141  {
142  target = static_cast<DST>(source);
143  }
144  // casting to/from floating points might cause truncation.
145  else if constexpr(std::is_floating_point<SRC>::value ||
147  {
148  bool both_float =
150  // to avoid being too pedantic, let's accept casting between double and float
151  if(!both_float)
152  {
153  checkTruncation<SRC, DST>(source);
154  }
155  target = static_cast<DST>(source);
156  }
157 }
158 
159 } //end namespace details
160 } //end namespace SafeAny
SafeAny
Definition: convert_impl.hpp:21
SafeAny::details::checkUpperLimit
void checkUpperLimit(const From &from)
Definition: convert_impl.hpp:64
SafeAny::details::checkLowerLimit
void checkLowerLimit(const From &from)
Definition: convert_impl.hpp:73
SafeAny::details::convertNumber
void convertNumber(const SRC &source, DST &target)
Definition: convert_impl.hpp:100
SafeAny::details::is_same
constexpr bool is_same()
Definition: convert_impl.hpp:58
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:664
SafeAny::details::checkTruncation
void checkTruncation(const From &from)
Definition: convert_impl.hpp:89
simple_string.hpp
SafeAny::details::is_convertible_to_bool
constexpr bool is_convertible_to_bool()
Definition: convert_impl.hpp:46
SafeAny::details::is_convertible_type
constexpr bool is_convertible_type()
Definition: convert_impl.hpp:38
SafeAny::details::EnableIf
typename std::enable_if< BoolCondition::value, void >::type EnableIf
Definition: convert_impl.hpp:28
SafeAny::details::is_integer
constexpr bool is_integer()
Definition: convert_impl.hpp:31
SafeAny::details::is_signed
constexpr bool is_signed()
Definition: convert_impl.hpp:52


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16