summaryrefslogtreecommitdiffstats
path: root/yaml-cpp/src/contrib/graphbuilderadapter.cpp
blob: 557e97c8929f1d46aace03abef2d469a75e8615d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "graphbuilderadapter.h"

namespace YAML
{
  int GraphBuilderAdapter::ContainerFrame::sequenceMarker;
  
  void GraphBuilderAdapter::OnNull(const Mark& mark, anchor_t anchor)
  {
    void *pParent = GetCurrentParent();
    void *pNode = m_builder.NewNull(mark, pParent);
    RegisterAnchor(anchor, pNode);
    
    DispositionNode(pNode);
  }
  
  void GraphBuilderAdapter::OnAlias(const Mark& mark, anchor_t anchor)
  {
    void *pReffedNode = m_anchors.Get(anchor);
    DispositionNode(m_builder.AnchorReference(mark, pReffedNode));
  }
  
  void GraphBuilderAdapter::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value)
  {
    void *pParent = GetCurrentParent();
    void *pNode = m_builder.NewScalar(mark, tag, pParent, value);
    RegisterAnchor(anchor, pNode);
    
    DispositionNode(pNode);
  }
  
  void GraphBuilderAdapter::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor)
  {
    void *pNode = m_builder.NewSequence(mark, tag, GetCurrentParent());
    m_containers.push(ContainerFrame(pNode));
    RegisterAnchor(anchor, pNode);
  }
  
  void GraphBuilderAdapter::OnSequenceEnd()
  {
    void *pSequence = m_containers.top().pContainer;
    m_containers.pop();
    
    DispositionNode(pSequence);
  }
  
  void GraphBuilderAdapter::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor)
  {
    void *pNode = m_builder.NewMap(mark, tag, GetCurrentParent());
    m_containers.push(ContainerFrame(pNode, m_pKeyNode));
    m_pKeyNode = NULL;
    RegisterAnchor(anchor, pNode);
  }
  
  void GraphBuilderAdapter::OnMapEnd()
  {
    void *pMap = m_containers.top().pContainer;
    m_pKeyNode = m_containers.top().pPrevKeyNode;
    m_containers.pop();
    DispositionNode(pMap);
  }
  
  void *GraphBuilderAdapter::GetCurrentParent() const
  {
    if (m_containers.empty()) {
      return NULL;
    }
    return m_containers.top().pContainer;
  }
  
  void GraphBuilderAdapter::RegisterAnchor(anchor_t anchor, void *pNode)
  {
    if (anchor) {
      m_anchors.Register(anchor, pNode);
    }
  }
  
  void GraphBuilderAdapter::DispositionNode(void *pNode)
  {
    if (m_containers.empty()) {
      m_pRootNode = pNode;
      return;
    }
    
    void *pContainer = m_containers.top().pContainer;
    if (m_containers.top().isMap()) {
      if (m_pKeyNode) {
        m_builder.AssignInMap(pContainer, m_pKeyNode, pNode);
        m_pKeyNode = NULL;
      } else {
        m_pKeyNode = pNode;
      }
    } else {
      m_builder.AppendToSequence(pContainer, pNode);
    }
  }
}