diff --git a/core/include/cubos/core/reflection/traits/array.hpp b/core/include/cubos/core/reflection/traits/array.hpp index 4e36330c6..911083841 100644 --- a/core/include/cubos/core/reflection/traits/array.hpp +++ b/core/include/cubos/core/reflection/traits/array.hpp @@ -92,6 +92,10 @@ namespace cubos::core::reflection /// @return Whether the operation is supported. bool hasErase() const; + /// @brief Checks if resize is supported. + /// @return Whether the operation is supported. + bool hasResize() const; + /// @brief Returns the element type of the array. /// @return Element type. const Type& elementType() const; @@ -159,6 +163,16 @@ namespace cubos::core::reflection /// @param index Element index. void erase(std::size_t index) const; + /// @brief Clears the array. + /// @note Aborts if @ref ArrayTrait::hasErase() returns false. + void clear() const; + + /// @brief Resizes the array. If this increases the size of the array, @ref insertDefault + /// is used to insert the remaining elements. + /// @note Aborts if @ref ArrayTrait::hasResize() returns false. + /// @param size Array size. + void resize(std::size_t size) const; + /// @brief Gets an iterator to the first element. /// @return Iterator. Iterator begin() const; diff --git a/core/src/cubos/core/reflection/traits/array.cpp b/core/src/cubos/core/reflection/traits/array.cpp index e0ebafc4c..d3ed1f594 100644 --- a/core/src/cubos/core/reflection/traits/array.cpp +++ b/core/src/cubos/core/reflection/traits/array.cpp @@ -55,6 +55,11 @@ bool ArrayTrait::hasErase() const return mErase != nullptr; } +bool ArrayTrait::hasResize() const +{ + return this->hasErase() && this->hasInsertDefault(); +} + const Type& ArrayTrait::elementType() const { return mElementType; @@ -111,6 +116,35 @@ void ArrayTrait::View::erase(std::size_t index) const mTrait.mErase(mInstance, index); } +void ArrayTrait::View::clear() const +{ + CUBOS_ASSERT(mTrait.hasErase(), "Clear not supported"); + + // This really inefficient, but if it ever becomes a problem its easy to improve, we could just + // add yet another function pointer to the trait, which each array type sets. + while (this->length() > 0) + { + this->erase(this->length() - 1); + } +} + +void ArrayTrait::View::resize(std::size_t size) const +{ + CUBOS_ASSERT(mTrait.hasResize(), "Resize not supported"); + + // This really inefficient, but if it ever becomes a problem its easy to improve, we could just + // add yet another function pointer to the trait, which each array type sets. + while (this->length() > size) + { + this->erase(this->length() - 1); + } + + while (this->length() < size) + { + this->insertDefault(this->length()); + } +} + ArrayTrait::View::Iterator ArrayTrait::View::begin() const { return Iterator{*this, 0};