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
4dd9f036
Commit
4dd9f036
authored
Sep 11, 2011
by
beder
Browse files
Implemented operator[] specialization, but only const (should the sequence be mutable?)
parent
e8210b47
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
11 deletions
+34
-11
include/yaml-cpp/node/detail/impl.h
include/yaml-cpp/node/detail/impl.h
+16
-9
test/new-api/nodetests.cpp
test/new-api/nodetests.cpp
+18
-2
No files found.
include/yaml-cpp/node/detail/impl.h
View file @
4dd9f036
...
...
@@ -8,29 +8,36 @@
#include "yaml-cpp/node/detail/node.h"
#include "yaml-cpp/node/detail/node_data.h"
#include <boost/type_traits.hpp>
namespace
YAML
{
namespace
detail
{
template
<
typename
Key
,
typename
Enable
=
void
>
struct
get_idx
_helper
{
static
node
*
get
(
const
Key
&
/* key */
,
shared_memory_holder
/* pMemory */
)
{
struct
get_idx
{
static
node
*
get
(
const
std
::
vector
<
node
*>&
/* sequence */
,
const
Key
&
/* key */
,
shared_memory_holder
/* pMemory */
)
{
return
0
;
}
};
template
<
typename
Key
>
struct
get_idx_helper
<
Key
,
typename
boost
::
enable_if
<
boost
::
is_integral
<
Key
>
>::
type
>
{
static
node
*
get
(
const
Key
&
key
,
shared_memory_holder
pMemory
)
{
struct
get_idx
<
Key
,
typename
boost
::
enable_if
<
boost
::
is_unsigned
<
Key
>
>::
type
>
{
static
node
*
get
(
const
std
::
vector
<
node
*>&
sequence
,
const
Key
&
key
,
shared_memory_holder
pMemory
)
{
if
(
key
<
sequence
.
size
())
return
sequence
[
key
];
return
0
;
}
};
template
<
typename
Key
>
inline
node
*
get_idx
(
const
Key
&
key
,
shared_memory_holder
pMemory
)
{
return
get_idx_helper
<
Key
>::
get
(
key
,
pMemory
);
struct
get_idx
<
Key
,
typename
boost
::
enable_if
<
boost
::
is_signed
<
Key
>
>::
type
>
{
static
node
*
get
(
const
std
::
vector
<
node
*>&
sequence
,
const
Key
&
key
,
shared_memory_holder
pMemory
)
{
if
(
key
<
0
)
return
0
;
return
get_idx
<
std
::
size_t
>::
get
(
sequence
,
static_cast
<
std
::
size_t
>
(
key
),
pMemory
);
}
};
// indexing
template
<
typename
Key
>
...
...
@@ -43,7 +50,7 @@ namespace YAML
case
NodeType
::
Null
:
return
pMemory
->
create_node
();
case
NodeType
::
Sequence
:
if
(
node
*
pNode
=
get_idx
(
key
,
pMemory
))
if
(
node
*
pNode
=
get_idx
<
Key
>::
get
(
m_sequence
,
key
,
pMemory
))
return
*
pNode
;
return
pMemory
->
create_node
();
case
NodeType
::
Scalar
:
...
...
@@ -67,7 +74,7 @@ namespace YAML
case
NodeType
::
Undefined
:
case
NodeType
::
Null
:
case
NodeType
::
Sequence
:
if
(
node
*
pNode
=
get_idx
(
key
,
pMemory
))
if
(
node
*
pNode
=
get_idx
<
Key
>::
get
(
m_sequence
,
key
,
pMemory
))
return
*
pNode
;
convert_to_map
(
pMemory
);
...
...
test/new-api/nodetests.cpp
View file @
4dd9f036
...
...
@@ -34,7 +34,7 @@ namespace Test
return
true
;
}
TEST
SimpleSequence
()
TEST
Simple
Append
Sequence
()
{
YAML
::
Node
node
;
node
.
append
(
10
);
...
...
@@ -49,6 +49,21 @@ namespace Test
return
true
;
}
TEST
SimpleAssignSequence
()
{
YAML
::
Node
node
;
node
[
0
]
=
10
;
node
[
1
]
=
"foo"
;
node
[
2
]
=
"monkey"
;
YAML_ASSERT
(
node
.
Type
()
==
YAML
::
NodeType
::
Sequence
);
YAML_ASSERT
(
node
.
size
()
==
3
);
YAML_ASSERT
(
node
[
0
].
as
<
int
>
()
==
10
);
YAML_ASSERT
(
node
[
1
].
as
<
std
::
string
>
()
==
"foo"
);
YAML_ASSERT
(
node
[
2
].
as
<
std
::
string
>
()
==
"monkey"
);
YAML_ASSERT
(
node
.
Type
()
==
YAML
::
NodeType
::
Sequence
);
return
true
;
}
TEST
SimpleMap
()
{
YAML
::
Node
node
;
...
...
@@ -113,7 +128,8 @@ namespace Test
RunNodeTest
(
&
Node
::
SimpleScalar
,
"simple scalar"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
IntScalar
,
"int scalar"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
SimpleSequence
,
"simple sequence"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
SimpleAppendSequence
,
"simple append sequence"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
SimpleAssignSequence
,
"simple assign sequence"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
SimpleMap
,
"simple map"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
MapWithUndefinedValues
,
"map with undefined values"
,
passed
,
total
);
RunNodeTest
(
&
Node
::
MapIteratorWithUndefinedValues
,
"map iterator with undefined values"
,
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