5
5
#include < cmath>
6
6
#include < limits>
7
7
#include < stdexcept>
8
+
8
9
#include < string>
9
10
#include < string.h>
11
+ #include < stdlib.h>
10
12
#include TYPE_TRAITS_HEADER
11
13
12
14
namespace autowiring {
@@ -107,6 +109,9 @@ namespace autowiring {
107
109
std::string marshal (const void * ptr) const override {
108
110
std::string retVal;
109
111
type val = *static_cast <const type*>(ptr);
112
+ if (val == 0 )
113
+ return " 0" ;
114
+
110
115
bool pos = 0 < val;
111
116
if (!pos)
112
117
val *= ~0 ;
@@ -120,28 +125,20 @@ namespace autowiring {
120
125
auto first = retVal.begin (), last = retVal.end ();
121
126
(first != last) && (first != --last);
122
127
++first
123
- )
128
+ )
124
129
std::swap (*first, *last);
125
130
return retVal;
126
131
}
127
132
128
133
void unmarshal (void * ptr, const char * szValue) const override {
129
134
type& value = *static_cast <type*>(ptr);
130
- bool negative = *szValue == ' -' ;
131
- if (negative) {
132
- // Skip over the sign, verify we aren't assigning to the wrong field type
133
- szValue++;
134
- if (std::is_unsigned<type>::value)
135
- throw std::range_error (" Attempted to set a signed value on an unsigned calibration field" );
136
- }
135
+ char * end = nullptr ;
136
+ const auto llvalue = strtoll (szValue, &end, 10 );
137
137
138
- for (value = 0 ; *szValue; szValue++) {
139
- if (*szValue < ' 0' || ' 9' < *szValue)
140
- throw std::invalid_argument (" String value is not an integer" );
141
- value = *szValue - ' 0' + value * 10 ;
142
- }
143
- if (negative)
144
- value *= -1 ;
138
+ if (llvalue > std::numeric_limits<type>::max () || llvalue < std::numeric_limits<type>::min ())
139
+ throw std::range_error (" Overflow error, value is outside the range representable by this type." );
140
+
141
+ value = static_cast <type>(llvalue);
145
142
}
146
143
147
144
void copy (void * lhs, const void * rhs) const override {
0 commit comments