opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
mesh_indices_view.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstdint>
7#include <cstddef>
8#include <iterator>
9#include <ranges>
10#include <span>
11#include <stdexcept>
12#include <variant>
13
14namespace osc
15{
16 // a span-like view over mesh indices
17 //
18 // for perf reasons, runtime mesh indices can be stored in either a 16-bit or 32-bit format
19 // the mesh class exposes this fact by returning this view class, which must be checked at
20 // runtime by calling code
21
23 private:
24 using U32PtrOrU16Ptr = std::variant<const uint16_t*, const uint32_t*>;
25 public:
27 public:
30 using pointer = void;
32 using iterator_category = std::forward_iterator_tag;
33
34 Iterator() = default;
35
36 explicit Iterator(U32PtrOrU16Ptr ptr) : ptr_{ptr}
37 {}
38
40 {
41 return std::visit(Overload{
42 [](const auto* ptr) { return static_cast<value_type>(*ptr); }
43 }, ptr_);
44 }
45
46 friend bool operator==(const Iterator&, const Iterator&) = default;
47
49 {
50 return *this += 1;
51 }
52
54 {
55 Iterator copy{*this};
56 ++(*this);
57 return copy;
58 }
59
61 {
62 std::visit(Overload{
63 [n](auto& ptr) { ptr += n; }
64 }, ptr_);
65 return *this;
66 }
67
69 {
70 return std::visit(Overload{
71 [pos](const auto* ptr) { return static_cast<value_type>(ptr[pos]); }
72 }, ptr_);
73 }
74 private:
75 U32PtrOrU16Ptr ptr_{static_cast<const uint16_t*>(nullptr)};
76 };
77
83 using pointer = void;
87
88 MeshIndicesView() = default;
89
90 MeshIndicesView(const uint16_t* ptr, size_t size) :
91 ptr_{ptr},
92 size_{size}
93 {}
94
95 MeshIndicesView(const uint32_t* ptr, size_t size) :
96 ptr_{ptr},
97 size_{size}
98 {}
99
100 template<std::ranges::contiguous_range Range>
103 MeshIndicesView{std::ranges::data(range), std::ranges::size(range)}
104 {}
105
106 bool is_uint16() const
107 {
108 return std::holds_alternative<const uint16_t*>(ptr_);
109 }
110
111 bool is_uint32() const
112 {
113 return std::holds_alternative<const uint32_t*>(ptr_);
114 }
115
116 [[nodiscard]] bool empty() const
117 {
118 return size() == 0;
119 }
120
122 {
123 return size_;
124 }
125
126 std::span<const uint16_t> to_uint16_span() const
127 {
128 return {std::get<const uint16_t*>(ptr_), size_};
129 }
130
131 std::span<const uint32_t> to_uint32_span() const
132 {
133 return {std::get<const uint32_t*>(ptr_), size_};
134 }
135
137 {
138 return begin()[pos];
139 }
140
142 {
143 if (pos >= size()) {
144 throw std::out_of_range{"attempted to access a MeshIndicesView with an invalid index"};
145 }
146 return this->operator[](pos);
147 }
148
150 {
151 return Iterator{ptr_};
152 }
153
154 Iterator end() const
155 {
156 return std::visit(Overload{
157 [size = size_](const auto* ptr) { return Iterator{ptr + size}; },
158 }, ptr_);
159 }
160
161 private:
162 U32PtrOrU16Ptr ptr_{static_cast<const uint16_t*>(nullptr)};
163 size_t size_ = 0;
164 };
165}
Definition mesh_indices_view.h:26
Iterator & operator+=(difference_type n)
Definition mesh_indices_view.h:60
value_type reference
Definition mesh_indices_view.h:31
Iterator & operator++()
Definition mesh_indices_view.h:48
ptrdiff_t difference_type
Definition mesh_indices_view.h:28
void pointer
Definition mesh_indices_view.h:30
uint32_t value_type
Definition mesh_indices_view.h:29
std::forward_iterator_tag iterator_category
Definition mesh_indices_view.h:32
friend bool operator==(const Iterator &, const Iterator &)=default
value_type operator[](difference_type pos) const
Definition mesh_indices_view.h:68
value_type operator*() const
Definition mesh_indices_view.h:39
Iterator(U32PtrOrU16Ptr ptr)
Definition mesh_indices_view.h:36
Iterator operator++(int)
Definition mesh_indices_view.h:53
Definition mesh_indices_view.h:22
value_type reference
Definition mesh_indices_view.h:81
MeshIndicesView(const uint16_t *ptr, size_t size)
Definition mesh_indices_view.h:90
std::span< const uint32_t > to_uint32_span() const
Definition mesh_indices_view.h:131
MeshIndicesView(const uint32_t *ptr, size_t size)
Definition mesh_indices_view.h:95
std::span< const uint16_t > to_uint16_span() const
Definition mesh_indices_view.h:126
void const_pointer
Definition mesh_indices_view.h:84
size_type size() const
Definition mesh_indices_view.h:121
bool is_uint32() const
Definition mesh_indices_view.h:111
ptrdiff_t difference_type
Definition mesh_indices_view.h:80
bool is_uint16() const
Definition mesh_indices_view.h:106
size_t size_type
Definition mesh_indices_view.h:79
MeshIndicesView(const Range &range)
Definition mesh_indices_view.h:102
MeshIndicesView()=default
value_type const_reference
Definition mesh_indices_view.h:82
value_type at(size_type pos) const
Definition mesh_indices_view.h:141
bool empty() const
Definition mesh_indices_view.h:116
void pointer
Definition mesh_indices_view.h:83
Iterator end() const
Definition mesh_indices_view.h:154
uint32_t value_type
Definition mesh_indices_view.h:78
Iterator begin() const
Definition mesh_indices_view.h:149
value_type operator[](size_type pos) const
Definition mesh_indices_view.h:136
Definition same_as_any_of.h:9
Definition custom_decoration_generator.h:5
constexpr U to(T &&value)
Definition conversion.h:56
Definition std_variant_helpers.h:35