From f8ecebef8c37df786bc434ba270948cb18154aab Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Thu, 27 Feb 2020 13:42:26 +0100 Subject: [PATCH] Support for using ConstString in unordered_maps and switch case statements. --- src/ConstString.hpp | 27 ++++++++++++++++++++++++++- tests/ConstStringTests.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/ConstStringTests.cpp diff --git a/src/ConstString.hpp b/src/ConstString.hpp index 68c415b..423affc 100644 --- a/src/ConstString.hpp +++ b/src/ConstString.hpp @@ -5,6 +5,7 @@ #include #include #include + namespace Arbutils { class ConstString { private: @@ -18,7 +19,9 @@ namespace Arbutils { inline static int constexpr Length(const char* str) { return *str ? 1 + Length(str + 1) : 0; } public: + constexpr ConstString() : _str(""), _length(0), _hash(Hash("")){}; constexpr explicit ConstString(const char* str) : _str(str), _length(Length(str)), _hash(Hash(str)){}; + constexpr explicit ConstString(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); } @@ -26,8 +29,30 @@ namespace Arbutils { [[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()(ConstString const& s) const noexcept { return s.GetHash(); } + inline constexpr operator uint32_t() const{ return _hash; } + + inline constexpr bool operator==(const ConstString& rhs) const{ + return _hash == rhs._hash; + } + inline constexpr bool operator!=(const ConstString& rhs) const{ + return _hash != rhs._hash; + } + }; +} + +inline constexpr Arbutils::ConstString operator"" _const(const char* c, unsigned long l) { + return Arbutils::ConstString(c, l); +} +inline constexpr Arbutils::ConstString operator"" _c(const char* c, unsigned long l) { + return Arbutils::ConstString(c, l); +} + +namespace std { + template <> struct hash { + constexpr std::size_t operator()(Arbutils::ConstString const& s) const noexcept { return s.GetHash(); } }; - inline constexpr ConstString operator"" _const(const char* c) { return ConstString(c); } } #endif // ARBUTILS_CONSTSTRING_HPP diff --git a/tests/ConstStringTests.cpp b/tests/ConstStringTests.cpp new file mode 100644 index 0000000..7ed588a --- /dev/null +++ b/tests/ConstStringTests.cpp @@ -0,0 +1,26 @@ +#ifdef TESTS_BUILD +#include +#include +#include "../extern/catch.hpp" +#include "../src/ConstString.hpp" + +TEST_CASE("Use const string in unordered_map", "[Utilities]") { + std::unordered_map map; + map.insert({"foo"_c, 1}); + map.insert({"bar"_c, 5}); + + CHECK(map["bar"_c] == 5); + CHECK(map["foo"_c] == 1); +} + +TEST_CASE("Use const string in switch case", "[Utilities]") { + auto val = Arbutils::ConstString("foobar"); + switch (val){ + case "foo"_c: FAIL(); break; + case "bar"_c: FAIL(); break; + case "foobar"_c: SUCCEED(); break; + default: FAIL(); break; + } +} + +#endif \ No newline at end of file