Browse Source

user defined string literal for hashed strings

Michele Caini 7 years ago
parent
commit
1e51ffdb72
4 changed files with 26 additions and 33 deletions
  1. 0 3
      TODO
  2. 7 1
      src/entt/config/config.h
  3. 5 0
      src/entt/core/hashed_string.hpp
  4. 14 29
      test/entt/core/hashed_string.cpp

+ 0 - 3
TODO

@@ -8,9 +8,6 @@
 * create dedicated flat map based on types implementation (sort of "type map") for types to use within the registry and so on...
 * create dedicated flat map based on types implementation (sort of "type map") for types to use within the registry and so on...
 * ease the assignment of tags as string (type-less assign member function + user defined literal for hashed strings)
 * ease the assignment of tags as string (type-less assign member function + user defined literal for hashed strings)
 * config system with atomic (something like Config<"foobar"_hs>::set(true) and Config<"foobar"_hs>::get<bool>())
 * config system with atomic (something like Config<"foobar"_hs>::set(true) and Config<"foobar"_hs>::get<bool>())
-* user defined literal for hashed strings (add a config parameter to disable it and avoid pollution)
-* is it possible to use EASTL instead of the standard library?
-* registry-to-registry serializer for deep copies (see #100)
 * work stealing job system (see #100)
 * work stealing job system (see #100)
 * C++17. That's all.
 * C++17. That's all.
 * AOB
 * AOB

+ 7 - 1
src/entt/config/config.h

@@ -4,7 +4,13 @@
 
 
 #ifndef ENTT_NOEXCEPT
 #ifndef ENTT_NOEXCEPT
 #define ENTT_NOEXCEPT noexcept
 #define ENTT_NOEXCEPT noexcept
-#endif
+#endif // ENTT_NOEXCEPT
+
+
+#ifndef ENTT_HS_SUFFIX
+#define ENTT_HS_SUFFIX _hs
+#endif // ENTT_HS_SUFFIX
+
 
 
 
 
 #endif // ENTT_CONFIG_CONFIG_H
 #endif // ENTT_CONFIG_CONFIG_H

+ 5 - 0
src/entt/core/hashed_string.hpp

@@ -108,4 +108,9 @@ constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) ENTT
 }
 }
 
 
 
 
+constexpr entt::HashedString operator"" ENTT_HS_SUFFIX(const char *str, const long unsigned int) ENTT_NOEXCEPT {
+    return entt::HashedString{str};
+}
+
+
 #endif // ENTT_CORE_HASHED_STRING_HPP
 #endif // ENTT_CORE_HASHED_STRING_HPP

+ 14 - 29
test/entt/core/hashed_string.cpp

@@ -1,33 +1,7 @@
-#include <cstddef>
+#include <type_traits>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include <entt/core/hashed_string.hpp>
 #include <entt/core/hashed_string.hpp>
 
 
-static constexpr bool ptr(const char *str) {
-    using hash_type = entt::HashedString::hash_type;
-
-    return (static_cast<hash_type>(entt::HashedString{str}) == entt::HashedString{str}
-            && static_cast<const char *>(entt::HashedString{str}) == str
-            && entt::HashedString{str} == entt::HashedString{str}
-            && !(entt::HashedString{str} != entt::HashedString{str}));
-}
-
-template<std::size_t N>
-static constexpr bool ref(const char (&str)[N]) {
-    using hash_type = entt::HashedString::hash_type;
-
-    return (static_cast<hash_type>(entt::HashedString{str}) == entt::HashedString{str}
-            && static_cast<const char *>(entt::HashedString{str}) == str
-            && entt::HashedString{str} == entt::HashedString{str}
-            && !(entt::HashedString{str} != entt::HashedString{str}));
-}
-
-TEST(HashedString, Constexprness) {
-    // how would you test a constexpr otherwise?
-    static_assert(ptr("foo"), "!");
-    static_assert(ref("bar"), "!");
-    ASSERT_TRUE(true);
-}
-
 TEST(HashedString, Functionalities) {
 TEST(HashedString, Functionalities) {
     using hash_type = entt::HashedString::hash_type;
     using hash_type = entt::HashedString::hash_type;
 
 
@@ -40,10 +14,21 @@ TEST(HashedString, Functionalities) {
     ASSERT_EQ(static_cast<const char *>(fooHs), "foo");
     ASSERT_EQ(static_cast<const char *>(fooHs), "foo");
     ASSERT_EQ(static_cast<const char *>(barHs), bar);
     ASSERT_EQ(static_cast<const char *>(barHs), bar);
 
 
-    ASSERT_TRUE(fooHs == fooHs);
-    ASSERT_TRUE(fooHs != barHs);
+    ASSERT_EQ(fooHs, fooHs);
+    ASSERT_NE(fooHs, barHs);
 
 
     entt::HashedString hs{"foobar"};
     entt::HashedString hs{"foobar"};
 
 
     ASSERT_EQ(static_cast<hash_type>(hs), 0x85944171f73967e8);
     ASSERT_EQ(static_cast<hash_type>(hs), 0x85944171f73967e8);
+
+    ASSERT_EQ(fooHs, "foo"_hs);
+    ASSERT_NE(barHs, "foo"_hs);
+}
+
+TEST(HashedString, Constexprness) {
+    using hash_type = entt::HashedString::hash_type;
+    // how would you test a constexpr otherwise?
+    (void)std::integral_constant<hash_type, entt::HashedString{"quux"}>{};
+    (void)std::integral_constant<hash_type, "quux"_hs>{};
+    ASSERT_TRUE(true);
 }
 }