Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
yaml-cpp
Commits
4dbfeb0b
Unverified
Commit
4dbfeb0b
authored
Jun 15, 2020
by
Chen
Committed by
GitHub
Jun 15, 2020
Browse files
Support as<uint8_t>/as<int8_t>.
Fix issue 844/848.
parent
33316d53
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
17 deletions
+55
-17
docs/Breaking-Changes.md
docs/Breaking-Changes.md
+1
-0
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+26
-2
test/integration/load_node_test.cpp
test/integration/load_node_test.cpp
+28
-15
No files found.
docs/Breaking-Changes.md
View file @
4dbfeb0b
...
...
@@ -5,6 +5,7 @@
## HEAD ##
*
Throws an exception when trying to parse a negative number as an unsigned integer.
*
Supports the
`as<int8_t>`
/
`as<uint8_t>`
, which throws an exception when the value exceeds the range of
`int8_t`
/
`uint8_t`
.
## 0.6.0 ##
...
...
include/yaml-cpp/node/convert.h
View file @
4dbfeb0b
...
...
@@ -15,7 +15,6 @@
#include <sstream>
#include <type_traits>
#include <vector>
#include <type_traits>
#include "yaml-cpp/binary.h"
#include "yaml-cpp/node/impl.h"
...
...
@@ -114,6 +113,31 @@ typename std::enable_if<!std::is_floating_point<T>::value, void>::type
inner_encode
(
const
T
&
rhs
,
std
::
stringstream
&
stream
){
stream
<<
rhs
;
}
template
<
typename
T
>
typename
std
::
enable_if
<
(
std
::
is_same
<
T
,
unsigned
char
>::
value
||
std
::
is_same
<
T
,
signed
char
>::
value
),
bool
>::
type
ConvertStreamTo
(
std
::
stringstream
&
stream
,
T
&
rhs
)
{
int
num
;
if
((
stream
>>
std
::
noskipws
>>
num
)
&&
(
stream
>>
std
::
ws
).
eof
())
{
if
(
num
>=
std
::
numeric_limits
<
T
>::
min
()
&&
num
<=
std
::
numeric_limits
<
T
>::
max
())
{
rhs
=
num
;
return
true
;
}
}
return
false
;
}
template
<
typename
T
>
typename
std
::
enable_if
<!
(
std
::
is_same
<
T
,
unsigned
char
>::
value
||
std
::
is_same
<
T
,
signed
char
>::
value
),
bool
>::
type
ConvertStreamTo
(
std
::
stringstream
&
stream
,
T
&
rhs
)
{
if
((
stream
>>
std
::
noskipws
>>
rhs
)
&&
(
stream
>>
std
::
ws
).
eof
())
{
return
true
;
}
return
false
;
}
}
#define YAML_DEFINE_CONVERT_STREAMABLE(type, negative_op) \
...
...
@@ -137,7 +161,7 @@ inner_encode(const T& rhs, std::stringstream& stream){
if ((stream.peek() == '-') && std::is_unsigned<type>::value) { \
return false; \
} \
if (
(stream >> std::noskipws >> rhs) && (stream >> std::ws).eof()) {
\
if (
conversion::ConvertStreamTo(stream, rhs)) {
\
return true; \
} \
if (std::numeric_limits<type>::has_infinity) { \
...
...
test/integration/load_node_test.cpp
View file @
4dbfeb0b
...
...
@@ -21,22 +21,35 @@ TEST(LoadNodeTest, FallbackValues) {
}
TEST
(
LoadNodeTest
,
NumericConversion
)
{
Node
node
=
Load
(
"[1.5, 1, .nan, .inf, -.inf, 0x15, 015]"
);
EXPECT_EQ
(
1.5
f
,
node
[
0
].
as
<
float
>
());
EXPECT_EQ
(
1.5
,
node
[
0
].
as
<
double
>
());
EXPECT_THROW
(
node
[
0
].
as
<
int
>
(),
TypedBadConversion
<
int
>
);
EXPECT_EQ
(
1
,
node
[
1
].
as
<
int
>
());
EXPECT_EQ
(
1.0
f
,
node
[
1
].
as
<
float
>
());
EXPECT_NE
(
node
[
2
].
as
<
float
>
(),
node
[
2
].
as
<
float
>
());
EXPECT_EQ
(
std
::
numeric_limits
<
float
>::
infinity
(),
node
[
3
].
as
<
float
>
());
EXPECT_EQ
(
-
std
::
numeric_limits
<
float
>::
infinity
(),
node
[
4
].
as
<
float
>
());
EXPECT_EQ
(
21
,
node
[
5
].
as
<
int
>
());
EXPECT_EQ
(
13
,
node
[
6
].
as
<
int
>
());
EXPECT_EQ
(
1.5
f
,
Load
(
"1.5"
).
as
<
float
>
());
EXPECT_EQ
(
1.5
,
Load
(
"1.5"
).
as
<
double
>
());
EXPECT_THROW
(
Load
(
"1.5"
).
as
<
int
>
(),
TypedBadConversion
<
int
>
);
EXPECT_EQ
(
1
,
Load
(
"1"
).
as
<
int
>
());
EXPECT_EQ
(
1.0
f
,
Load
(
"1"
).
as
<
float
>
());
EXPECT_NE
(
Load
(
".nan"
).
as
<
float
>
(),
Load
(
".nan"
).
as
<
float
>
());
EXPECT_EQ
(
std
::
numeric_limits
<
float
>::
infinity
(),
Load
(
".inf"
).
as
<
float
>
());
EXPECT_EQ
(
-
std
::
numeric_limits
<
float
>::
infinity
(),
Load
(
"-.inf"
).
as
<
float
>
());
EXPECT_EQ
(
21
,
Load
(
"0x15"
).
as
<
int
>
());
EXPECT_EQ
(
13
,
Load
(
"015"
).
as
<
int
>
());
EXPECT_EQ
(
-
128
,
+
Load
(
"-128"
).
as
<
int8_t
>
());
EXPECT_EQ
(
127
,
+
Load
(
"127"
).
as
<
int8_t
>
());
EXPECT_THROW
(
Load
(
"128"
).
as
<
int8_t
>
(),
TypedBadConversion
<
signed
char
>
);
EXPECT_EQ
(
255
,
+
Load
(
"255"
).
as
<
uint8_t
>
());
EXPECT_THROW
(
Load
(
"256"
).
as
<
uint8_t
>
(),
TypedBadConversion
<
unsigned
char
>
);
// test as<char>/as<uint8_t> with ‘a’,"ab",'1',"127"
EXPECT_EQ
(
'a'
,
Load
(
"a"
).
as
<
char
>
());
EXPECT_THROW
(
Load
(
"ab"
).
as
<
char
>
(),
TypedBadConversion
<
char
>
);
EXPECT_EQ
(
'1'
,
Load
(
"1"
).
as
<
char
>
());
EXPECT_THROW
(
Load
(
"127"
).
as
<
char
>
(),
TypedBadConversion
<
char
>
);
EXPECT_THROW
(
Load
(
"a"
).
as
<
uint8_t
>
(),
TypedBadConversion
<
unsigned
char
>
);
EXPECT_THROW
(
Load
(
"ab"
).
as
<
uint8_t
>
(),
TypedBadConversion
<
unsigned
char
>
);
EXPECT_EQ
(
1
,
+
Load
(
"1"
).
as
<
uint8_t
>
());
// Throw exception: convert a negative number to an unsigned number.
EXPECT_THROW
(
node
[
7
].
as
<
unsigned
>
(),
TypedBadConversion
<
unsigned
int
>
);
EXPECT_THROW
(
node
[
7
].
as
<
unsigned
short
>
(),
TypedBadConversion
<
unsigned
short
>
);
EXPECT_THROW
(
node
[
7
].
as
<
unsigned
long
>
(),
TypedBadConversion
<
unsigned
long
>
);
EXPECT_THROW
(
node
[
7
].
as
<
unsigned
long
long
>
(),
TypedBadConversion
<
unsigned
long
long
>
);
EXPECT_THROW
(
Load
(
"-128"
).
as
<
unsigned
>
(),
TypedBadConversion
<
unsigned
int
>
);
EXPECT_THROW
(
Load
(
"-128"
).
as
<
unsigned
short
>
(),
TypedBadConversion
<
unsigned
short
>
);
EXPECT_THROW
(
Load
(
"-128"
).
as
<
unsigned
long
>
(),
TypedBadConversion
<
unsigned
long
>
);
EXPECT_THROW
(
Load
(
"-128"
).
as
<
unsigned
long
long
>
(),
TypedBadConversion
<
unsigned
long
long
>
);
EXPECT_THROW
(
Load
(
"-128"
).
as
<
uint8_t
>
(),
TypedBadConversion
<
unsigned
char
>
);
}
TEST
(
LoadNodeTest
,
Binary
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment