2014-05-26 17:11:07 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-05-26 17:11:07 +10:00
|
|
|
*
|
|
|
|
* Copyright 2014 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stringid.hpp"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
using cruft::stringid;
|
2014-05-26 17:11:07 +10:00
|
|
|
|
|
|
|
|
2018-09-17 14:50:23 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-05-26 17:11:07 +10:00
|
|
|
stringid::id_t
|
2018-09-17 14:50:23 +10:00
|
|
|
stringid::operator[] (std::string_view const &key)
|
|
|
|
{
|
|
|
|
if (auto const pos = m_map.find (key); pos != m_map.cend ())
|
|
|
|
return pos->second;
|
|
|
|
|
|
|
|
|
|
|
|
auto const &[pos, success] = m_map.emplace (key, m_map.size ());
|
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
stringid::id_t
|
|
|
|
stringid::add (std::string key)
|
|
|
|
{
|
2014-05-26 17:11:07 +10:00
|
|
|
auto pos = m_map.find (key);
|
|
|
|
if (pos != m_map.end ())
|
|
|
|
throw std::invalid_argument ("duplicate stringid key");
|
|
|
|
|
|
|
|
id_t id = m_map.size ();
|
2018-07-24 15:47:21 +10:00
|
|
|
m_map.insert ({ std::move (key), id });
|
2015-04-13 18:06:08 +10:00
|
|
|
return id;
|
2014-05-26 17:11:07 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 14:50:23 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-05-26 17:11:07 +10:00
|
|
|
stringid::id_t
|
2018-09-18 16:36:03 +10:00
|
|
|
stringid::find (std::string_view const &key) const
|
2018-09-17 14:50:23 +10:00
|
|
|
{
|
2014-05-26 17:11:07 +10:00
|
|
|
auto pos = m_map.find (key);
|
|
|
|
if (pos == m_map.end ())
|
|
|
|
throw std::out_of_range ("invalid stringid key");
|
|
|
|
|
|
|
|
return pos->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-09-17 14:50:23 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2014-05-26 17:11:07 +10:00
|
|
|
void
|
2018-09-17 14:50:23 +10:00
|
|
|
stringid::clear (void)
|
|
|
|
{
|
2014-05-26 17:11:07 +10:00
|
|
|
m_map.clear ();
|
|
|
|
}
|