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
a84c1af9
Commit
a84c1af9
authored
Jul 12, 2009
by
Jesse Beder
Browse files
Added FindValue to more easily read optional keys in a map
parent
3e0179fd
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
78 additions
and
54 deletions
+78
-54
include/node.h
include/node.h
+13
-54
include/nodeimpl.h
include/nodeimpl.h
+65
-0
No files found.
include/node.h
View file @
a84c1af9
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
#include "exceptions.h"
#include "exceptions.h"
#include "iterator.h"
#include "iterator.h"
#include "conversion.h"
#include "conversion.h"
#include "noncopyable.h"
#include <iostream>
namespace
YAML
namespace
YAML
{
{
...
@@ -16,7 +18,7 @@ namespace YAML
...
@@ -16,7 +18,7 @@ namespace YAML
enum
CONTENT_TYPE
{
CT_NONE
,
CT_SCALAR
,
CT_SEQUENCE
,
CT_MAP
};
enum
CONTENT_TYPE
{
CT_NONE
,
CT_SCALAR
,
CT_SEQUENCE
,
CT_MAP
};
class
Node
class
Node
:
private
noncopyable
{
{
public:
public:
Node
();
Node
();
...
@@ -48,11 +50,11 @@ namespace YAML
...
@@ -48,11 +50,11 @@ namespace YAML
// just for maps
// just for maps
template
<
typename
T
>
template
<
typename
T
>
const
Node
&
GetValue
(
const
T
&
key
)
const
;
const
Node
*
FindValue
(
const
T
&
key
)
const
;
const
Node
*
FindValue
(
const
char
*
key
)
const
;
template
<
typename
T
>
template
<
typename
T
>
const
Node
&
operator
[]
(
const
T
&
key
)
const
;
const
Node
&
operator
[]
(
const
T
&
key
)
const
;
const
Node
&
operator
[]
(
const
char
*
key
)
const
;
const
Node
&
operator
[]
(
const
char
*
key
)
const
;
// just for sequences
// just for sequences
...
@@ -72,11 +74,11 @@ namespace YAML
...
@@ -72,11 +74,11 @@ namespace YAML
friend
bool
operator
<
(
const
Node
&
n1
,
const
Node
&
n2
);
friend
bool
operator
<
(
const
Node
&
n1
,
const
Node
&
n2
);
private:
private:
//
shouldn't be copyable! (at least
for
now)
//
helper
for
maps
Node
(
const
Node
&
rhs
);
template
<
typename
T
>
Node
&
operator
=
(
const
Node
&
rhs
)
;
const
Node
&
GetValue
(
const
T
&
key
)
const
;
private:
// helpers for parsing
void
ParseHeader
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseHeader
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseTag
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseTag
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseAnchor
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
void
ParseAnchor
(
Scanner
*
pScanner
,
const
ParserState
&
state
);
...
@@ -90,49 +92,6 @@ namespace YAML
...
@@ -90,49 +92,6 @@ namespace YAML
const
Node
*
m_pIdentity
;
const
Node
*
m_pIdentity
;
mutable
bool
m_referenced
;
mutable
bool
m_referenced
;
};
};
// templated things we need to keep inline in the header
template
<
typename
T
>
inline
bool
Node
::
Read
(
T
&
value
)
const
{
std
::
string
scalar
;
if
(
!
GetScalar
(
scalar
))
return
false
;
return
Convert
(
scalar
,
value
);
}
template
<
typename
T
>
inline
void
operator
>>
(
const
Node
&
node
,
T
&
value
)
{
if
(
!
node
.
Read
(
value
))
throw
InvalidScalar
(
node
.
m_line
,
node
.
m_column
);
}
template
<
typename
T
>
inline
const
Node
&
Node
::
GetValue
(
const
T
&
key
)
const
{
if
(
!
m_pContent
)
throw
BadDereference
();
for
(
Iterator
it
=
begin
();
it
!=
end
();
++
it
)
{
T
t
;
if
(
it
.
first
().
Read
(
t
))
{
if
(
key
==
t
)
return
it
.
second
();
}
}
throw
MakeTypedKeyNotFound
(
m_line
,
m_column
,
key
);
}
template
<
typename
T
>
inline
const
Node
&
Node
::
operator
[]
(
const
T
&
key
)
const
{
return
GetValue
(
key
);
}
inline
const
Node
&
Node
::
operator
[]
(
const
char
*
key
)
const
{
return
GetValue
(
std
::
string
(
key
));
}
}
}
#include "nodeimpl.h"
include/nodeimpl.h
0 → 100644
View file @
a84c1af9
#pragma once
namespace
YAML
{
// implementation of templated things
template
<
typename
T
>
inline
bool
Node
::
Read
(
T
&
value
)
const
{
std
::
string
scalar
;
if
(
!
GetScalar
(
scalar
))
return
false
;
return
Convert
(
scalar
,
value
);
}
template
<
typename
T
>
inline
void
operator
>>
(
const
Node
&
node
,
T
&
value
)
{
if
(
!
node
.
Read
(
value
))
throw
InvalidScalar
(
node
.
m_line
,
node
.
m_column
);
}
template
<
typename
T
>
inline
const
Node
*
Node
::
FindValue
(
const
T
&
key
)
const
{
if
(
!
m_pContent
)
return
0
;
for
(
Iterator
it
=
begin
();
it
!=
end
();
++
it
)
{
T
t
;
if
(
it
.
first
().
Read
(
t
))
{
if
(
key
==
t
)
return
&
it
.
second
();
}
}
return
0
;
}
inline
const
Node
*
Node
::
FindValue
(
const
char
*
key
)
const
{
return
FindValue
(
std
::
string
(
key
));
}
template
<
typename
T
>
inline
const
Node
&
Node
::
GetValue
(
const
T
&
key
)
const
{
if
(
!
m_pContent
)
throw
BadDereference
();
for
(
Iterator
it
=
begin
();
it
!=
end
();
++
it
)
{
T
t
;
if
(
it
.
first
().
Read
(
t
))
{
if
(
key
==
t
)
return
it
.
second
();
}
}
throw
MakeTypedKeyNotFound
(
m_line
,
m_column
,
key
);
}
template
<
typename
T
>
inline
const
Node
&
Node
::
operator
[]
(
const
T
&
key
)
const
{
return
GetValue
(
key
);
}
inline
const
Node
&
Node
::
operator
[]
(
const
char
*
key
)
const
{
return
GetValue
(
std
::
string
(
key
));
}
}
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