resolve.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef ENTT_META_RESOLVE_HPP
  2. #define ENTT_META_RESOLVE_HPP
  3. #include <type_traits>
  4. #include "../core/type_info.hpp"
  5. #include "../locator/locator.hpp"
  6. #include "context.hpp"
  7. #include "meta.hpp"
  8. #include "node.hpp"
  9. #include "range.hpp"
  10. namespace entt {
  11. /**
  12. * @brief Returns the meta type associated with a given type.
  13. * @tparam Type Type to use to search for a meta type.
  14. * @param ctx The context from which to search for meta types.
  15. * @return The meta type associated with the given type, if any.
  16. */
  17. template<typename Type>
  18. [[nodiscard]] meta_type resolve(const meta_ctx &ctx) noexcept {
  19. auto &&context = internal::meta_context::from(ctx);
  20. return {ctx, internal::resolve<std::remove_cv_t<std::remove_reference_t<Type>>>(context)};
  21. }
  22. /**
  23. * @brief Returns the meta type associated with a given type.
  24. * @tparam Type Type to use to search for a meta type.
  25. * @return The meta type associated with the given type, if any.
  26. */
  27. template<typename Type>
  28. [[nodiscard]] meta_type resolve() noexcept {
  29. return resolve<Type>(locator<meta_ctx>::value_or());
  30. }
  31. /**
  32. * @brief Returns a range to use to visit all meta types.
  33. * @param ctx The context from which to search for meta types.
  34. * @return An iterable range to use to visit all meta types.
  35. */
  36. [[nodiscard]] inline meta_range<meta_type, typename decltype(internal::meta_context::value)::const_iterator> resolve(const meta_ctx &ctx) noexcept {
  37. auto &&context = internal::meta_context::from(ctx);
  38. return {{ctx, context.value.cbegin()}, {ctx, context.value.cend()}};
  39. }
  40. /**
  41. * @brief Returns a range to use to visit all meta types.
  42. * @return An iterable range to use to visit all meta types.
  43. */
  44. [[nodiscard]] inline meta_range<meta_type, typename decltype(internal::meta_context::value)::const_iterator> resolve() noexcept {
  45. return resolve(locator<meta_ctx>::value_or());
  46. }
  47. /**
  48. * @brief Returns the meta type associated with a given identifier, if any.
  49. * @param ctx The context from which to search for meta types.
  50. * @param id Unique identifier.
  51. * @return The meta type associated with the given identifier, if any.
  52. */
  53. [[nodiscard]] inline meta_type resolve(const meta_ctx &ctx, const id_type id) noexcept {
  54. for(auto &&curr: resolve(ctx)) {
  55. if(curr.second.id() == id) {
  56. return curr.second;
  57. }
  58. }
  59. return meta_type{};
  60. }
  61. /**
  62. * @brief Returns the meta type associated with a given identifier, if any.
  63. * @param id Unique identifier.
  64. * @return The meta type associated with the given identifier, if any.
  65. */
  66. [[nodiscard]] inline meta_type resolve(const id_type id) noexcept {
  67. return resolve(locator<meta_ctx>::value_or(), id);
  68. }
  69. /**
  70. * @brief Returns the meta type associated with a given type info object.
  71. * @param ctx The context from which to search for meta types.
  72. * @param info The type info object of the requested type.
  73. * @return The meta type associated with the given type info object, if any.
  74. */
  75. [[nodiscard]] inline meta_type resolve(const meta_ctx &ctx, const type_info &info) noexcept {
  76. auto &&context = internal::meta_context::from(ctx);
  77. const auto *elem = internal::try_resolve(context, info);
  78. return elem ? meta_type{ctx, *elem} : meta_type{};
  79. }
  80. /**
  81. * @brief Returns the meta type associated with a given type info object.
  82. * @param info The type info object of the requested type.
  83. * @return The meta type associated with the given type info object, if any.
  84. */
  85. [[nodiscard]] inline meta_type resolve(const type_info &info) noexcept {
  86. return resolve(locator<meta_ctx>::value_or(), info);
  87. }
  88. } // namespace entt
  89. #endif