Procházet zdrojové kódy

meta: avoid duplicating data in meta containers

Michele Caini před 2 roky
rodič
revize
9f80b14f88
1 změnil soubory, kde provedl 29 přidání a 35 odebrání
  1. 29 35
      src/entt/meta/meta.hpp

+ 29 - 35
src/entt/meta/meta.hpp

@@ -64,11 +64,8 @@ public:
         end_fn = meta_sequence_container_traits<std::remove_const_t<Type>>::end;
         insert_fn = meta_sequence_container_traits<std::remove_const_t<Type>>::insert;
         erase_fn = meta_sequence_container_traits<std::remove_const_t<Type>>::erase;
-        cdata = &instance;
-
-        if constexpr(!std::is_const_v<Type>) {
-            data = &instance;
-        }
+        const_only = std::is_const_v<Type>;
+        data = &instance;
     }
 
     [[nodiscard]] inline meta_type value_type() const noexcept;
@@ -95,8 +92,8 @@ private:
     iterator (*end_fn)(const meta_ctx &, void *, const void *){};
     iterator (*insert_fn)(const meta_ctx &, void *, const void *, const void *, const iterator &){};
     iterator (*erase_fn)(const meta_ctx &, void *, const iterator &){};
-    const void *cdata{};
-    void *data{};
+    const void *data{};
+    bool const_only{};
 };
 
 /*! @brief Proxy object for associative containers. */
@@ -142,11 +139,8 @@ public:
         insert_fn = &meta_associative_container_traits<std::remove_const_t<Type>>::insert;
         erase_fn = &meta_associative_container_traits<std::remove_const_t<Type>>::erase;
         find_fn = &meta_associative_container_traits<std::remove_const_t<Type>>::find;
-        cdata = &instance;
-
-        if constexpr(!std::is_const_v<Type>) {
-            data = &instance;
-        }
+        const_only = std::is_const_v<Type>;
+        data = &instance;
     }
 
     [[nodiscard]] inline bool key_only() const noexcept;
@@ -176,8 +170,8 @@ private:
     bool (*insert_fn)(void *, const void *, const void *){};
     size_type (*erase_fn)(void *, const void *){};
     iterator (*find_fn)(const meta_ctx &, void *, const void *, const void *){};
-    const void *cdata{};
-    void *data{};
+    const void *data{};
+    bool const_only{};
 };
 
 /*! @brief Possible modes of a meta any object. */
