]> git.lizzy.rs Git - minetest.git/blob - src/nameidmapping.h
Add clouds API
[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 "irrlichttypes_bloated.h"
27 #include "util/cpp11_container.h"
28
29 class NameIdMapping
30 {
31 public:
32         void serialize(std::ostream &os) const;
33         void deSerialize(std::istream &is);
34
35         void clear()
36         {
37                 m_id_to_name.clear();
38                 m_name_to_id.clear();
39         }
40
41         void set(u16 id, const std::string &name)
42         {
43                 m_id_to_name[id] = name;
44                 m_name_to_id[name] = id;
45         }
46         void removeId(u16 id)
47         {
48                 std::string name;
49                 bool found = getName(id, name);
50                 if (!found)
51                         return;
52                 m_id_to_name.erase(id);
53                 m_name_to_id.erase(name);
54         }
55         void eraseName(const std::string &name)
56         {
57                 u16 id;
58                 bool found = getId(name, id);
59                 if (!found)
60                         return;
61                 m_id_to_name.erase(id);
62                 m_name_to_id.erase(name);
63         }
64         bool getName(u16 id, std::string &result) const
65         {
66                 UNORDERED_MAP<u16, std::string>::const_iterator i;
67                 i = m_id_to_name.find(id);
68                 if (i == m_id_to_name.end())
69                         return false;
70                 result = i->second;
71                 return true;
72         }
73         bool getId(const std::string &name, u16 &result) const
74         {
75                 UNORDERED_MAP<std::string, u16>::const_iterator i;
76                 i = m_name_to_id.find(name);
77                 if (i == m_name_to_id.end())
78                         return false;
79                 result = i->second;
80                 return true;
81         }
82         u16 size() const { return m_id_to_name.size(); }
83 private:
84         UNORDERED_MAP<u16, std::string> m_id_to_name;
85         UNORDERED_MAP<std::string, u16> m_name_to_id;
86 };
87
88 #endif