Formspecs: Add starting frame to `animated_image` (#9411)
[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 #pragma once
21
22 #include <string>
23 #include <iostream>
24 #include <set>
25 #include <unordered_map>
26 #include "irrlichttypes_bloated.h"
27
28 typedef std::unordered_map<u16, std::string> IdToNameMap;
29 typedef std::unordered_map<std::string, u16> NameToIdMap;
30
31 class NameIdMapping
32 {
33 public:
34         void serialize(std::ostream &os) const;
35         void deSerialize(std::istream &is);
36
37         void clear()
38         {
39                 m_id_to_name.clear();
40                 m_name_to_id.clear();
41         }
42
43         void set(u16 id, const std::string &name)
44         {
45                 m_id_to_name[id] = name;
46                 m_name_to_id[name] = id;
47         }
48
49         void removeId(u16 id)
50         {
51                 std::string name;
52                 bool found = getName(id, name);
53                 if (!found)
54                         return;
55                 m_id_to_name.erase(id);
56                 m_name_to_id.erase(name);
57         }
58
59         void eraseName(const std::string &name)
60         {
61                 u16 id;
62                 bool found = getId(name, id);
63                 if (!found)
64                         return;
65                 m_id_to_name.erase(id);
66                 m_name_to_id.erase(name);
67         }
68         bool getName(u16 id, std::string &result) const
69         {
70                 IdToNameMap::const_iterator i;
71                 i = m_id_to_name.find(id);
72                 if (i == m_id_to_name.end())
73                         return false;
74                 result = i->second;
75                 return true;
76         }
77         bool getId(const std::string &name, u16 &result) const
78         {
79                 NameToIdMap::const_iterator i;
80                 i = m_name_to_id.find(name);
81                 if (i == m_name_to_id.end())
82                         return false;
83                 result = i->second;
84                 return true;
85         }
86         u16 size() const { return m_id_to_name.size(); }
87
88 private:
89         IdToNameMap m_id_to_name;
90         NameToIdMap m_name_to_id;
91 };