Bifrost SDK
Bifrost SDK documentation
Span.h
Go to the documentation of this file.
1//-
2//*****************************************************************************
3// Copyright (c) 2025 Autodesk, Inc. All rights reserved.
4//
5// These coded instructions, statements, and computer programs contain
6// unpublished proprietary information written by Autodesk, Inc. and are
7// protected by Federal copyright law. They may not be disclosed to third
8// parties or copied or duplicated in any form, in whole or in part, without
9// the prior written consent of Autodesk, Inc.
10//*****************************************************************************
11//+
12
14
15#ifndef AMINO_CORE_SPAN_H
16#define AMINO_CORE_SPAN_H
17
18#include <cassert>
19#include <cstddef>
20#include <type_traits>
21
22namespace Amino {
23
24//==============================================================================
25// CLASS Span
26//==============================================================================
27
33// LCOV_EXCL_BR_START
34template <typename T>
35class Span {
36private:
37 template <typename R>
38 static inline constexpr bool is_span =
39 std::is_same_v<std::remove_cv_t<std::remove_reference_t<R>>, Span<T>>;
40
41public:
42 /*----- types -----*/
43
44 using element_type = T;
45 using value_type = std::remove_cv_t<T>;
46 using iterator = T*;
47 using const_iterator = T const*;
48 using size_type = size_t;
49 using difference_type = ptrdiff_t;
50
51 /*----- member functions -----*/
52
54 constexpr Span() noexcept = default;
55
57 constexpr Span(T* data, size_type size)
58 : m_begin{data}, m_end{internal_get_end(data, size)} {
59 assert(data || size == 0);
60 }
61
64 constexpr Span(T* begin, T* end) : m_begin{begin}, m_end{end} {
65 assert(begin <= end);
66 }
67
68 template <typename R, typename = std::enable_if_t<!is_span<R>>>
69 constexpr explicit Span(R&& r)
70 : m_begin{r.data()}, m_end{m_begin + r.size()} {}
71
74 constexpr Span(std::nullptr_t, size_type) = delete;
75 constexpr Span(T*, std::nullptr_t) = delete;
76 constexpr Span(std::nullptr_t, T*) = delete;
78
80 constexpr bool empty() const { return m_begin == m_end; }
81
86 constexpr T const& operator[](size_type i) const {
87 assert(i < size());
88 return m_begin[i];
89 }
90 constexpr T& operator[](size_type i) {
91 assert(i < size());
92 return m_begin[i];
93 }
95
100 constexpr T const& front() const {
101 assert(!empty());
102 return *m_begin;
103 }
104 constexpr T& front() {
105 assert(!empty());
106 return *m_begin;
107 }
109
114 constexpr T const& back() const {
115 assert(!empty());
116 return *(m_end - 1);
117 }
118 constexpr T& back() {
119 assert(!empty());
120 return *(m_end - 1);
121 }
123
126 constexpr const_iterator cbegin() const { return m_begin; }
127 constexpr const_iterator begin() const { return m_begin; }
128 constexpr iterator begin() { return m_begin; }
130
133 constexpr const_iterator cend() const { return m_end; }
134 constexpr const_iterator end() const { return m_end; }
135 constexpr iterator end() { return m_end; }
137
140 constexpr T* data() const { return m_begin; }
141
143 constexpr size_type size() const {
144 return static_cast<size_type>(m_end - m_begin);
145 }
146
147private:
148 constexpr static T* internal_get_end(T* data, size_type size) {
149 if (data) // separate line for coverage
150 return data + size;
151 return nullptr;
152 }
153
154 /*----- data members -----*/
155
156 T* m_begin{nullptr};
157 T* m_end{nullptr};
158};
159// LCOV_EXCL_BR_STOP
160
163template <typename T>
164Span(T*, size_t) -> Span<T>;
165template <typename T>
166Span(T*, T*) -> Span<T>;
167template <typename R>
170
171//==============================================================================
172// CLASS SpanParam
173//==============================================================================
174
177template <typename T>
178class SpanParam : public Span<T> {
179private:
180 template <typename R>
181 static inline constexpr bool is_span_param =
182 std::is_same_v<std::remove_cv_t<std::remove_reference_t<R>>, Span<T>>;
183
184public:
185 using Span<T>::Span;
186
187 template <typename R, typename = std::enable_if_t<!is_span_param<R>>>
188 // NOLINTNEXTLINE(google-explicit-constructor)
189 /*implicit*/ constexpr SpanParam(R&& r) : Span<T>{r} {}
190
191 // NOLINTNEXTLINE(google-explicit-constructor)
192 /*implicit*/ constexpr SpanParam(Span<T> const& other) : Span<T>{other} {}
193};
194
197template <typename T>
198SpanParam(T*, size_t) -> SpanParam<T>;
199template <typename T>
201template <typename R>
205
206} // namespace Amino
207
208#endif
Definition: HostData.h:33
SpanParam(T *, size_t) -> SpanParam< T >
Deduction guides for SpanParam.
Span(T *, size_t) -> Span< T >
Deduction guides for Span.
The class template span describes an object that can refer to a contiguous sequence of objects with t...
Definition: Span.h:35
size_t size_type
Definition: Span.h:48
constexpr bool empty() const
Check if the Span is empty.
Definition: Span.h:80
ptrdiff_t difference_type
Definition: Span.h:49
constexpr Span(std::nullptr_t, T *)=delete
Span is not constructible from a nullptr.
constexpr T const & operator[](size_type i) const
Access the ith element.
Definition: Span.h:86
constexpr const_iterator cbegin() const
Returns an iterator to the beginning of the Span.
Definition: Span.h:126
constexpr iterator begin()
Returns an iterator to the beginning of the Span.
Definition: Span.h:128
constexpr const_iterator cend() const
Returns an iterator to the end of the Span.
Definition: Span.h:133
constexpr Span() noexcept=default
Default constructor (empty Span).
T * iterator
Definition: Span.h:46
constexpr Span(std::nullptr_t, size_type)=delete
Span is not constructible from a nullptr.
constexpr Span(R &&r)
Definition: Span.h:69
T const * const_iterator
Definition: Span.h:47
constexpr T const & back() const
Access the last element.
Definition: Span.h:114
constexpr T * data() const
Direct access to the underlying contiguous storage of the Span.
Definition: Span.h:140
constexpr const_iterator end() const
Returns an iterator to the end of the Span.
Definition: Span.h:134
constexpr T & front()
Access the first element.
Definition: Span.h:104
constexpr T & back()
Access the last element.
Definition: Span.h:118
constexpr size_type size() const
Returns the number of elements in the Span.
Definition: Span.h:143
std::remove_cv_t< T > value_type
Definition: Span.h:45
constexpr T const & front() const
Access the first element.
Definition: Span.h:100
constexpr Span(T *begin, T *end)
Construct a Span from a pointer to the beginning and a pointer to the end.
Definition: Span.h:64
constexpr Span(T *, std::nullptr_t)=delete
Span is not constructible from a nullptr.
T element_type
Definition: Span.h:44
constexpr iterator end()
Returns an iterator to the end of the Span.
Definition: Span.h:135
constexpr const_iterator begin() const
Returns an iterator to the beginning of the Span.
Definition: Span.h:127
constexpr T & operator[](size_type i)
Access the ith element.
Definition: Span.h:90
Same as Span but the constructor from a range is implicit, making it more convenient and safe to use ...
Definition: Span.h:178
constexpr SpanParam(R &&r)
Definition: Span.h:189
constexpr SpanParam(Span< T > const &other)
Definition: Span.h:192