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
12364895
Commit
12364895
authored
Sep 10, 2011
by
Jesse Beder
Browse files
Implemented value events emitter
parent
50120631
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
6 deletions
+98
-6
src/value/valueevents.cpp
src/value/valueevents.cpp
+75
-5
src/value/valueevents.h
src/value/valueevents.h
+23
-1
No files found.
src/value/valueevents.cpp
View file @
12364895
#include "valueevents.h"
#include "valueevents.h"
#include "yaml-cpp/value.h"
#include "yaml-cpp/value.h"
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/mark.h"
namespace
YAML
namespace
YAML
{
{
void
ValueEvents
::
AliasManager
::
RegisterReference
(
const
detail
::
node
&
node
)
{
m_anchorByIdentity
.
insert
(
std
::
make_pair
(
node
.
ref
(),
_CreateNewAnchor
()));
}
anchor_t
ValueEvents
::
AliasManager
::
LookupAnchor
(
const
detail
::
node
&
node
)
const
{
AnchorByIdentity
::
const_iterator
it
=
m_anchorByIdentity
.
find
(
node
.
ref
());
if
(
it
==
m_anchorByIdentity
.
end
())
return
0
;
return
it
->
second
;
}
ValueEvents
::
ValueEvents
(
const
Value
&
value
)
:
m_pMemory
(
value
.
m_pMemory
),
m_root
(
*
value
.
m_pNode
)
ValueEvents
::
ValueEvents
(
const
Value
&
value
)
:
m_pMemory
(
value
.
m_pMemory
),
m_root
(
*
value
.
m_pNode
)
{
{
Visit
(
m_root
);
Setup
(
m_root
);
}
}
void
ValueEvents
::
Visit
(
const
detail
::
node
&
node
)
void
ValueEvents
::
Setup
(
const
detail
::
node
&
node
)
{
{
int
&
refCount
=
m_refCount
[
node
.
ref
()];
int
&
refCount
=
m_refCount
[
node
.
ref
()];
refCount
++
;
refCount
++
;
...
@@ -17,12 +32,67 @@ namespace YAML
...
@@ -17,12 +32,67 @@ namespace YAML
if
(
node
.
type
()
==
ValueType
::
Sequence
)
{
if
(
node
.
type
()
==
ValueType
::
Sequence
)
{
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
Visit
(
**
it
);
Setup
(
**
it
);
}
else
if
(
node
.
type
()
==
ValueType
::
Map
)
{
}
else
if
(
node
.
type
()
==
ValueType
::
Map
)
{
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
{
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
{
Visit
(
*
it
->
first
);
Setup
(
*
it
->
first
);
Visit
(
*
it
->
second
);
Setup
(
*
it
->
second
);
}
}
}
}
}
void
ValueEvents
::
Emit
(
EventHandler
&
handler
)
{
AliasManager
am
;
handler
.
OnDocumentStart
(
Mark
());
Emit
(
m_root
,
handler
,
am
);
handler
.
OnDocumentEnd
();
}
void
ValueEvents
::
Emit
(
const
detail
::
node
&
node
,
EventHandler
&
handler
,
AliasManager
&
am
)
const
{
anchor_t
anchor
=
NullAnchor
;
if
(
IsAliased
(
node
))
{
anchor
=
am
.
LookupAnchor
(
node
);
if
(
anchor
)
{
handler
.
OnAlias
(
Mark
(),
anchor
);
return
;
}
am
.
RegisterReference
(
node
);
anchor
=
am
.
LookupAnchor
(
node
);
}
switch
(
node
.
type
())
{
case
ValueType
::
Undefined
:
break
;
case
ValueType
::
Null
:
handler
.
OnNull
(
Mark
(),
anchor
);
break
;
case
ValueType
::
Scalar
:
handler
.
OnScalar
(
Mark
(),
""
,
anchor
,
node
.
scalar
());
break
;
case
ValueType
::
Sequence
:
handler
.
OnSequenceStart
(
Mark
(),
""
,
anchor
);
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
Emit
(
**
it
,
handler
,
am
);
handler
.
OnSequenceEnd
();
break
;
case
ValueType
::
Map
:
handler
.
OnMapStart
(
Mark
(),
""
,
anchor
);
for
(
detail
::
const_node_iterator
it
=
node
.
begin
();
it
!=
node
.
end
();
++
it
)
{
Emit
(
*
it
->
first
,
handler
,
am
);
Emit
(
*
it
->
second
,
handler
,
am
);
}
handler
.
OnMapEnd
();
break
;
}
}
bool
ValueEvents
::
IsAliased
(
const
detail
::
node
&
node
)
const
{
RefCount
::
const_iterator
it
=
m_refCount
.
find
(
node
.
ref
());
return
it
!=
m_refCount
.
end
()
&&
it
->
second
>
1
;
}
}
}
}
src/value/valueevents.h
View file @
12364895
...
@@ -13,14 +13,36 @@
...
@@ -13,14 +13,36 @@
namespace
YAML
namespace
YAML
{
{
class
Value
;
class
Value
;
class
EventHandler
;
class
ValueEvents
class
ValueEvents
{
{
public:
public:
ValueEvents
(
const
Value
&
value
);
ValueEvents
(
const
Value
&
value
);
void
Emit
(
EventHandler
&
handler
);
private:
class
AliasManager
{
public:
AliasManager
()
:
m_curAnchor
(
0
)
{}
void
RegisterReference
(
const
detail
::
node
&
node
);
anchor_t
LookupAnchor
(
const
detail
::
node
&
node
)
const
;
private:
private:
void
Visit
(
const
detail
::
node
&
node
);
anchor_t
_CreateNewAnchor
()
{
return
++
m_curAnchor
;
}
private:
typedef
std
::
map
<
const
detail
::
node_ref
*
,
anchor_t
>
AnchorByIdentity
;
AnchorByIdentity
m_anchorByIdentity
;
anchor_t
m_curAnchor
;
};
void
Setup
(
const
detail
::
node
&
node
);
void
Emit
(
const
detail
::
node
&
node
,
EventHandler
&
handler
,
AliasManager
&
am
)
const
;
bool
IsAliased
(
const
detail
::
node
&
node
)
const
;
private:
private:
detail
::
shared_memory_holder
m_pMemory
;
detail
::
shared_memory_holder
m_pMemory
;
...
...
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