🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

  1. #ifndef ENTT_CORE_IDENT_HPP
  2. #define ENTT_CORE_IDENT_HPP
  3. #include <cstddef>
  4. #include <type_traits>
  5. #include <utility>
  6. #include "../config/config.h"
  7. #include "fwd.hpp"
  8. #include "type_traits.hpp"
  9. namespace entt {
  10. /**
  11. * @brief Types identifiers.
  12. *
  13. * Variable template used to generate identifiers at compile-time for the given
  14. * types. Use the `get` member function to know what's the identifier associated
  15. * to the specific type.
  16. *
  17. * @note
  18. * Identifiers are constant expression and can be used in any context where such
  19. * an expression is required. As an example:
  20. * @code{.cpp}
  21. * using id = entt::identifier<a_type, another_type>;
  22. *
  23. * switch(a_type_identifier) {
  24. * case id::type<a_type>:
  25. * // ...
  26. * break;
  27. * case id::type<another_type>:
  28. * // ...
  29. * break;
  30. * default:
  31. * // ...
  32. * }
  33. * @endcode
  34. *
  35. * @tparam Types List of types for which to generate identifiers.
  36. */
  37. template<typename... Types>
  38. class identifier {
  39. template<typename Type, std::size_t... Index>
  40. [[nodiscard]] static constexpr id_type get(std::index_sequence<Index...>) ENTT_NOEXCEPT {
  41. static_assert((std::is_same_v<Type, Types> || ...), "Invalid type");
  42. return (0 + ... + (std::is_same_v<Type, type_list_element_t<Index, type_list<std::decay_t<Types>...>>> ? id_type{Index} : id_type{}));
  43. }
  44. public:
  45. /*! @brief Unsigned integer type. */
  46. using identifier_type = id_type;
  47. /*! @brief Statically generated unique identifier for the given type. */
  48. template<typename Type>
  49. static constexpr identifier_type type = get<std::decay_t<Type>>(std::index_sequence_for<Types...>{});
  50. };
  51. } // namespace entt
  52. #endif