minetest/src/nameidmapping.h

83 lines
2.1 KiB
C
Raw Normal View History

2011-11-16 12:03:28 +01:00
/*
2013-02-24 18:40:43 +01:00
Minetest
2013-02-24 19:38:45 +01:00
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
2011-11-16 12:03:28 +01:00
This program is free software; you can redistribute it and/or modify
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 12:03:28 +01: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
GNU Lesser General Public License for more details.
2011-11-16 12:03:28 +01:00
You should have received a copy of the GNU Lesser General Public License along
2011-11-16 12:03:28 +01:00
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef NAMEIDMAPPING_HEADER
#define NAMEIDMAPPING_HEADER
#include <string>
#include <iostream>
#include <set>
2012-06-17 03:00:31 +02:00
#include "irrlichttypes_bloated.h"
#include "util/cpp11_container.h"
2011-11-16 12:03:28 +01:00
class NameIdMapping
{
public:
void serialize(std::ostream &os) const;
void deSerialize(std::istream &is);
2011-11-16 12:03:28 +01:00
void clear(){
m_id_to_name.clear();
m_name_to_id.clear();
}
void set(u16 id, const std::string &name){
m_id_to_name[id] = name;
m_name_to_id[name] = id;
}
void removeId(u16 id){
std::string name;
bool found = getName(id, name);
if(!found) return;
m_id_to_name.erase(id);
m_name_to_id.erase(name);
}
void eraseName(const std::string &name){
u16 id;
bool found = getId(name, id);
if(!found) return;
m_id_to_name.erase(id);
m_name_to_id.erase(name);
}
bool getName(u16 id, std::string &result) const{
UNORDERED_MAP<u16, std::string>::const_iterator i;
2011-11-16 12:03:28 +01:00
i = m_id_to_name.find(id);
if(i == m_id_to_name.end())
return false;
result = i->second;
return true;
}
bool getId(const std::string &name, u16 &result) const{
UNORDERED_MAP<std::string, u16>::const_iterator i;
2011-11-16 12:03:28 +01:00
i = m_name_to_id.find(name);
if(i == m_name_to_id.end())
return false;
result = i->second;
return true;
}
u16 size() const{
return m_id_to_name.size();
}
2011-11-16 12:03:28 +01:00
private:
UNORDERED_MAP<u16, std::string> m_id_to_name;
UNORDERED_MAP<std::string, u16> m_name_to_id;
2011-11-16 12:03:28 +01:00
};
#endif