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
|
|
|
*
|
2018-01-13 13:48:58 +11:00
|
|
|
* Copyright 2014-2018 Danny Robson <danny@nerdcruft.net>
|
2014-05-26 17:11:07 +10:00
|
|
|
*/
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
#ifndef CRUFT_UTIL_STRINGID_HPP
|
|
|
|
#define CRUFT_UTIL_STRINGID_HPP
|
|
|
|
|
|
|
|
#include "view.hpp"
|
2014-05-26 17:11:07 +10:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <string>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft {
|
2014-05-26 17:11:07 +10:00
|
|
|
class stringid {
|
2018-01-13 13:48:58 +11:00
|
|
|
public:
|
|
|
|
typedef size_t id_t;
|
|
|
|
|
2018-09-17 14:50:23 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/// Returns the ID for the supplied string, or creates and returns a
|
|
|
|
/// new ID if the string is not present.
|
|
|
|
id_t operator[] (std::string_view const&);
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
id_t add (std::string);
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
id_t add (cruft::view<T> key)
|
2018-01-13 13:48:58 +11:00
|
|
|
{
|
|
|
|
return add (
|
|
|
|
std::string{
|
|
|
|
std::cbegin (key),
|
|
|
|
std::cend (key)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
id_t find (const std::string&) const;
|
|
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2018-08-05 14:42:02 +10:00
|
|
|
id_t find (cruft::view<T> key) const
|
2018-01-13 13:48:58 +11:00
|
|
|
{
|
|
|
|
return find (
|
|
|
|
std::string {
|
|
|
|
std::cbegin (key),
|
|
|
|
std::cend (key)
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-05-26 17:11:07 +10:00
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
void clear (void);
|
2014-05-26 17:11:07 +10:00
|
|
|
|
|
|
|
|
2018-01-13 13:48:58 +11:00
|
|
|
private:
|
2018-09-17 14:50:23 +10:00
|
|
|
std::map<const std::string, id_t, std::less<>> m_map;
|
2014-05-26 17:11:07 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|