]> git.lizzy.rs Git - minetest-m13.git/blob - src/nameidmapping.h
Update code style to C++11
[minetest-m13.git] / src / nameidmapping.h
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 <map>
27 #include "irrlichttypes.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                 m_id_to_name.clear();
37                 m_name_to_id.clear();
38         }
39         void set(u16 id, const std::string &name){
40                 m_id_to_name[id] = name;
41                 m_name_to_id[name] = id;
42         }
43         void removeId(u16 id){
44                 std::string name;
45                 bool found = getName(id, name);
46                 if(!found) return;
47                 m_id_to_name.erase(id);
48                 m_name_to_id.erase(name);
49         }
50         void eraseName(const std::string &name){
51                 u16 id;
52                 bool found = getId(name, id);
53                 if(!found) return;
54                 m_id_to_name.erase(id);
55                 m_name_to_id.erase(name);
56         }
57         bool getName(u16 id, std::string &result) const{
58                 std::map<u16, std::string>::const_iterator i;
59                 i = m_id_to_name.find(id);
60                 if(i == m_id_to_name.end())
61                         return false;
62                 result = i->second;
63                 return true;
64         }
65         bool getId(const std::string &name, u16 &result) const{
66                 std::map<std::string, u16>::const_iterator i;
67                 i = m_name_to_id.find(name);
68                 if(i == m_name_to_id.end())
69                         return false;
70                 result = i->second;
71                 return true;
72         }
73         u16 size() const{
74                 return m_id_to_name.size();
75         }
76 private:
77         std::map<u16, std::string> m_id_to_name;
78         std::map<std::string, u16> m_name_to_id;
79 };
80
81 #endif
82