2011-11-16 03:03:28 -08:00
|
|
|
/*
|
2013-02-24 09:40:43 -08:00
|
|
|
Minetest
|
2013-02-24 10:38:45 -08:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-11-16 03:03:28 -08:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
2012-06-05 07:56:56 -07:00
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-11-16 03:03:28 -08:00
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-06-05 07:56:56 -07:00
|
|
|
GNU Lesser General Public License for more details.
|
2011-11-16 03:03:28 -08:00
|
|
|
|
2012-06-05 07:56:56 -07:00
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
2011-11-16 03:03:28 -08:00
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 13:19:39 -07:00
|
|
|
#pragma once
|
2011-11-16 03:03:28 -08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
2017-06-04 12:00:04 -07:00
|
|
|
#include <unordered_map>
|
2012-06-16 18:00:31 -07:00
|
|
|
#include "irrlichttypes_bloated.h"
|
2017-06-04 12:00:04 -07:00
|
|
|
|
|
|
|
typedef std::unordered_map<u16, std::string> IdToNameMap;
|
|
|
|
typedef std::unordered_map<std::string, u16> NameToIdMap;
|
2011-11-16 03:03:28 -08:00
|
|
|
|
|
|
|
class NameIdMapping
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void serialize(std::ostream &os) const;
|
|
|
|
void deSerialize(std::istream &is);
|
2016-08-10 03:08:05 -07:00
|
|
|
|
2017-03-25 11:12:18 -07:00
|
|
|
void clear()
|
|
|
|
{
|
2011-11-16 03:03:28 -08:00
|
|
|
m_id_to_name.clear();
|
|
|
|
m_name_to_id.clear();
|
|
|
|
}
|
2017-03-25 11:12:18 -07:00
|
|
|
|
2017-04-23 00:52:40 -07:00
|
|
|
void set(u16 id, const std::string &name)
|
|
|
|
{
|
2011-11-16 03:03:28 -08:00
|
|
|
m_id_to_name[id] = name;
|
|
|
|
m_name_to_id[name] = id;
|
|
|
|
}
|
2017-06-04 12:00:04 -07:00
|
|
|
|
2017-04-23 00:52:40 -07:00
|
|
|
void removeId(u16 id)
|
|
|
|
{
|
2011-11-16 03:03:28 -08:00
|
|
|
std::string name;
|
|
|
|
bool found = getName(id, name);
|
2017-04-23 00:52:40 -07:00
|
|
|
if (!found)
|
|
|
|
return;
|
2011-11-16 03:03:28 -08:00
|
|
|
m_id_to_name.erase(id);
|
|
|
|
m_name_to_id.erase(name);
|
|
|
|
}
|
2017-06-18 10:55:15 -07:00
|
|
|
|
2017-04-23 00:52:40 -07:00
|
|
|
void eraseName(const std::string &name)
|
|
|
|
{
|
2011-11-16 03:03:28 -08:00
|
|
|
u16 id;
|
|
|
|
bool found = getId(name, id);
|
2017-04-23 00:52:40 -07:00
|
|
|
if (!found)
|
|
|
|
return;
|
2011-11-16 03:03:28 -08:00
|
|
|
m_id_to_name.erase(id);
|
|
|
|
m_name_to_id.erase(name);
|
|
|
|
}
|
2017-04-23 00:52:40 -07:00
|
|
|
bool getName(u16 id, std::string &result) const
|
|
|
|
{
|
2017-06-04 12:00:04 -07:00
|
|
|
IdToNameMap::const_iterator i;
|
2011-11-16 03:03:28 -08:00
|
|
|
i = m_id_to_name.find(id);
|
2017-04-23 00:52:40 -07:00
|
|
|
if (i == m_id_to_name.end())
|
2011-11-16 03:03:28 -08:00
|
|
|
return false;
|
|
|
|
result = i->second;
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-23 00:52:40 -07:00
|
|
|
bool getId(const std::string &name, u16 &result) const
|
|
|
|
{
|
2017-06-04 12:00:04 -07:00
|
|
|
NameToIdMap::const_iterator i;
|
2011-11-16 03:03:28 -08:00
|
|
|
i = m_name_to_id.find(name);
|
2017-04-23 00:52:40 -07:00
|
|
|
if (i == m_name_to_id.end())
|
2011-11-16 03:03:28 -08:00
|
|
|
return false;
|
|
|
|
result = i->second;
|
|
|
|
return true;
|
|
|
|
}
|
2017-04-23 00:52:40 -07:00
|
|
|
u16 size() const { return m_id_to_name.size(); }
|
2017-05-26 08:03:46 -07:00
|
|
|
|
2011-11-16 03:03:28 -08:00
|
|
|
private:
|
2017-06-04 12:00:04 -07:00
|
|
|
IdToNameMap m_id_to_name;
|
|
|
|
NameToIdMap m_name_to_id;
|
2011-11-16 03:03:28 -08:00
|
|
|
};
|