From ff32d77eba8a98aa988392b47fa1468c3369eedd Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 4 Apr 2025 12:00:15 +0200 Subject: [PATCH] meta: deprecate fixed_size in favor of extent/meta_dynamic_extent --- TODO | 3 +-- src/entt/meta/container.hpp | 41 ++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/TODO b/TODO index 96f6b43f0..88cda3866 100644 --- a/TODO +++ b/TODO @@ -34,5 +34,4 @@ TODO: * doc: IMPLICIT_DIR_DOCS for dir docs or \dir * meta non-const allow_cast overloads: (const int &) to (int &) is not allowed, but (const int &) to (double &) is allowed (support only for convertibles) * improve non-const allow cast with in-place switch -* meta fixed_size could return the size directly if present -* setup testbed workflow on the CI and review build process for testbed (i.e. tests first due to SDL) +* review build process for testbed (i.e. tests first due to SDL) diff --git a/src/entt/meta/container.hpp b/src/entt/meta/container.hpp index 3c2da970e..4ca4cb022 100644 --- a/src/entt/meta/container.hpp +++ b/src/entt/meta/container.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -15,23 +16,27 @@ #include #include "../container/dense_map.hpp" #include "../container/dense_set.hpp" +#include "../core/type_traits.hpp" #include "context.hpp" #include "meta.hpp" #include "type_traits.hpp" namespace entt { +/*! @brief Used to identicate that a sequence container has not a fixed size. */ +inline constexpr std::size_t meta_dynamic_extent = std::numeric_limits::max(); + /*! @cond TURN_OFF_DOXYGEN */ namespace internal { -template -struct fixed_size_sequence_container: std::true_type {}; +template +struct sequence_container_extent: integral_constant {}; template -struct fixed_size_sequence_container>: std::false_type {}; +struct sequence_container_extent>>>: integral_constant> {}; template -inline constexpr bool fixed_size_sequence_container_v = fixed_size_sequence_container::value; +inline constexpr std::size_t sequence_container_extent_v = sequence_container_extent::value; template struct key_only_associative_container: std::true_type {}; @@ -67,8 +72,10 @@ struct basic_meta_sequence_container_traits { /*! @brief Meta iterator type. */ using iterator = typename meta_sequence_container::iterator; + /*! @brief Number of elements, or `meta_dynamic_extent` if dynamic. */ + static constexpr std::size_t extent = internal::sequence_container_extent_v; /*! @brief True in case of fixed size containers, false otherwise. */ - static constexpr bool fixed_size = internal::fixed_size_sequence_container_v; + [[deprecated("use ::extent instead")]] static constexpr bool fixed_size = (extent != meta_dynamic_extent); /** * @brief Returns the number of elements in a container. @@ -85,11 +92,11 @@ struct basic_meta_sequence_container_traits { * @return True in case of success, false otherwise. */ [[nodiscard]] static bool clear([[maybe_unused]] void *container) { - if constexpr(fixed_size) { - return false; - } else { + if constexpr(extent == meta_dynamic_extent) { static_cast(container)->clear(); return true; + } else { + return false; } } @@ -115,11 +122,11 @@ struct basic_meta_sequence_container_traits { * @return True in case of success, false otherwise. */ [[nodiscard]] static bool resize([[maybe_unused]] void *container, [[maybe_unused]] const size_type sz) { - if constexpr(fixed_size || !std::is_default_constructible_v) { - return false; - } else { + if constexpr((extent == meta_dynamic_extent) && std::is_default_constructible_v) { static_cast(container)->resize(sz); return true; + } else { + return false; } } @@ -160,13 +167,13 @@ struct basic_meta_sequence_container_traits { * @return A possibly invalid iterator to the inserted element. */ [[nodiscard]] static iterator insert([[maybe_unused]] const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const void *value, [[maybe_unused]] const void *cref, [[maybe_unused]] const iterator &it) { - if constexpr(fixed_size) { - return iterator{}; - } else { + if constexpr(extent == meta_dynamic_extent) { auto *const non_const = any_cast(&it.base()); return {area, static_cast(container)->insert( non_const ? *non_const : any_cast(it.base()), (value != nullptr) ? *static_cast(value) : *static_cast *>(cref))}; + } else { + return iterator{}; } } @@ -178,11 +185,11 @@ struct basic_meta_sequence_container_traits { * @return A possibly invalid iterator following the last removed element. */ [[nodiscard]] static iterator erase([[maybe_unused]] const meta_ctx &area, [[maybe_unused]] void *container, [[maybe_unused]] const iterator &it) { - if constexpr(fixed_size) { - return iterator{}; - } else { + if constexpr(extent == meta_dynamic_extent) { auto *const non_const = any_cast(&it.base()); return {area, static_cast(container)->erase(non_const ? *non_const : any_cast(it.base()))}; + } else { + return iterator{}; } } };