Skip to content

Commit

Permalink
Add #69: Let IterableAccessor satisfy ranges::input_range
Browse files Browse the repository at this point in the history
  • Loading branch information
spnda committed Aug 19, 2024
1 parent f434a94 commit bea9aa0
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions include/fastgltf/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,10 @@ class IterableAccessor;
template <typename ElementType, typename BufferDataAdapter = DefaultBufferDataAdapter>
class AccessorIterator {
protected:
const IterableAccessor<ElementType, BufferDataAdapter>* accessor;
std::size_t idx;
std::size_t sparseIdx = 0;
std::size_t nextSparseIndex = 0;
const IterableAccessor<ElementType, BufferDataAdapter>* accessor = nullptr;
std::size_t idx = std::numeric_limits<std::size_t>::max();
mutable std::size_t sparseIdx = 0;
mutable std::size_t nextSparseIndex = 0;

public:
using value_type = ElementType;
Expand All @@ -486,6 +486,8 @@ class AccessorIterator {
// some things that these come with (e.g. std::distance using operator-).
using iterator_category = std::random_access_iterator_tag;

AccessorIterator() = default;

AccessorIterator(const IterableAccessor<ElementType, BufferDataAdapter>* accessor, std::size_t idx = 0)
: accessor(accessor), idx(idx) {
if (accessor->accessor.sparse.has_value()) {
Expand All @@ -512,7 +514,8 @@ class AccessorIterator {

[[nodiscard]] bool operator==(const AccessorIterator& iterator) const noexcept {
// We don't compare sparse properties
return idx == iterator.idx &&
return accessor == iterator.accessor &&
idx == iterator.idx &&
accessor->bufferBytes.data() == iterator.accessor->bufferBytes.data() &&
accessor->stride == iterator.accessor->stride &&
accessor->componentType == iterator.accessor->componentType;
Expand All @@ -522,7 +525,7 @@ class AccessorIterator {
return !(*this == iterator);
}

[[nodiscard]] ElementType operator*() noexcept {
[[nodiscard]] ElementType operator*() const noexcept {
if (accessor->accessor.sparse.has_value()) {
if (idx == nextSparseIndex) {
// Get the sparse value for this index
Expand All @@ -546,6 +549,10 @@ class AccessorIterator {
}
};

#if FASTGLTF_HAS_CONCEPTS
static_assert(std::input_iterator<AccessorIterator<math::fvec4>>, "AccessorIterator needs to satisfy input_iterator");
#endif

template <typename ElementType, typename BufferDataAdapter = DefaultBufferDataAdapter>
class IterableAccessor {
friend class AccessorIterator<ElementType, BufferDataAdapter>;
Expand Down Expand Up @@ -602,6 +609,10 @@ class IterableAccessor {
}
};

#if FASTGLTF_HAS_CONCEPTS
static_assert(std::ranges::input_range<IterableAccessor<math::fvec4>>, "IterableAccessor needs to satisfy input_range");
#endif

FASTGLTF_EXPORT template <typename ElementType, typename BufferDataAdapter = DefaultBufferDataAdapter>
#if FASTGLTF_HAS_CONCEPTS
requires Element<ElementType>
Expand Down

0 comments on commit bea9aa0

Please sign in to comment.