summaryrefslogtreecommitdiff
path: root/src/nameidmapping.h
blob: 3acb43ef120db7b0bed345872a008400f4c8127a (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
/*
Minetest
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include <string>
#include <iostream>
#include <set>
#include <unordered_map>
#include "irrlichttypes_bloated.h"

typedef std::unordered_map<u16, std::string> IdToNameMap;
typedef std::unordered_map<std::string, u16> NameToIdMap;

class NameIdMapping
{
public:
	void serialize(std::ostream &os) const;
	void deSerialize(std::istream &is);

	void clear()
	{
		m_id_to_name.clear();
		m_name_to_id.clear();
	}

	void set(u16 id, const std::string &name)
	{
		m_id_to_name[id] = name;
		m_name_to_id[name] = id;
	}

	void removeId(u16 id)
	{
		std::string name;
		bool found = getName(id, name);
		if (!found)
			return;
		m_id_to_name.erase(id);
		m_name_to_id.erase(name);
	}

	void eraseName(const std::string &name)
	{
		u16 id;
		bool found = getId(name, id);
		if (!found)
			return;
		m_id_to_name.erase(id);
		m_name_to_id.erase(name);
	}
	bool getName(u16 id, std::string &result) const
	{
		IdToNameMap::const_iterator i;
		i = m_id_to_name.find(id);
		if (i == m_id_to_name.end())
			return false;
		result = i->second;
		return true;
	}
	bool getId(const std::string &name, u16 &result) const
	{
		NameToIdMap::const_iterator i;
		i = m_name_to_id.find(name);
		if (i == m_name_to_id.end())
			return false;
		result = i->second;
		return true;
	}
	u16 size() const { return m_id_to_name.size(); }

private:
	IdToNameMap m_id_to_name;
	NameToIdMap m_name_to_id;
};