aliasmanager.cpp 1.08 KB
Newer Older
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
#include "aliasmanager.h"
#include "node.h"
#include <cassert>
#include <sstream>

namespace YAML
{
	AliasManager::AliasManager(): m_curAnchor(0)
	{
	}

	void AliasManager::RegisterReference(const Node& node)
	{
		const Node *pIdentity = node.Identity();
		m_newIdentityByOldIdentity.insert(std::make_pair(pIdentity, &node));
		m_anchorByIdentity.insert(std::make_pair(&node, _CreateNewAnchor()));
	}
	
	const Node *AliasManager::LookupReference(const Node& node) const
	{
		const Node *pIdentity = node.Identity();
		return _LookupReference(*pIdentity);
	}
	
	anchor_t AliasManager::LookupAnchor(const Node& node) const
	{
		AnchorByIdentity::const_iterator it = m_anchorByIdentity.find(&node);
		if(it == m_anchorByIdentity.end())
			assert(false); // TODO: throw
		return it->second;
	}

	const Node *AliasManager::_LookupReference(const Node& oldIdentity) const
	{
		NodeByNode::const_iterator it = m_newIdentityByOldIdentity.find(&oldIdentity);
		if(it == m_newIdentityByOldIdentity.end())
			return 0;
		return it->second;
	}
	
	anchor_t AliasManager::_CreateNewAnchor()
	{
		return ++m_curAnchor;
	}
}