@@ -1814,7 +1808,7 @@ private:
  * @return The size of the container.
  */
 [[nodiscard]] inline meta_sequence_container::size_type meta_sequence_container::size() const noexcept {
-    return size_fn(cdata);
+    return size_fn(data);
 }
 
 /**
@@ -1823,7 +1817,7 @@ private:
  * @return True in case of success, false otherwise.
  */
 inline bool meta_sequence_container::resize(const size_type sz) {
-    return data && resize_fn(data, sz);
+    return !const_only && resize_fn(const_cast<void *>(data), sz);
 }
 
 /**
@@ -1831,7 +1825,7 @@ inline bool meta_sequence_container::resize(const size_type sz) {
  * @return True in case of success, false otherwise.
  */
 inline bool meta_sequence_container::clear() {
-    return data && clear_fn(data);
+    return !const_only && clear_fn(const_cast<void *>(data));
 }
 
 /**
@@ -1840,7 +1834,7 @@ inline bool meta_sequence_container::clear() {
  * @return True in case of success, false otherwise.
  */
 inline bool meta_sequence_container::reserve(const size_type sz) {
-    return data && reserve_fn(data, sz);
+    return !const_only && reserve_fn(const_cast<void *>(data), sz);
 }
 
 /**
@@ -1848,7 +1842,7 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
  * @return An iterator to the first element of the container.
  */
 [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::begin() {
-    return begin_fn(*ctx, data, cdata);
+    return begin_fn(*ctx, const_only ? nullptr : const_cast<void *>(data), data);
 }
 
 /**
@@ -1856,7 +1850,7 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
  * @return An iterator that is past the last element of the container.
  */
 [[nodiscard]] inline meta_sequence_container::iterator meta_sequence_container::end() {
-    return end_fn(*ctx, data, cdata);
+    return end_fn(*ctx, const_only ? nullptr : const_cast<void *>(data), data);
 }
 
 /**
@@ -1867,9 +1861,9 @@ inline bool meta_sequence_container::reserve(const size_type sz) {
  */
 inline meta_sequence_container::iterator meta_sequence_container::insert(iterator it, meta_any value) {
     // this abomination is necessary because only on macos value_type and const_reference are different types for std::vector<bool>
-    if(const auto vtype = value_type_node(internal::meta_context::from(*ctx)); data && (value.allow_cast({*ctx, vtype}) || value.allow_cast({*ctx, const_reference_node(internal::meta_context::from(*ctx))}))) {
+    if(const auto vtype = value_type_node(internal::meta_context::from(*ctx)); !const_only && (value.allow_cast({*ctx, vtype}) || value.allow_cast({*ctx, const_reference_node(internal::meta_context::from(*ctx))}))) {
         const bool is_value_type = (value.type().info() == *vtype.info);
-        return insert_fn(*ctx, data, is_value_type ? std::as_const(value).data() : nullptr, is_value_type ? nullptr : std::as_const(value).data(), it);
+        return insert_fn(*ctx, const_cast<void *>(data), is_value_type ? std::as_const(value).data() : nullptr, is_value_type ? nullptr : std::as_const(value).data(), it);
     }
 
     return iterator{*ctx};
@@ -1881,7 +1875,7 @@ inline meta_sequence_container::iterator meta_sequence_container::insert(iterato
  * @return A possibly invalid iterator following the last removed element.
  */
 inline meta_sequence_container::iterator meta_sequence_container::erase(iterator it) {
-    return data ? erase_fn(*ctx, data, it) : iterator{*ctx};
+    return const_only ? iterator{*ctx} : erase_fn(*ctx, const_cast<void *>(data), it);
 }
 
 /**
@@ -1901,7 +1895,7 @@ inline meta_sequence_container::iterator meta_sequence_container::erase(iterator
  * @return False if the proxy is invalid, true otherwise.
  */
 [[nodiscard]] inline meta_sequence_container::operator bool() const noexcept {
-    return (cdata != nullptr);
+    return (data != nullptr);
 }
 
 /**
@@ -1935,27 +1929,27 @@ inline meta_sequence_container::iterator meta_sequence_container::erase(iterator
 
 /*! @copydoc meta_sequence_container::size */
 [[nodiscard]] inline meta_associative_container::size_type meta_associative_container::size() const noexcept {
-    return size_fn(cdata);
+    return size_fn(data);
 }
 
 /*! @copydoc meta_sequence_container::clear */
 inline bool meta_associative_container::clear() {
-    return data && clear_fn(data);
+    return !const_only && clear_fn(const_cast<void *>(data));
 }
 
 /*! @copydoc meta_sequence_container::reserve */
 inline bool meta_associative_container::reserve(const size_type sz) {
-    return data && reserve_fn(data, sz);
+    return !const_only && reserve_fn(const_cast<void *>(data), sz);
 }
 
 /*! @copydoc meta_sequence_container::begin */
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::begin() {
-    return begin_fn(*ctx, data, cdata);
+    return begin_fn(*ctx, const_only ? nullptr : const_cast<void *>(data), data);
 }
 
 /*! @copydoc meta_sequence_container::end */
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::end() {
-    return end_fn(*ctx, data, cdata);
+    return end_fn(*ctx, const_only ? nullptr : const_cast<void *>(data), data);
 }
 
 /**
@@ -1965,8 +1959,9 @@ inline bool meta_associative_container::reserve(const size_type sz) {
  * @return A bool denoting whether the insertion took place.
  */
 inline bool meta_associative_container::insert(meta_any key, meta_any value = {}) {
-    const bool valid_key = key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) && (!mapped_type_node || value.allow_cast(meta_type{*ctx, mapped_type_node(internal::meta_context::from(*ctx))}));
-    return valid_key && data && insert_fn(data, std::as_const(key).data(), std::as_const(value).data());
+    return !const_only && key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))})
+           && (!mapped_type_node || value.allow_cast(meta_type{*ctx, mapped_type_node(internal::meta_context::from(*ctx))}))
+           && insert_fn(const_cast<void *>(data), std::as_const(key).data(), std::as_const(value).data());
 }
 
 /**
@@ -1975,8 +1970,7 @@ inline bool meta_associative_container::insert(meta_any key, meta_any value = {}
  * @return A bool denoting whether the removal took place.
  */
 inline meta_associative_container::size_type meta_associative_container::erase(meta_any key) {
-    const bool valid_key = key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))});
-    return (valid_key && data) ? erase_fn(data, std::as_const(key).data()) : 0u;
+    return (!const_only && key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))})) ? erase_fn(const_cast<void *>(data), std::as_const(key).data()) : 0u;
 }
 
 /**
@@ -1985,7 +1979,7 @@ inline meta_associative_container::size_type meta_associative_container::erase(m
  * @return An iterator to the element with the given key, if any.
  */
 [[nodiscard]] inline meta_associative_container::iterator meta_associative_container::find(meta_any key) {
-    return key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) ? find_fn(*ctx, data, cdata, std::as_const(key).data()) : iterator{*ctx};
+    return key.allow_cast(meta_type{*ctx, key_type_node(internal::meta_context::from(*ctx))}) ? find_fn(*ctx, const_only ? nullptr : const_cast<void *>(data), data, std::as_const(key).data()) : iterator{*ctx};
 }
 
 /**
@@ -1993,7 +1987,7 @@ inline meta_associative_container::size_type meta_associative_container::erase(m
  * @return False if the proxy is invalid, true otherwise.
  */
 [[nodiscard]] inline meta_associative_container::operator bool() const noexcept {
-    return (cdata != nullptr);
+    return (data != nullptr);
 }
 
 } // namespace entt