From 79902d7c85119aab7670b9d4b15a867862451dcc Mon Sep 17 00:00:00 2001 From: sean Date: Sat, 25 Jan 2025 15:46:21 +0100 Subject: [PATCH] Fix: Add missing span constructors and guides --- include/fastgltf/types.hpp | 128 ++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 58 deletions(-) diff --git a/include/fastgltf/types.hpp b/include/fastgltf/types.hpp index 3aaf791e6..e5c86b663 100644 --- a/include/fastgltf/types.hpp +++ b/include/fastgltf/types.hpp @@ -1476,91 +1476,103 @@ namespace fastgltf { [[nodiscard]] bool valid() const noexcept; [[nodiscard]] bool isLocalPath() const noexcept; [[nodiscard]] bool isDataUri() const noexcept; - }; + }; - FASTGLTF_EXPORT inline constexpr std::size_t dynamic_extent = std::numeric_limits::max(); + FASTGLTF_EXPORT inline constexpr std::size_t dynamic_extent = std::numeric_limits::max(); - /** - * Custom span class imitating C++20's std::span for referencing bytes without owning the - * allocation. Can also directly be converted to a std::span or used by itself. - */ - FASTGLTF_EXPORT template - class span { - using element_type = T; - using value_type = std::remove_cv_t; - using size_type = std::size_t; - using difference_type = std::ptrdiff_t; - using pointer = T*; - using const_pointer = const T*; - using reference = T&; - using const_reference = const T&; - - pointer _ptr = nullptr; - size_type _size = 0; + /** + * Custom span class imitating C++20's std::span for referencing bytes without owning the + * allocation. Can also directly be converted to a std::span or used by itself. + */ + FASTGLTF_EXPORT template + class span { + using element_type = T; + using value_type = std::remove_cv_t; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using pointer = T*; + using const_pointer = const T*; + using reference = T&; + using const_reference = const T&; - public: - constexpr span() noexcept = default; + pointer _ptr = nullptr; + size_type _size = 0; + + public: + constexpr span() = default; + + // std::span ctor (2) + template + explicit /*(Extent != dynamic_extent)*/ constexpr span(Iterator first, const size_type count) : _ptr(first), _size(count) {} + + // std::span ctor (5) + template + constexpr span(std::array& arr) noexcept : _ptr(arr.data()), _size(N) {} - template - explicit constexpr span(Iterator first, size_type count) : _ptr(first), _size(count) {} + // std::span ctor (6) + template + constexpr span(const std::array& arr) noexcept : _ptr(arr.data()), _size(N) {} #if FASTGLTF_CPP_20 - constexpr span(std::span data) : _ptr(data.data()), _size(data.size()) {} + constexpr span(std::span data) : _ptr(data.data()), _size(data.size()) {} #endif - constexpr span(const span& other) noexcept = default; - constexpr span& operator=(const span& other) noexcept = default; + constexpr span(const span& other) noexcept = default; + constexpr span& operator=(const span& other) = default; - [[nodiscard]] constexpr reference operator[](size_type idx) const { - return data()[idx]; - } + [[nodiscard]] constexpr reference operator[](size_type idx) const { + return data()[idx]; + } [[nodiscard]] constexpr reference at(size_type idx) const { - if (idx >= size()) { - raise("Index is out of range for span"); - } return data()[idx]; } - [[nodiscard]] constexpr pointer data() const noexcept { - return _ptr; - } + [[nodiscard]] constexpr pointer data() const { + return _ptr; + } - [[nodiscard]] constexpr size_type size() const noexcept { - return _size; - } + [[nodiscard]] constexpr size_type size() const { + return _size; + } - [[nodiscard]] constexpr size_type size_bytes() const noexcept { - return size() * sizeof(element_type); - } + [[nodiscard]] constexpr size_type size_bytes() const { + return size() * sizeof(element_type); + } - [[nodiscard]] constexpr bool empty() const noexcept { - return size() == 0; - } + [[nodiscard]] constexpr bool empty() const { + return size() == 0; + } - [[nodiscard]] constexpr span first(size_type count) const { - return span(_ptr, count); - } + [[nodiscard]] constexpr span first(size_type count) const { + return span(_ptr, count); + } - [[nodiscard]] constexpr span last(size_type count) const { - return span(&data()[size() - count], count); - } + [[nodiscard]] constexpr span last(size_type count) const { + return span(&data()[size() - count], count); + } - [[nodiscard]] constexpr span subspan(size_type offset, size_type count = dynamic_extent) const { - return span(&data()[offset], count == dynamic_extent ? size() - offset : count); - } + [[nodiscard]] constexpr span subspan(size_type offset, size_type count = dynamic_extent) const { + return span(&data()[offset], count == dynamic_extent ? size() - offset : count); + } #if FASTGLTF_CPP_20 - operator std::span() const { - return std::span(data(), size()); - } + operator std::span() const { + return std::span(data(), size()); + } #endif - }; + }; + + FASTGLTF_EXPORT template + span(T* data, std::size_t count) -> span; - // Deduction guide for easily instantiating spans FASTGLTF_EXPORT template span(const T* data, std::size_t size) -> span; + // std::span deduction guide (4) + FASTGLTF_EXPORT template + span(const std::array&) -> span; + FASTGLTF_EXPORT using CustomBufferId = std::uint64_t; /**