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
3126507a
Commit
3126507a
authored
Oct 16, 2010
by
rtweeks21
Browse files
- Merged latest changes from the event-api branch.
parent
55c8486e
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
135 additions
and
131 deletions
+135
-131
include/anchordict.h
include/anchordict.h
+40
-0
src/graphbuilderadapter.cpp
src/graphbuilderadapter.cpp
+2
-2
src/graphbuilderadapter.h
src/graphbuilderadapter.h
+2
-1
src/node.cpp
src/node.cpp
+4
-1
src/nodebuilder.cpp
src/nodebuilder.cpp
+44
-96
src/nodebuilder.h
src/nodebuilder.h
+21
-27
src/parser.cpp
src/parser.cpp
+1
-1
src/ptr_stack.h
src/ptr_stack.h
+21
-3
No files found.
include/anchordict.h
0 → 100644
View file @
3126507a
#pragma once
#ifndef ANCHORDICT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define ANCHORDICT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include <vector>
#include "anchor.h"
namespace
YAML
{
/// AnchorDict
/// . An object that stores and retrieves values correlating to anchor_t
/// values.
/// . Efficient implementation that can make assumptions about how anchor_t
/// values are assigned by the Parser class.
template
<
class
T
>
class
AnchorDict
{
public:
void
Register
(
anchor_t
anchor
,
T
value
)
{
if
(
anchor
>
m_data
.
size
())
{
m_data
.
resize
(
anchor
);
}
m_data
[
anchor
-
1
]
=
value
;
}
T
Get
(
anchor_t
anchor
)
const
{
return
m_data
[
anchor
-
1
];
}
private:
std
::
vector
<
T
>
m_data
;
};
}
#endif // ANCHORDICT_H_62B23520_7C8E_11DE_8A39_0800200C9A66
src/graphbuilderadapter.cpp
View file @
3126507a
...
@@ -15,7 +15,7 @@ namespace YAML
...
@@ -15,7 +15,7 @@ namespace YAML
void
GraphBuilderAdapter
::
OnAlias
(
const
Mark
&
mark
,
anchor_t
anchor
)
void
GraphBuilderAdapter
::
OnAlias
(
const
Mark
&
mark
,
anchor_t
anchor
)
{
{
void
*
pReffedNode
=
m_anchors
[
anchor
]
;
void
*
pReffedNode
=
m_anchors
.
Get
(
anchor
)
;
DispositionNode
(
m_builder
.
AnchorReference
(
mark
,
pReffedNode
));
DispositionNode
(
m_builder
.
AnchorReference
(
mark
,
pReffedNode
));
}
}
...
@@ -70,7 +70,7 @@ namespace YAML
...
@@ -70,7 +70,7 @@ namespace YAML
void
GraphBuilderAdapter
::
RegisterAnchor
(
anchor_t
anchor
,
void
*
pNode
)
void
GraphBuilderAdapter
::
RegisterAnchor
(
anchor_t
anchor
,
void
*
pNode
)
{
{
if
(
anchor
)
{
if
(
anchor
)
{
m_anchors
[
anchor
]
=
pNode
;
m_anchors
.
Register
(
anchor
,
pNode
)
;
}
}
}
}
...
...
src/graphbuilderadapter.h
View file @
3126507a
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
#include <map>
#include <map>
#include <stack>
#include <stack>
#include "anchordict.h"
#include "eventhandler.h"
#include "eventhandler.h"
#include "graphbuilder.h"
#include "graphbuilder.h"
...
@@ -50,7 +51,7 @@ namespace YAML
...
@@ -50,7 +51,7 @@ namespace YAML
static
int
sequenceMarker
;
static
int
sequenceMarker
;
};
};
typedef
std
::
stack
<
ContainerFrame
>
ContainerStack
;
typedef
std
::
stack
<
ContainerFrame
>
ContainerStack
;
typedef
std
::
map
<
anchor_t
,
void
*>
AnchorMap
;
typedef
AnchorDict
<
void
*>
AnchorMap
;
GraphBuilderInterface
&
m_builder
;
GraphBuilderInterface
&
m_builder
;
ContainerStack
m_containers
;
ContainerStack
m_containers
;
...
...
src/node.cpp
View file @
3126507a
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include "emitfromevents.h"
#include "emitfromevents.h"
#include "emitter.h"
#include "emitter.h"
#include "eventhandler.h"
#include "eventhandler.h"
#include "graphbuilderadapter.h"
#include "iterpriv.h"
#include "iterpriv.h"
#include "map.h"
#include "map.h"
#include "nodebuilder.h"
#include "nodebuilder.h"
...
@@ -47,7 +48,9 @@ namespace YAML
...
@@ -47,7 +48,9 @@ namespace YAML
{
{
std
::
auto_ptr
<
Node
>
pNode
(
new
Node
);
std
::
auto_ptr
<
Node
>
pNode
(
new
Node
);
NodeBuilder
nodeBuilder
(
*
pNode
);
NodeBuilder
nodeBuilder
(
*
pNode
);
EmitEvents
(
nodeBuilder
);
GraphBuilder
<
NodeBuilder
>
graphBuilder
(
nodeBuilder
);
GraphBuilderAdapter
eventHandler
(
graphBuilder
);
EmitEvents
(
eventHandler
);
return
pNode
;
return
pNode
;
}
}
...
...
src/nodebuilder.cpp
View file @
3126507a
...
@@ -6,137 +6,85 @@
...
@@ -6,137 +6,85 @@
namespace
YAML
namespace
YAML
{
{
NodeBuilder
::
NodeBuilder
(
Node
&
root
)
:
m_root
(
root
),
m_initializedRoot
(
false
)
,
m_finished
(
false
)
NodeBuilder
::
NodeBuilder
(
Node
&
root
)
:
m_root
(
root
),
m_initializedRoot
(
false
)
{
{
m_root
.
Clear
();
m_root
.
Clear
();
m_anchors
.
push_back
(
0
);
// since the anchors start at 1
}
}
NodeBuilder
::~
NodeBuilder
()
NodeBuilder
::
NodeBuilder
(
const
NodeBuilder
&
o
)
:
m_root
(
o
.
m_root
),
m_initializedRoot
(
o
.
m_initializedRoot
)
{
}
void
NodeBuilder
::
OnDocumentStart
(
const
Mark
&
)
{
}
void
NodeBuilder
::
OnDocumentEnd
()
{
{
assert
(
m_finished
);
}
}
void
NodeBuilder
::
OnNull
(
const
std
::
string
&
tag
,
anchor_t
anchor
)
NodeBuilder
::
~
NodeBuilder
(
)
{
{
Node
&
node
=
Push
(
anchor
);
node
.
InitNull
(
tag
);
Pop
();
}
}
void
NodeBuilder
::
OnAlias
(
const
Mark
&
mark
,
anchor_t
anchor
)
Node
*
NodeBuilder
::
NewNull
(
const
std
::
string
&
tag
,
Node
*
pParent
)
{
{
Node
&
node
=
Push
();
(
void
)
pParent
;
node
.
InitAlias
(
mark
,
*
m_anchors
[
anchor
]);
Node
*
pNode
=
NewNode
();
Pop
();
pNode
->
InitNull
(
tag
);
return
pNode
;
}
}
void
NodeBuilder
::
OnScalar
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
,
const
std
::
string
&
valu
e
)
Node
*
NodeBuilder
::
AnchorReference
(
const
Mark
&
mark
,
Node
*
pNod
e
)
{
{
Node
&
node
=
Push
(
anchor
);
Node
*
pAlias
=
NewNode
();
node
.
Init
(
CT_SCALAR
,
mark
,
tag
);
pAlias
->
InitAlias
(
mark
,
*
pNode
);
node
.
SetData
(
value
);
return
pAlias
;
Pop
();
}
}
void
NodeBuilder
::
OnSequenceSt
ar
t
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
)
Node
*
NodeBuilder
::
NewScal
ar
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
,
const
std
::
string
&
value
)
{
{
Node
&
node
=
Push
(
anchor
);
(
void
)
pParent
;
node
.
Init
(
CT_SEQUENCE
,
mark
,
tag
);
Node
*
pNode
=
NewNode
();
pNode
->
Init
(
CT_SCALAR
,
mark
,
tag
);
pNode
->
SetData
(
value
);
return
pNode
;
}
}
void
NodeBuilder
::
On
Sequence
End
(
)
NodeBuilder
::
Sequence
*
NodeBuilder
::
New
Sequence
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
)
{
{
Pop
();
(
void
)
pParent
;
Node
*
pNode
=
NewNode
();
pNode
->
Init
(
CT_SEQUENCE
,
mark
,
tag
);
return
pNode
;
}
}
void
NodeBuilder
::
OnMapStart
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
)
void
NodeBuilder
::
AppendToSequence
(
Sequence
*
pSequence
,
Node
*
pNode
)
{
{
Node
&
node
=
Push
(
anchor
);
std
::
auto_ptr
<
Node
>
apNode
(
m_unlinked
.
pop
(
pNode
)
);
node
.
Init
(
CT_MAP
,
mark
,
tag
);
assert
(
apNode
.
get
()
);
m_didPushKey
.
push
(
fals
e
);
pSequence
->
Append
(
apNod
e
);
}
}
void
NodeBuilder
::
OnMapEnd
(
)
NodeBuilder
::
Map
*
NodeBuilder
::
NewMap
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
)
{
{
m_didPushKey
.
pop
();
(
void
)
pParent
;
Pop
();
Node
*
pNode
=
NewNode
();
pNode
->
Init
(
CT_MAP
,
mark
,
tag
);
return
pNode
;
}
}
Node
&
NodeBuilder
::
Push
(
anchor_t
anchor
)
void
NodeBuilder
::
AssignInMap
(
Map
*
pMap
,
Node
*
pKeyNode
,
Node
*
pValueNode
)
{
{
Node
&
node
=
Push
();
std
::
auto_ptr
<
Node
>
apKeyNode
(
m_unlinked
.
pop
(
pKeyNode
));
RegisterAnchor
(
anchor
,
node
);
std
::
auto_ptr
<
Node
>
apValueNode
(
m_unlinked
.
pop
(
pValueNode
));
return
node
;
assert
(
apKeyNode
.
get
()
&&
apValueNode
.
get
());
pMap
->
Insert
(
apKeyNode
,
apValueNode
);
}
}
Node
&
NodeBuilder
::
Push
()
Node
*
NodeBuilder
::
NewNode
()
{
{
if
(
!
m_initializedRoot
)
{
if
(
!
m_initializedRoot
)
{
m_initializedRoot
=
true
;
m_initializedRoot
=
true
;
return
m_root
;
return
&
m_root
;
}
}
std
::
auto_ptr
<
Node
>
pNode
(
new
Node
);
std
::
auto_ptr
<
Node
>
pNode
(
new
Node
);
m_stack
.
push
(
pNode
);
Node
*
pResult
=
pNode
.
get
();
return
m_stack
.
top
();
// Save the pointer in a collection that will free it on exception
}
m_unlinked
.
push
(
pNode
);
return
pResult
;
Node
&
NodeBuilder
::
Top
()
{
return
m_stack
.
empty
()
?
m_root
:
m_stack
.
top
();
}
void
NodeBuilder
::
Pop
()
{
assert
(
!
m_finished
);
if
(
m_stack
.
empty
())
{
m_finished
=
true
;
return
;
}
std
::
auto_ptr
<
Node
>
pNode
=
m_stack
.
pop
();
Insert
(
pNode
);
}
void
NodeBuilder
::
Insert
(
std
::
auto_ptr
<
Node
>
pNode
)
{
Node
&
node
=
Top
();
switch
(
node
.
GetType
())
{
case
CT_SEQUENCE
:
node
.
Append
(
pNode
);
break
;
case
CT_MAP
:
assert
(
!
m_didPushKey
.
empty
());
if
(
m_didPushKey
.
top
())
{
assert
(
!
m_pendingKeys
.
empty
());
std
::
auto_ptr
<
Node
>
pKey
=
m_pendingKeys
.
pop
();
node
.
Insert
(
pKey
,
pNode
);
m_didPushKey
.
top
()
=
false
;
}
else
{
m_pendingKeys
.
push
(
pNode
);
m_didPushKey
.
top
()
=
true
;
}
break
;
default:
assert
(
false
);
}
}
void
NodeBuilder
::
RegisterAnchor
(
anchor_t
anchor
,
const
Node
&
node
)
{
if
(
anchor
)
{
assert
(
anchor
==
m_anchors
.
size
());
m_anchors
.
push_back
(
&
node
);
}
}
}
}
}
src/nodebuilder.h
View file @
3126507a
...
@@ -3,55 +3,49 @@
...
@@ -3,55 +3,49 @@
#ifndef NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#ifndef NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define NODEBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include "eventhandler.h"
#include "ptr_stack.h"
#include <map>
#include <map>
#include <memory>
#include <memory>
#include <stack>
#include <stack>
#include <string>
#include "ptr_stack.h"
namespace
YAML
namespace
YAML
{
{
class
Node
;
class
Node
;
class
Mark
;
class
NodeBuilder
:
public
EventHandler
class
NodeBuilder
{
{
public:
public:
typedef
YAML
::
Node
Node
;
typedef
YAML
::
Node
Map
;
typedef
YAML
::
Node
Sequence
;
explicit
NodeBuilder
(
Node
&
root
);
explicit
NodeBuilder
(
Node
&
root
);
NodeBuilder
(
const
NodeBuilder
&
o
);
virtual
~
NodeBuilder
();
virtual
~
NodeBuilder
();
virtual
void
OnDocumentStart
(
const
Mark
&
mark
);
Node
*
NewNull
(
const
std
::
string
&
tag
,
Node
*
pParent
);
virtual
void
OnDocumentEnd
();
Node
*
AnchorReference
(
const
Mark
&
mark
,
Node
*
pNode
);
Node
*
NewScalar
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
,
const
std
::
string
&
value
);
virtual
void
OnNull
(
const
std
::
string
&
tag
,
anchor_t
anchor
);
Sequence
*
NewSequence
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
);
v
irtual
void
OnAlias
(
const
Mark
&
mark
,
anchor_t
anchor
);
v
oid
AppendToSequence
(
Sequence
*
pSequence
,
Node
*
pNode
);
v
irtual
void
OnScalar
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
,
const
std
::
string
&
value
);
v
oid
SequenceComplete
(
Sequence
*
pSequence
)
{(
void
)
pSequence
;}
virtual
void
OnSequenceStart
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
);
Map
*
NewMap
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
Node
*
pParent
);
virtual
void
OnSequenceEnd
();
void
AssignInMap
(
Map
*
pMap
,
Node
*
pKeyNode
,
Node
*
pValueNode
);
void
MapComplete
(
Map
*
pMap
)
{(
void
)
pMap
;}
virtual
void
OnMapStart
(
const
Mark
&
mark
,
const
std
::
string
&
tag
,
anchor_t
anchor
);
virtual
void
OnMapEnd
();
private:
private:
Node
&
Push
(
anchor_t
anchor
);
Node
*
NewNode
();
Node
&
Push
();
Node
&
Top
();
void
Pop
();
void
Insert
(
std
::
auto_ptr
<
Node
>
pNode
);
void
RegisterAnchor
(
anchor_t
anchor
,
const
Node
&
node
);
private:
private:
Node
&
m_root
;
Node
&
m_root
;
bool
m_initializedRoot
;
bool
m_initializedRoot
;
bool
m_finished
;
ptr_stack
<
Node
>
m_stack
;
ptr_stack
<
Node
>
m_pendingKeys
;
std
::
stack
<
bool
>
m_didPushKey
;
typedef
std
::
vector
<
const
Node
*>
Anchors
;
ptr_stack
<
Node
>
m_unlinked
;
Anchors
m_anchors
;
};
};
}
}
...
...
src/parser.cpp
View file @
3126507a
...
@@ -75,7 +75,7 @@ namespace YAML
...
@@ -75,7 +75,7 @@ namespace YAML
bool
Parser
::
GetNextDocument
(
Node
&
document
)
bool
Parser
::
GetNextDocument
(
Node
&
document
)
{
{
NodeBuilder
builder
(
document
);
NodeBuilder
builder
(
document
);
return
Handle
NextDocument
(
builder
);
return
Build
NextDocument
Graph
(
builder
);
}
}
// ParseDirectives
// ParseDirectives
...
...
src/ptr_stack.h
View file @
3126507a
...
@@ -3,11 +3,14 @@
...
@@ -3,11 +3,14 @@
#ifndef PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#ifndef PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define PTR_STACK_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#include <algorithm>
#include <memory>
#include <memory>
#include <vector>
#include <vector>
#include "noncopyable.h"
template
<
typename
T
>
template
<
typename
T
>
class
ptr_stack
class
ptr_stack
:
private
YAML
::
noncopyable
{
{
public:
public:
ptr_stack
()
{}
ptr_stack
()
{}
...
@@ -22,13 +25,28 @@ public:
...
@@ -22,13 +25,28 @@ public:
std
::
size_t
size
()
const
{
return
m_data
.
size
();
}
std
::
size_t
size
()
const
{
return
m_data
.
size
();
}
bool
empty
()
const
{
return
m_data
.
empty
();
}
bool
empty
()
const
{
return
m_data
.
empty
();
}
void
push
(
std
::
auto_ptr
<
T
>
t
)
{
m_data
.
push_back
(
t
.
release
());
}
void
push
(
std
::
auto_ptr
<
T
>
t
)
{
// Make sure that the space is available before releasing the
// auto_ptr to it. NULL can be deleted safely.
m_data
.
push_back
(
NULL
);
m_data
.
back
()
=
t
.
release
();
}
std
::
auto_ptr
<
T
>
pop
()
{
std
::
auto_ptr
<
T
>
pop
()
{
std
::
auto_ptr
<
T
>
t
(
m_data
.
back
());
std
::
auto_ptr
<
T
>
t
(
m_data
.
back
());
m_data
.
pop_back
();
m_data
.
pop_back
();
return
t
;
return
t
;
}
}
T
&
top
()
{
return
*
m_data
.
back
();
}
std
::
auto_ptr
<
T
>
pop
(
T
*
val
)
{
typename
std
::
vector
<
T
*>::
reverse_iterator
itVal
=
std
::
find
(
m_data
.
rbegin
(),
m_data
.
rend
(),
val
);
std
::
auto_ptr
<
T
>
t
;
if
(
itVal
!=
m_data
.
rend
())
{
t
.
reset
(
*
itVal
);
m_data
.
erase
((
itVal
+
1
).
base
());
}
return
t
;
}
T
&
top
()
const
{
return
*
m_data
.
back
();
}
private:
private:
std
::
vector
<
T
*>
m_data
;
std
::
vector
<
T
*>
m_data
;
...
...
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