From f0fee57abf2add9a874e0abf6aa4e7a6246e5da1 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 | 114 +++++++++++++++++++------------------ 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/include/fastgltf/types.hpp b/include/fastgltf/types.hpp index 3aaf791e6..69096330f 100644 --- a/include/fastgltf/types.hpp +++ b/include/fastgltf/types.hpp @@ -1485,82 +1485,84 @@ namespace fastgltf { * 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; + 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; - template - explicit constexpr span(Iterator first, size_type count) : _ptr(first), _size(count) {} + public: + constexpr span() = default; -#if FASTGLTF_CPP_20 - constexpr span(std::span data) : _ptr(data.data()), _size(data.size()) {} -#endif + // std::span ctor (2) + template + explicit /*(Extent != dynamic_extent)*/ constexpr span(Iterator first, const size_type count) : _ptr(first), _size(count) {} - constexpr span(const span& other) noexcept = default; - constexpr span& operator=(const span& other) noexcept = default; + // std::span ctor (5) + template + constexpr span(std::array& arr) noexcept : _ptr(arr.data()), _size(N) {} - [[nodiscard]] constexpr reference operator[](size_type idx) const { - return data()[idx]; - } + // std::span ctor (6) + template + constexpr span(const std::array& arr) noexcept : _ptr(arr.data()), _size(N) {} + + 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 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()); - } -#endif - }; + template + span(T* data, std::size_t count) -> span; - // Deduction guide for easily instantiating spans - FASTGLTF_EXPORT template + template span(const T* data, std::size_t size) -> span; + // std::span deduction guide (4) + template + span(const std::array&) -> span; + FASTGLTF_EXPORT using CustomBufferId = std::uint64_t; /**