Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yaml-cpp
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
yaml-cpp
Commits
f64f619c
Commit
f64f619c
authored
Sep 14, 2011
by
Jesse Beder
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added bool conversions
parent
e5d03667
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
+15
-5
src/node/convert.cpp
+78
-0
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
.
IsScalar
()
)
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
.
IsNull
()
;
}
};
...
...
@@ -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
.
IsSequence
()
)
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
.
IsSequence
()
)
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