2#ifndef _TINYPBRT_COMMON_HPP_
3#define _TINYPBRT_COMMON_HPP_
33 inline constexpr bool are_distinct_v =
true;
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...>;
41 using optional_variant = std::optional<std::variant<Ts...>>;
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));
51 if constexpr (std::is_void_v<return_type>) {
52 if (variant.has_value()) { std::visit(std::forward<Visitor>(visitor), *variant); }
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));
61 template<
class Derived,
class Native>
65 using native_type = Native;
69 const native_type* _native;
73 constexpr wrapper(
const native_type& native) noexcept : _native(&native) {}
76 wrapper(
const native_type&&) =
delete;
79 [[nodiscard]]
constexpr const native_type& native()
const noexcept {
return *_native; }
82 [[nodiscard]]
constexpr operator const native_type&()
const noexcept {
return *_native; }
85 [[nodiscard]]
constexpr auto operator<=>(
const wrapper& rhs)
const noexcept
86 requires requires (
const Derived& d) { d.reflect(); }
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();
95 [[nodiscard]]
constexpr bool_t operator==(
const wrapper& rhs)
const noexcept
96 requires requires (
const Derived& d) { d.reflect(); }
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();
105 template<
class Derived,
class... Ts>
106 struct with_variant {
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)"
116 using variant_type = optional_variant<Ts...>;
122 template<
class Visitor>
123 constexpr auto visit(Visitor&& visitor)
const requires requires (
const Derived& d) { d.to_variant(); }
125 return detail::visit_optional(std::forward<Visitor>(visitor),
static_cast<const Derived&
>(*this).to_variant());
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(); }
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; }
141 requires (std::is_same_v<T, Ts> || ...)
142 [[nodiscard]]
constexpr bool_t holds()
const noexcept requires requires (
const Derived& d) { d.to_variant(); }
144 const variant_type variant =
static_cast<const Derived&
>(*this).to_variant();
145 return variant.has_value() && std::holds_alternative<T>(*variant);
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>;
161 static constexpr size_t Extent = std::extent_v<Native>;
164 using span_type = std::span<const native_element_type, Extent>;
167 [[nodiscard]]
static constexpr span_type get_span(
const Native& native)
noexcept {
return span_type(native); }
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) {
180 struct array_traits<Native, DataField> {
182 using native_element_type = std::remove_pointer_t<
decltype(Native::data)>;
185 static constexpr size_t Extent = std::dynamic_extent;
188 using span_type = std::span<const native_element_type>;
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 };
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) {
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)>>;
214 static constexpr size_t Extent = std::dynamic_extent;
217 using span_type = std::span<const native_element_type>;
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 };
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)>;
239 using native_element_type = std::remove_extent_t<field_type>;
242 static constexpr size_t Extent = std::extent_v<field_type>;
245 using span_type = std::span<const native_element_type, Extent>;
248 [[nodiscard]]
static constexpr span_type get_span(
const Native& native)
noexcept {
249 return span_type(native.*DataField);
258 template<
class NativeElement,
class Element>
259 class converting_iterator {
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;
272 const NativeElement* _ptr =
nullptr;
276 constexpr converting_iterator()
noexcept =
default;
279 constexpr explicit converting_iterator(
const NativeElement*
const ptr) noexcept : _ptr(ptr) {}
282 [[nodiscard]]
constexpr value_type operator*()
const noexcept {
return static_cast<value_type
>(*_ptr); }
285 [[nodiscard]]
constexpr value_type operator[](
const difference_type n)
const noexcept {
286 return static_cast<value_type
>(_ptr[n]);
290 constexpr converting_iterator& operator++()
noexcept {
296 constexpr converting_iterator operator++(
int)
noexcept {
297 converting_iterator copy = *
this;
303 constexpr converting_iterator& operator--()
noexcept {
309 constexpr converting_iterator operator--(
int)
noexcept {
310 converting_iterator copy = *
this;
316 constexpr converting_iterator& operator+=(
const difference_type n)
noexcept {
322 constexpr converting_iterator& operator-=(
const difference_type n)
noexcept {
328 [[nodiscard]]
friend constexpr converting_iterator operator+(converting_iterator it,
329 const difference_type n)
noexcept {
334 [[nodiscard]]
friend constexpr converting_iterator operator+(
const difference_type n,
335 converting_iterator it)
noexcept {
340 [[nodiscard]]
friend constexpr converting_iterator operator-(converting_iterator it,
341 const difference_type n)
noexcept {
346 [[nodiscard]]
friend constexpr difference_type operator-(
const converting_iterator& lhs,
347 const converting_iterator& rhs)
noexcept {
348 return lhs._ptr - rhs._ptr;
352 [[nodiscard]]
friend constexpr bool operator==(
const converting_iterator&,
353 const converting_iterator&)
noexcept =
default;
356 [[nodiscard]]
friend constexpr auto operator<=>(
const converting_iterator&,
357 const converting_iterator&)
noexcept =
default;
361 [[nodiscard]]
inline tpbrt_string_t to_native_string(
const std::string_view str)
noexcept {
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; }
375 return Element(*found);
379 struct array_base {};
386 struct string : detail::wrapper<string, tpbrt_string_t> {
388 using wrapper::wrapper;
391 [[nodiscard]]
constexpr const char_t*
data() const noexcept {
return _native->data; }
394 [[nodiscard]]
constexpr size_t size() const noexcept {
return _native->size; }
397 [[nodiscard]]
constexpr size_t length() const noexcept {
return size(); }
405 if (data_ptr ==
nullptr) {
return {}; }
406 return std::string_view(data_ptr,
size());
410 [[nodiscard]]
constexpr operator std::string_view() const noexcept {
return to_string_view(); }
419 [[nodiscard]]
constexpr std::strong_ordering
operator<=>(
const string& rhs)
const noexcept {
429 [[nodiscard]]
constexpr std::strong_ordering
operator<=>(
const std::string_view rhs)
const noexcept {
446 template<
class Native,
class Element, auto DataField>
447 struct array : detail::wrapper<array<Native, Element, DataField>, Native>,
450 using detail::wrapper<
array, Native>::wrapper;
453 using traits = detail::array_traits<Native, DataField>;
468 static constexpr size_t Extent = traits::Extent;
475 detail::converting_iterator<native_element_type, element_type>>;
478 using reference = std::conditional_t<IsIdentity, const native_element_type&, element_type>;
482 using detail::wrapper<
array, Native>::_native;
489 [[nodiscard]]
constexpr size_t size() const noexcept {
return _raw_span().size(); }
532 return std::lexicographical_compare_three_way(
begin(),
end(), rhs.begin(), rhs.end());
537 return std::ranges::equal(
begin(),
end(), rhs.begin(), rhs.end());
554requires std::derived_from<T, tpbrt::detail::array_base>
555inline constexpr bool std::ranges::enable_borrowed_range<T> =
true;
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