tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
common.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _TINYPBRT_COMMON_HPP_
3#define _TINYPBRT_COMMON_HPP_
4
6
8
9#include <algorithm>
10#include <compare>
11#include <cstddef>
12#include <iterator>
13#include <optional>
14#include <ranges>
15#include <span>
16#include <string_view>
17#include <tuple>
18#include <type_traits>
19#include <utility>
20#include <variant>
21
23
24namespace tpbrt {
30 namespace detail {
32 template<class... Ts>
33 inline constexpr bool are_distinct_v = true; // NOLINT
34
36 template<class T, class... Ts>
37 inline constexpr bool are_distinct_v<T, Ts...> = (!std::is_same_v<T, Ts> && ...) && are_distinct_v<Ts...>; // NOLINT
38
40 template<class... Ts>
41 using optional_variant = std::optional<std::variant<Ts...>>;
42
47 template<class Visitor, class... Ts>
48 constexpr auto visit_optional(Visitor&& visitor, const std::optional<std::variant<Ts...>>& variant) {
49 using return_type = decltype(std::visit(std::forward<Visitor>(visitor), *variant));
50
51 if constexpr (std::is_void_v<return_type>) {
52 if (variant.has_value()) { std::visit(std::forward<Visitor>(visitor), *variant); }
53 }
54 else {
55 using value_type = std::remove_cvref_t<return_type>;
56 if (!variant.has_value()) { return std::optional<value_type>(std::nullopt); }
57 return std::optional<value_type>(std::visit(std::forward<Visitor>(visitor), *variant));
58 }
59 }
60
61 template<class Derived, class Native>
62 class wrapper {
63 public:
65 using native_type = Native;
66
67 protected:
69 const native_type* _native;
70
71 public:
73 constexpr wrapper(const native_type& native) noexcept : _native(&native) {} // NOLINT
74
76 wrapper(const native_type&&) = delete; // NOLINT
77
79 [[nodiscard]] constexpr const native_type& native() const noexcept { return *_native; }
80
82 [[nodiscard]] constexpr operator const native_type&() const noexcept { return *_native; } // NOLINT
83
85 [[nodiscard]] constexpr auto operator<=>(const wrapper& rhs) const noexcept
86 requires requires (const Derived& d) { d.reflect(); }
87 {
88 static_assert(std::is_base_of_v<wrapper, Derived>, "Derived must be the type which inherits this wrapper (CRTP)");
89 const auto& derived_lhs = static_cast<const Derived&>(*this);
90 const auto& derived_rhs = static_cast<const Derived&>(rhs);
91 return derived_lhs.reflect() <=> derived_rhs.reflect();
92 }
93
95 [[nodiscard]] constexpr bool_t operator==(const wrapper& rhs) const noexcept
96 requires requires (const Derived& d) { d.reflect(); }
97 {
98 static_assert(std::is_base_of_v<wrapper, Derived>, "Derived must be the type which inherits this wrapper (CRTP)");
99 const auto& derived_lhs = static_cast<const Derived&>(*this);
100 const auto& derived_rhs = static_cast<const Derived&>(rhs);
101 return derived_lhs.reflect() == derived_rhs.reflect();
102 }
103 };
104
105 template<class Derived, class... Ts>
106 struct with_variant {
107 // clang-format off
108 static_assert(
109 are_distinct_v<Ts...>,
110 "Variant alternatives must be distinct types, otherwise std::visit cannot tell them apart "
111 "(use a separate wrapper type, e.g. tagged_params, instead of a type alias)"
112 );
113 // clang-format on
114
116 using variant_type = optional_variant<Ts...>;
117
122 template<class Visitor>
123 constexpr auto visit(Visitor&& visitor) const requires requires (const Derived& d) { d.to_variant(); }
124 {
125 return detail::visit_optional(std::forward<Visitor>(visitor), static_cast<const Derived&>(*this).to_variant());
126 }
127
129 template<class T>
130 requires (std::is_same_v<T, Ts> || ...)
131 [[nodiscard]] constexpr std::optional<T> as() const noexcept requires requires (const Derived& d) { d.to_variant(); }
132 {
133 const variant_type variant = static_cast<const Derived&>(*this).to_variant();
134 if (!variant.has_value()) { return std::nullopt; }
135 if (const T* const value = std::get_if<T>(&*variant); value != nullptr) { return *value; }
136 return std::nullopt;
137 }
138
140 template<class T>
141 requires (std::is_same_v<T, Ts> || ...)
142 [[nodiscard]] constexpr bool_t holds() const noexcept requires requires (const Derived& d) { d.to_variant(); }
143 {
144 const variant_type variant = static_cast<const Derived&>(*this).to_variant();
145 return variant.has_value() && std::holds_alternative<T>(*variant);
146 }
147 };
148
154 template<class Native, auto DataField>
155 requires std::is_array_v<Native>
156 struct array_traits<Native, DataField> {
158 using native_element_type = std::remove_extent_t<Native>;
159
161 static constexpr size_t Extent = std::extent_v<Native>;
162
164 using span_type = std::span<const native_element_type, Extent>;
165
167 [[nodiscard]] static constexpr span_type get_span(const Native& native) noexcept { return span_type(native); }
168 };
169
175 template<class Native, auto DataField>
176 requires (!std::is_array_v<Native>) && std::is_same_v<decltype(DataField), std::nullptr_t> && requires (const Native& n) {
177 n.data;
178 n.count;
179 }
180 struct array_traits<Native, DataField> {
182 using native_element_type = std::remove_pointer_t<decltype(Native::data)>;
183
185 static constexpr size_t Extent = std::dynamic_extent;
186
188 using span_type = std::span<const native_element_type>;
189
191 [[nodiscard]] static constexpr span_type get_span(const Native& native) noexcept {
192 const native_element_type* ptr = native.data;
193 return (ptr == nullptr || native.count == 0) ? span_type {} : span_type { ptr, native.count };
194 }
195 };
196
202 template<class Native, auto DataField>
203 requires (!std::is_array_v<Native>) && (!std::is_same_v<decltype(DataField), std::nullptr_t>) &&
204 requires (const Native& n) {
205 n.*DataField;
206 n.count;
207 } && std::is_pointer_v<std::remove_cvref_t<decltype(std::declval<const Native&>().*DataField)>>
208 struct array_traits<Native, DataField> {
210 using native_element_type =
211 std::remove_pointer_t<std::remove_cvref_t<decltype(std::declval<const Native&>().*DataField)>>;
212
214 static constexpr size_t Extent = std::dynamic_extent;
215
217 using span_type = std::span<const native_element_type>;
218
220 [[nodiscard]] static constexpr span_type get_span(const Native& native) noexcept {
221 const native_element_type* ptr = native.*DataField;
222 return (ptr == nullptr || native.count == 0) ? span_type {} : span_type { ptr, native.count };
223 }
224 };
225
231 template<class Native, auto DataField>
232 requires (!std::is_array_v<Native>) && (!std::is_same_v<decltype(DataField), std::nullptr_t>) &&
233 std::is_array_v<std::remove_cvref_t<decltype(std::declval<const Native&>().*DataField)>>
234 struct array_traits<Native, DataField> {
236 using field_type = std::remove_cvref_t<decltype(std::declval<const Native&>().*DataField)>;
237
239 using native_element_type = std::remove_extent_t<field_type>;
240
242 static constexpr size_t Extent = std::extent_v<field_type>;
243
245 using span_type = std::span<const native_element_type, Extent>;
246
248 [[nodiscard]] static constexpr span_type get_span(const Native& native) noexcept {
249 return span_type(native.*DataField);
250 }
251 };
252
258 template<class NativeElement, class Element>
259 class converting_iterator {
260 public:
262 using iterator_concept = std::random_access_iterator_tag;
264 using iterator_category = std::input_iterator_tag;
266 using value_type = Element;
268 using difference_type = std::ptrdiff_t;
269
270 private:
272 const NativeElement* _ptr = nullptr;
273
274 public:
276 constexpr converting_iterator() noexcept = default;
277
279 constexpr explicit converting_iterator(const NativeElement* const ptr) noexcept : _ptr(ptr) {}
280
282 [[nodiscard]] constexpr value_type operator*() const noexcept { return static_cast<value_type>(*_ptr); }
283
285 [[nodiscard]] constexpr value_type operator[](const difference_type n) const noexcept {
286 return static_cast<value_type>(_ptr[n]);
287 }
288
290 constexpr converting_iterator& operator++() noexcept {
291 ++_ptr;
292 return *this;
293 }
294
296 constexpr converting_iterator operator++(int) noexcept {
297 converting_iterator copy = *this;
298 ++_ptr;
299 return copy;
300 }
301
303 constexpr converting_iterator& operator--() noexcept {
304 --_ptr;
305 return *this;
306 }
307
309 constexpr converting_iterator operator--(int) noexcept {
310 converting_iterator copy = *this;
311 --_ptr;
312 return copy;
313 }
314
316 constexpr converting_iterator& operator+=(const difference_type n) noexcept {
317 _ptr += n;
318 return *this;
319 }
320
322 constexpr converting_iterator& operator-=(const difference_type n) noexcept {
323 _ptr -= n;
324 return *this;
325 }
326
328 [[nodiscard]] friend constexpr converting_iterator operator+(converting_iterator it,
329 const difference_type n) noexcept {
330 return it += n;
331 }
332
334 [[nodiscard]] friend constexpr converting_iterator operator+(const difference_type n,
335 converting_iterator it) noexcept {
336 return it += n;
337 }
338
340 [[nodiscard]] friend constexpr converting_iterator operator-(converting_iterator it,
341 const difference_type n) noexcept {
342 return it -= n;
343 }
344
346 [[nodiscard]] friend constexpr difference_type operator-(const converting_iterator& lhs,
347 const converting_iterator& rhs) noexcept {
348 return lhs._ptr - rhs._ptr;
349 }
350
352 [[nodiscard]] friend constexpr bool operator==(const converting_iterator&,
353 const converting_iterator&) noexcept = default;
354
356 [[nodiscard]] friend constexpr auto operator<=>(const converting_iterator&,
357 const converting_iterator&) noexcept = default;
358 };
359
361 [[nodiscard]] inline tpbrt_string_t to_native_string(const std::string_view str) noexcept {
362 return tpbrt_string_t { .data = str.data(), .size = str.size() };
363 }
364
370 template<class Element, class NativeElement, class Fn, class... Args>
371 [[nodiscard]] result<Element> find_in(Fn fn, const Args&... args) noexcept {
372 const NativeElement* found = nullptr;
373 if (const auto err = static_cast<error>(fn(args..., &found)); err != error::NONE) { return err; }
374 if (found == nullptr) { return error::NOT_FOUND; }
375 return Element(*found);
376 }
377
379 struct array_base {};
380 } // namespace detail
381
386 struct string : detail::wrapper<string, tpbrt_string_t> {
388 using wrapper::wrapper;
389
391 [[nodiscard]] constexpr const char_t* data() const noexcept { return _native->data; }
392
394 [[nodiscard]] constexpr size_t size() const noexcept { return _native->size; }
395
397 [[nodiscard]] constexpr size_t length() const noexcept { return size(); }
398
400 [[nodiscard]] constexpr bool_t empty() const noexcept { return data() == nullptr || size() == 0; }
401
403 [[nodiscard]] constexpr std::string_view to_string_view() const noexcept {
404 const char_t* data_ptr = data();
405 if (data_ptr == nullptr) { return {}; }
406 return std::string_view(data_ptr, size());
407 }
408
410 [[nodiscard]] constexpr operator std::string_view() const noexcept { return to_string_view(); } // NOLINT
411
413 [[nodiscard]] constexpr auto begin() const noexcept { return to_string_view().begin(); }
414
416 [[nodiscard]] constexpr auto end() const noexcept { return to_string_view().end(); }
417
419 [[nodiscard]] constexpr std::strong_ordering operator<=>(const string& rhs) const noexcept {
420 return to_string_view() <=> rhs.to_string_view();
421 }
422
424 [[nodiscard]] constexpr bool_t operator==(const string& rhs) const noexcept {
425 return to_string_view() == rhs.to_string_view();
426 }
427
429 [[nodiscard]] constexpr std::strong_ordering operator<=>(const std::string_view rhs) const noexcept {
430 return to_string_view() <=> rhs;
431 }
432
434 [[nodiscard]] constexpr bool_t operator==(const std::string_view rhs) const noexcept { return to_string_view() == rhs; }
435 };
436
446 template<class Native, class Element, auto DataField>
447 struct array : detail::wrapper<array<Native, Element, DataField>, Native>,
448 detail::array_base {
450 using detail::wrapper<array, Native>::wrapper;
451
453 using traits = detail::array_traits<Native, DataField>;
454
456 using native_element_type = traits::native_element_type;
457
459 using element_type = std::conditional_t<std::is_void_v<Element>, native_element_type, Element>;
460
463
465 using span_type = traits::span_type;
466
468 static constexpr size_t Extent = traits::Extent;
469
471 static constexpr bool_t IsIdentity = std::is_same_v<native_element_type, element_type>;
472
474 using iterator = std::conditional_t<IsIdentity, typename span_type::iterator,
475 detail::converting_iterator<native_element_type, element_type>>;
476
478 using reference = std::conditional_t<IsIdentity, const native_element_type&, element_type>;
479
480 protected:
482 using detail::wrapper<array, Native>::_native;
483
485 [[nodiscard]] constexpr span_type _raw_span() const noexcept { return traits::get_span(*_native); }
486
487 public:
489 [[nodiscard]] constexpr size_t size() const noexcept { return _raw_span().size(); }
490
492 [[nodiscard]] constexpr bool_t empty() const noexcept { return _raw_span().empty(); }
493
495 [[nodiscard]] constexpr const native_element_type* data() const noexcept { return _raw_span().data(); }
496
498 [[nodiscard]] constexpr span_type to_span() const noexcept { return _raw_span(); }
499
501 [[nodiscard]] constexpr iterator begin() const noexcept {
502 if constexpr (IsIdentity) { return _raw_span().begin(); }
503 else { return iterator(_raw_span().data()); }
504 }
505
507 [[nodiscard]] constexpr iterator end() const noexcept {
508 if constexpr (IsIdentity) { return _raw_span().end(); }
509 else { return iterator(_raw_span().data() + _raw_span().size()); }
510 }
511
513 [[nodiscard]] constexpr reference operator[](const size_t index) const noexcept {
514 if constexpr (IsIdentity) { return _raw_span()[index]; }
515 else { return static_cast<element_type>(_raw_span()[index]); }
516 }
517
519 [[nodiscard]] constexpr result<element_type> at(const size_t index) const noexcept {
520 if (index >= size()) { return error::INDEX_OUT_OF_RANGE; }
521 return element_type((*this)[index]);
522 }
523
525 [[nodiscard]] constexpr reference front() const noexcept { return (*this)[0]; }
526
528 [[nodiscard]] constexpr reference back() const noexcept { return (*this)[size() - 1]; }
529
531 [[nodiscard]] constexpr auto operator<=>(const array& rhs) const noexcept {
532 return std::lexicographical_compare_three_way(begin(), end(), rhs.begin(), rhs.end());
533 }
534
536 [[nodiscard]] constexpr bool_t operator==(const array& rhs) const noexcept {
537 return std::ranges::equal(begin(), end(), rhs.begin(), rhs.end());
538 }
539 };
540
545 struct string_array : array<tpbrt_string_array_t, string> {
547 using array::array;
548 };
549} // namespace tpbrt
550
551// Every tpbrt::array (and every type derived from it) is a view whose iterators do not depend on the view object.
553template<class T>
554requires std::derived_from<T, tpbrt::detail::array_base>
555inline constexpr bool std::ranges::enable_borrowed_range<T> = true; // NOLINT
557
558#endif // _TINYPBRT_COMMON_HPP_
tpbrt_char_t char_t
Char type.
Definition fwd.hpp:29
error
Error type (used as result type where success = TPBRT_ERROR_NONE = 0).
Definition error.hpp:25
@ INDEX_OUT_OF_RANGE
Index out of range.
@ NOT_FOUND
Something was not found. For example texture with given name.
@ NONE
No error -> Success.
bool bool_t
Bool type.
Definition fwd.hpp:82
Definition accelerator.hpp:14
Generic C++ array view with type conversion (a non owning view on memory owned by the C library).
Definition common.hpp:448
constexpr span_type _raw_span() const noexcept
Returns std::span over the C data.
Definition common.hpp:485
constexpr reference operator[](const size_t index) const noexcept
Returns element on given index (unchecked, like std::span).
Definition common.hpp:513
constexpr reference front() const noexcept
Returns first element (unchecked).
Definition common.hpp:525
constexpr iterator end() const noexcept
Returns end iterator.
Definition common.hpp:507
std::conditional_t< IsIdentity, typename span_type::iterator, detail::converting_iterator< native_element_type, element_type > > iterator
Iterator type.
Definition common.hpp:475
std::conditional_t< std::is_void_v< Element >, native_element_type, Element > element_type
C++ array view element type.
Definition common.hpp:459
detail::array_traits< Native, DataField > traits
Array traits.
Definition common.hpp:453
constexpr auto operator<=>(const array &rhs) const noexcept
Three-way comparison operator (compares elements).
Definition common.hpp:531
constexpr span_type to_span() const noexcept
Returns std::span over the C data.
Definition common.hpp:498
traits::native_element_type native_element_type
Native C library array element type.
Definition common.hpp:456
traits::span_type span_type
std::span type of this array.
Definition common.hpp:465
std::conditional_t< IsIdentity, const native_element_type &, element_type > reference
Type returned by operator[] (a reference for identity arrays, a converted value otherwise).
Definition common.hpp:478
constexpr size_t size() const noexcept
Returns size of this array.
Definition common.hpp:489
static constexpr bool_t IsIdentity
True when elements are returned without conversion (as references to the C data).
Definition common.hpp:471
constexpr const native_element_type * data() const noexcept
Returns pointer to the C data.
Definition common.hpp:495
constexpr bool_t operator==(const array &rhs) const noexcept
Equality operator (compares elements).
Definition common.hpp:536
constexpr bool_t empty() const noexcept
Returns true if array is empty.
Definition common.hpp:492
constexpr iterator begin() const noexcept
Returns begin iterator.
Definition common.hpp:501
constexpr result< element_type > at(const size_t index) const noexcept
Returns element on given index or error::INDEX_OUT_OF_RANGE.
Definition common.hpp:519
element_type value_type
Value type (as in std::span).
Definition common.hpp:462
constexpr reference back() const noexcept
Returns last element (unchecked).
Definition common.hpp:528
static constexpr size_t Extent
Array static size.
Definition common.hpp:468
Result of an operation: either a value or an error code (similar to C++23 std::expected).
Definition error.hpp:165
C++ wrapper for tpbrt_string_array_t.
Definition common.hpp:545
C++ wrapper for tpbrt_string_t (a view on characters owned by the C library).
Definition common.hpp:386
constexpr size_t size() const noexcept
Returns size value.
Definition common.hpp:394
constexpr std::strong_ordering operator<=>(const std::string_view rhs) const noexcept
Three-way comparison operator (compares characters).
Definition common.hpp:429
constexpr bool_t operator==(const string &rhs) const noexcept
Equality operator (compares characters).
Definition common.hpp:424
constexpr size_t length() const noexcept
Returns string length.
Definition common.hpp:397
constexpr bool_t empty() const noexcept
Returns true if string is empty.
Definition common.hpp:400
constexpr bool_t operator==(const std::string_view rhs) const noexcept
Equality operator (compares characters).
Definition common.hpp:434
constexpr const char_t * data() const noexcept
Returns data value.
Definition common.hpp:391
constexpr auto begin() const noexcept
Returns begin iterator.
Definition common.hpp:413
constexpr std::string_view to_string_view() const noexcept
Conversion to std::string_view.
Definition common.hpp:403
constexpr auto end() const noexcept
Returns end iterator.
Definition common.hpp:416
constexpr std::strong_ordering operator<=>(const string &rhs) const noexcept
Three-way comparison operator (compares characters).
Definition common.hpp:419
Simple string representation (not always terminated with '\0', the size determines the length of the ...
Definition common.h:20
const tpbrt_char_t * data
String characters.
Definition common.h:22