NumericConv.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2023 SICK AG, Waldkirch
3 //
4 // SPDX-License-Identifier: Unlicense
5 
6 #pragma once
7 
8 #include <limits> // for type value ranges
9 #include <type_traits> // for meta programming
10 
11 namespace visionary {
12 
13 // Helper for chosing the right clamping method
14 // depending on signedness of target and source types
15 template <typename TTrg, typename TSrc, bool u1, bool u2>
17 {
18  // here both types are signed
19  static TTrg clamped(const TSrc& src)
20  {
21  static constexpr TTrg trgMinVal = std::numeric_limits<TTrg>::lowest();
22  static constexpr TTrg trgMaxVal = std::numeric_limits<TTrg>::max();
23 
24  using max_type = typename std::common_type<TTrg, TSrc>::type;
25 
26  if (static_cast<max_type>(src) >= static_cast<max_type>(trgMaxVal))
27  {
28  return trgMaxVal;
29  }
30  else if (static_cast<max_type>(src) <= static_cast<max_type>(trgMinVal))
31  {
32  return trgMinVal;
33  }
34  else
35  {
36  return static_cast<TTrg>(src);
37  }
38  }
39 };
40 
41 template <typename TTrg, typename TSrc>
42 struct NumericCastImpl<TTrg, TSrc, true, false>
43 {
44  static TTrg clamped(const TSrc& src)
45  {
46  static constexpr TTrg trgMinVal = std::numeric_limits<TTrg>::lowest();
47  static constexpr TTrg trgMaxVal = std::numeric_limits<TTrg>::max();
48 
49  using max_type = typename std::common_type<TTrg, TSrc>::type;
50 
51  // a negative src value might be converted to unsigned yielding wrong results
52  // since 0 is (for unsigned types) also the ::min, we can omit an additional test for src < trgMinVal
53  if (src < 0)
54  {
55  return trgMinVal;
56  }
57  if (static_cast<max_type>(src) > static_cast<max_type>(trgMaxVal))
58  {
59  return trgMaxVal;
60  }
61  else
62  {
63  return static_cast<TTrg>(src);
64  }
65  }
66 };
67 
68 template <typename TTrg, typename TSrc>
69 struct NumericCastImpl<TTrg, TSrc, false, true>
70 {
71  static TTrg clamped(const TSrc& src)
72  {
73  static constexpr TTrg trgMaxVal = std::numeric_limits<TTrg>::max();
74 
75  using max_type = typename std::common_type<TTrg, TSrc>::type;
76 
77  // only need to clip to max value
78  if (static_cast<max_type>(src) > static_cast<max_type>(trgMaxVal))
79  {
80  return trgMaxVal;
81  }
82  else
83  {
84  return static_cast<TTrg>(src);
85  }
86  }
87 };
88 
93 template <typename TTrg, typename TSrc>
95 {
101  static TTrg clamped(const TSrc& src)
102  {
103  return NumericCastImpl<TTrg, TSrc, std::is_unsigned<TTrg>::value, std::is_unsigned<TSrc>::value>::clamped(src);
104  }
105 };
106 
107 template <typename TSrc>
108 struct NumericCast<TSrc, TSrc>
109 {
110  static TSrc clamped(const TSrc& src)
111  {
112  return src;
113  }
114 };
115 
116 template <typename TTrg, typename TSrc>
117 TTrg castClamped(const TSrc& src)
118 {
120 }
121 
122 } // namespace visionary
visionary::castClamped
TTrg castClamped(const TSrc &src)
Definition: NumericConv.h:117
visionary
Definition: MD5.cpp:44
visionary::NumericCastImpl< TTrg, TSrc, false, true >::clamped
static TTrg clamped(const TSrc &src)
Definition: NumericConv.h:71
visionary::NumericCast< TSrc, TSrc >::clamped
static TSrc clamped(const TSrc &src)
Definition: NumericConv.h:110
visionary::NumericCastImpl::clamped
static TTrg clamped(const TSrc &src)
Definition: NumericConv.h:19
visionary::NumericCast
Definition: NumericConv.h:94
visionary::NumericCastImpl< TTrg, TSrc, true, false >::clamped
static TTrg clamped(const TSrc &src)
Definition: NumericConv.h:44
visionary::NumericCastImpl
Definition: NumericConv.h:16
visionary::NumericCast::clamped
static TTrg clamped(const TSrc &src)
Definition: NumericConv.h:101


sick_visionary_ros
Author(s): SICK AG TechSupport 3D Snapshot
autogenerated on Thu Feb 8 2024 03:45:33