Modernize client code (#6250)
[oweals/minetest.git] / src / nameidmapping.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef NAMEIDMAPPING_HEADER
21 #define NAMEIDMAPPING_HEADER
22
23 #include <string>
24 #include <iostream>
25 #include <set>
26 #include <unordered_map>
27 #include "irrlichttypes_bloated.h"
28
29 typedef std::unordered_map<u16, std::string> IdToNameMap;
30 typedef std::unordered_map<std::string, u16> NameToIdMap;
31
32 class NameIdMapping
33 {
34 public:
35         void serialize(std::ostream &os) const;
36         void deSerialize(std::istream &is);
37
38         void clear()
39         {
40                 m_id_to_name.clear();
41                 m_name_to_id.clear();
42         }
43
44         void set(u16 id, const std::string &name)
45         {
46                 m_id_to_name[id] = name;
47                 m_name_to_id[name] = id;
48         }
49
50         void removeId(u16 id)
51         {
52                 std::string name;
53                 bool found = getName(id, name);
54                 if (!found)
55                         return;
56                 m_id_to_name.erase(id);
57                 m_name_to_id.erase(name);
58         }
59
60         void eraseName(const std::string &name)
61         {
62                 u16 id;
63                 bool found = getId(name, id);
64                 if (!found)
65                         return;
66                 m_id_to_name.erase(id);
67                 m_name_to_id.erase(name);
68         }
69         bool getName(u16 id, std::string &result) const
70         {
71                 IdToNameMap::const_iterator i;
72                 i = m_id_to_name.find(id);
73                 if (i == m_id_to_name.end())
74                         return false;
75                 result = i->second;
76                 return true;
77         }
78         bool getId(const std::string &name, u16 &result) const
79         {
80                 NameToIdMap::const_iterator i;
81                 i = m_name_to_id.find(name);
82                 if (i == m_name_to_id.end())
83                         return false;
84                 result = i->second;
85                 return true;
86         }
87         u16 size() const { return m_id_to_name.size(); }
88
89 private:
90         IdToNameMap m_id_to_name;
91         NameToIdMap m_name_to_id;
92 };
93
94 #endif