2020-02-27 16:22:06 +00:00
|
|
|
#ifndef ARBUTILS_CONSTSTRINGCORE_HPP
|
|
|
|
#define ARBUTILS_CONSTSTRINGCORE_HPP
|
|
|
|
|
|
|
|
#if WINDOWS
|
2020-02-27 16:30:31 +00:00
|
|
|
#define STDSTRINGCONSTEXPR
|
2020-02-27 16:22:06 +00:00
|
|
|
#else
|
2020-02-27 16:30:31 +00:00
|
|
|
#define STDSTRINGCONSTEXPR constexpr
|
2020-02-27 16:22:06 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ConstStringCore(name, hashFunction) \
|
|
|
|
namespace Arbutils { \
|
|
|
|
class name { \
|
|
|
|
private: \
|
|
|
|
const char* _str; \
|
|
|
|
size_t _length; \
|
|
|
|
uint32_t _hash; \
|
|
|
|
\
|
|
|
|
hashFunction; \
|
|
|
|
\
|
|
|
|
inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } \
|
|
|
|
\
|
|
|
|
public: \
|
|
|
|
constexpr name() : _str(""), _length(0), _hash(Hash("")){}; \
|
|
|
|
constexpr explicit name(const char* str) : _str(str), _length(Length(str)), _hash(Hash(str)){}; \
|
2020-02-27 16:30:31 +00:00
|
|
|
STDSTRINGCONSTEXPR explicit name(const std::string& str) \
|
2020-02-27 16:22:06 +00:00
|
|
|
: _str(str.c_str()), _length(str.length()), _hash(Hash(str.c_str())){}; \
|
|
|
|
constexpr explicit name(const char* str, size_t size) : _str(str), _length(size), _hash(Hash(str)){}; \
|
|
|
|
\
|
|
|
|
[[nodiscard]] inline constexpr const char* c_str() const noexcept { return _str; } \
|
|
|
|
[[nodiscard]] inline std::string std_str() const { return std::string(_str, _length); } \
|
|
|
|
\
|
|
|
|
[[nodiscard]] inline constexpr size_t Length() const noexcept { return _length; } \
|
|
|
|
\
|
|
|
|
[[nodiscard]] inline constexpr uint32_t GetHash() const noexcept { return _hash; } \
|
|
|
|
\
|
|
|
|
constexpr std::size_t operator()(name const& s) const noexcept { return s.GetHash(); } \
|
|
|
|
inline constexpr operator uint32_t() const { return _hash; } \
|
|
|
|
\
|
|
|
|
inline constexpr bool operator==(const name& rhs) const { return _hash == rhs._hash; } \
|
|
|
|
inline constexpr bool operator!=(const name& rhs) const { return _hash != rhs._hash; } \
|
2020-02-27 16:30:31 +00:00
|
|
|
inline STDSTRINGCONSTEXPR bool operator==(const std::string& rhs) const { \
|
|
|
|
return _hash == Hash(rhs.c_str()); } \
|
|
|
|
inline STDSTRINGCONSTEXPR bool operator!=(const std::string& rhs) const { \
|
|
|
|
return _hash != Hash(rhs.c_str()); } \
|
2020-02-27 16:22:06 +00:00
|
|
|
inline constexpr bool operator==(const char* rhs) const { return _hash == Hash(rhs); } \
|
|
|
|
inline constexpr bool operator!=(const char* rhs) const { return _hash != Hash(rhs); } \
|
|
|
|
}; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
namespace std { \
|
|
|
|
template <> struct hash<Arbutils::name> { \
|
|
|
|
constexpr std::size_t operator()(Arbutils::name const& s) const noexcept { return s.GetHash(); } \
|
|
|
|
}; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|