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
f64f619c
Commit
f64f619c
authored
Sep 14, 2011
by
Jesse Beder
Browse files
Added bool conversions
parent
e5d03667
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
103 additions
and
5 deletions
+103
-5
include/yaml-cpp/node/convert.h
include/yaml-cpp/node/convert.h
+15
-5
src/node/convert.cpp
src/node/convert.cpp
+78
-0
test/new-api/nodetests.cpp
test/new-api/nodetests.cpp
+10
-0
No files found.
include/yaml-cpp/node/convert.h
View file @
f64f619c
...
...
@@ -24,7 +24,7 @@ namespace YAML
}
static
bool
decode
(
const
Node
&
node
,
std
::
string
&
rhs
)
{
if
(
node
.
Type
()
!=
NodeType
::
Scalar
)
if
(
!
node
.
Is
Scalar
()
)
return
false
;
rhs
=
node
.
Scalar
();
return
true
;
...
...
@@ -38,7 +38,7 @@ namespace YAML
}
static
bool
decode
(
const
Node
&
node
,
_Null
&
/* rhs */
)
{
return
node
.
Type
()
==
NodeType
::
Null
;
return
node
.
Is
Null
()
;
}
};
...
...
@@ -77,6 +77,16 @@ namespace YAML
YAML_DEFINE_CONVERT_STREAMABLE
(
long
double
);
#undef YAML_DEFINE_CONVERT_STREAMABLE
// bool
template
<
>
struct
convert
<
bool
>
{
static
Node
encode
(
bool
rhs
)
{
return
rhs
?
Node
(
"true"
)
:
Node
(
"false"
);
}
static
bool
decode
(
const
Node
&
node
,
bool
&
rhs
);
};
// std::map
template
<
typename
K
,
typename
V
>
...
...
@@ -89,7 +99,7 @@ namespace YAML
}
static
bool
decode
(
const
Node
&
node
,
std
::
map
<
K
,
V
>&
rhs
)
{
if
(
node
.
Type
()
!=
NodeType
::
Map
)
if
(
!
node
.
IsMap
()
)
return
false
;
rhs
.
clear
();
...
...
@@ -110,7 +120,7 @@ namespace YAML
}
static
bool
decode
(
const
Node
&
node
,
std
::
vector
<
T
>&
rhs
)
{
if
(
node
.
Type
()
!=
NodeType
::
Sequence
)
if
(
!
node
.
Is
Sequence
()
)
return
false
;
rhs
.
clear
();
...
...
@@ -131,7 +141,7 @@ namespace YAML
}
static
bool
decode
(
const
Node
&
node
,
std
::
list
<
T
>&
rhs
)
{
if
(
node
.
Type
()
!=
NodeType
::
Sequence
)
if
(
!
node
.
Is
Sequence
()
)
return
false
;
rhs
.
clear
();
...
...
src/node/convert.cpp
View file @
f64f619c
#include "yaml-cpp/node/convert.h"
#include "yaml-cpp/node/impl.h"
#include <algorithm>
namespace
{
// we're not gonna mess with the mess that is all the isupper/etc. functions
bool
IsLower
(
char
ch
)
{
return
'a'
<=
ch
&&
ch
<=
'z'
;
}
bool
IsUpper
(
char
ch
)
{
return
'A'
<=
ch
&&
ch
<=
'Z'
;
}
char
ToLower
(
char
ch
)
{
return
IsUpper
(
ch
)
?
ch
+
'a'
-
'A'
:
ch
;
}
std
::
string
tolower
(
const
std
::
string
&
str
)
{
std
::
string
s
(
str
);
std
::
transform
(
s
.
begin
(),
s
.
end
(),
s
.
begin
(),
ToLower
);
return
s
;
}
template
<
typename
T
>
bool
IsEntirely
(
const
std
::
string
&
str
,
T
func
)
{
for
(
std
::
size_t
i
=
0
;
i
<
str
.
size
();
i
++
)
if
(
!
func
(
str
[
i
]))
return
false
;
return
true
;
}
// IsFlexibleCase
// . Returns true if 'str' is:
// . UPPERCASE
// . lowercase
// . Capitalized
bool
IsFlexibleCase
(
const
std
::
string
&
str
)
{
if
(
str
.
empty
())
return
true
;
if
(
IsEntirely
(
str
,
IsLower
))
return
true
;
bool
firstcaps
=
IsUpper
(
str
[
0
]);
std
::
string
rest
=
str
.
substr
(
1
);
return
firstcaps
&&
(
IsEntirely
(
rest
,
IsLower
)
||
IsEntirely
(
rest
,
IsUpper
));
}
}
namespace
YAML
{
bool
convert
<
bool
>::
decode
(
const
Node
&
node
,
bool
&
rhs
)
{
if
(
!
node
.
IsScalar
())
return
false
;
// we can't use iostream bool extraction operators as they don't
// recognize all possible values in the table below (taken from
// http://yaml.org/type/bool.html)
static
const
struct
{
std
::
string
truename
,
falsename
;
}
names
[]
=
{
{
"y"
,
"n"
},
{
"yes"
,
"no"
},
{
"true"
,
"false"
},
{
"on"
,
"off"
},
};
if
(
!
IsFlexibleCase
(
node
.
Scalar
()))
return
false
;
for
(
unsigned
i
=
0
;
i
<
sizeof
(
names
)
/
sizeof
(
names
[
0
]);
i
++
)
{
if
(
names
[
i
].
truename
==
tolower
(
node
.
Scalar
()))
{
rhs
=
true
;
return
true
;
}
if
(
names
[
i
].
falsename
==
tolower
(
node
.
Scalar
()))
{
rhs
=
false
;
return
true
;
}
}
return
false
;
}
}
test/new-api/nodetests.cpp
View file @
f64f619c
...
...
@@ -259,6 +259,15 @@ namespace Test
YAML_ASSERT
(
node
[
"other"
]
==
node
[
"key"
]);
return
true
;
}
TEST
Bool
()
{
YAML
::
Node
node
;
node
[
true
]
=
false
;
YAML_ASSERT
(
node
.
IsMap
());
YAML_ASSERT
(
node
[
true
].
as
<
bool
>
()
==
false
);
return
true
;
}
}
void
RunNodeTest
(
TEST
(
*
test
)(),
const
std
::
string
&
name
,
int
&
passed
,
int
&
total
)
{
...
...
@@ -302,6 +311,7 @@ namespace Test
RunNodeTest
(
&
Node
::
SelfReferenceMap
,
"self reference map"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
TempMapVariable
,
"temp map variable"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
TempMapVariableAlias
,
"temp map variable alias"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
Bool
,
"bool"
,
passed
,
total
);
std
::
cout
<<
"Node tests: "
<<
passed
<<
"/"
<<
total
<<
" passed
\n
"
;
return
passed
==
total
;
...
...
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