Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
nameidmapping.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <string>
8#include <iostream>
9#include <unordered_map>
10#include <cassert>
11#include "irrlichttypes.h"
12
13typedef std::unordered_map<u16, std::string> IdToNameMap;
14typedef std::unordered_map<std::string, u16> NameToIdMap;
15
17{
18public:
19 void serialize(std::ostream &os) const;
20 void deSerialize(std::istream &is);
21
22 void clear()
23 {
24 m_id_to_name.clear();
25 m_name_to_id.clear();
26 }
27
28 void set(u16 id, const std::string &name)
29 {
30 assert(!name.empty());
31 m_id_to_name[id] = name;
32 m_name_to_id[name] = id;
33 }
34
35 void removeId(u16 id)
36 {
37 std::string name;
38 bool found = getName(id, name);
39 if (!found)
40 return;
41 m_id_to_name.erase(id);
42 m_name_to_id.erase(name);
43 }
44
45 void eraseName(const std::string &name)
46 {
47 u16 id;
48 bool found = getId(name, id);
49 if (!found)
50 return;
51 m_id_to_name.erase(id);
52 m_name_to_id.erase(name);
53 }
54 bool getName(u16 id, std::string &result) const
55 {
56 auto i = m_id_to_name.find(id);
57 if (i == m_id_to_name.end())
58 return false;
59 result = i->second;
60 return true;
61 }
62 bool getId(const std::string &name, u16 &result) const
63 {
64 auto i = m_name_to_id.find(name);
65 if (i == m_name_to_id.end())
66 return false;
67 result = i->second;
68 return true;
69 }
70 u16 size() const { return m_id_to_name.size(); }
71
72private:
75};
Definition nameidmapping.h:17
void eraseName(const std::string &name)
Definition nameidmapping.h:45
void set(u16 id, const std::string &name)
Definition nameidmapping.h:28
void serialize(std::ostream &os) const
Definition nameidmapping.cpp:9
void clear()
Definition nameidmapping.h:22
u16 size() const
Definition nameidmapping.h:70
bool getName(u16 id, std::string &result) const
Definition nameidmapping.h:54
void removeId(u16 id)
Definition nameidmapping.h:35
NameToIdMap m_name_to_id
Definition nameidmapping.h:74
void deSerialize(std::istream &is)
Definition nameidmapping.cpp:19
bool getId(const std::string &name, u16 &result) const
Definition nameidmapping.h:62
IdToNameMap m_id_to_name
Definition nameidmapping.h:73
std::unordered_map< std::string, u16 > NameToIdMap
Definition nameidmapping.h:14
std::unordered_map< u16, std::string > IdToNameMap
Definition nameidmapping.h:13