Просмотр исходного кода

meta: allow injecting a meta_type into a meta_any to rebind overloaded types as needed

skypjack 1 день назад
Родитель
Сommit
a54c0689dd
2 измененных файлов с 15 добавлено и 0 удалено
  1. 1 0
      TODO
  2. 14 0
      src/entt/meta/meta.hpp

+ 1 - 0
TODO

@@ -43,3 +43,4 @@ TODO:
 * test (and fix) resolve where the type id conflicts with the type hash
 * rename meta-type::id -> ::alias
 * assert and test type mismatch on meta factory construction and type(...) - ie id vs alias on overloaded and standard
+* test meta_any::type(value) and use it with from_void and the like to avoid lookups in some cases

+ 14 - 0
src/entt/meta/meta.hpp

@@ -389,6 +389,12 @@ public:
      */
     [[nodiscard]] inline meta_type type() const noexcept;
 
+    /**
+     * @brief Sets a meta type for the contained instance.
+     * @param alias The meta to use with the contained instance.
+     */
+    inline void type(const meta_type &alias) noexcept;
+
     /**
      * @brief Invokes the underlying function, if possible.
      * @param id Unique identifier.
@@ -1009,6 +1015,8 @@ struct meta_base: meta_object<internal::meta_base_node> {
 
 /*! @brief Opaque wrapper for types. */
 class meta_type {
+    friend class meta_any;
+
     [[nodiscard]] const auto &fetch_node() const {
         return (node == nullptr) ? internal::resolve<void>(internal::meta_context::from(*ctx)) : *node;
     }
@@ -1470,6 +1478,12 @@ private:
     return *this ? meta_type{*ctx, fetch_node()} : meta_type{};
 }
 
+inline void meta_any::type(const meta_type &alias) noexcept {
+    ENTT_ASSERT(type().info() == alias.info(), "Unexpected type");
+    node = alias.node;
+    ctx = alias.ctx;
+}
+
 // NOLINTNEXTLINE(modernize-use-nodiscard)
 meta_any meta_any::invoke(const id_type id, auto &&...args) const {
     return type().invoke(id, *this, stl::forward<decltype(args)>(args)...);