HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfSpan< T > Class Template Reference

#include <span.h>

Public Types

using element_type = T
 
using value_type = typename std::remove_cv< T >::type
 
using pointer = T *
 
using reference = T &
 
using index_type = std::size_t
 
using difference_type = std::ptrdiff_t
 
using iterator = T *
 
using const_iterator = const T *
 
using reverse_iterator = std::reverse_iterator< iterator >
 
using const_reverse_iterator = std::reverse_iterator< const_iterator >
 

Public Member Functions

 TfSpan () noexcept=default
 
 TfSpan (pointer ptr, index_type count)
 
 TfSpan (pointer first, pointer last)
 Construct a span over the range [first, last). More...
 
template<class Container >
 TfSpan (Container &cont, typename std::enable_if< !std::is_const< element_type >::value &&std::is_same< typename Container::value_type, value_type >::value, Container >::type *=0)
 
template<class Container >
 TfSpan (const Container &cont, typename std::enable_if< std::is_same< typename Container::value_type, value_type >::value, Container >::type *=0)
 
pointer data () const noexcept
 Return a pointer to the first element of the span. More...
 
index_type size () const noexcept
 Return the total number of elements in the span. More...
 
bool empty () const noexcept
 Returns true if this span contains no elements, false otherwise. More...
 
reference operator[] (index_type idx) const
 
reference front () const
 Return a reference to the first element in the span. More...
 
reference back () const
 Return a reference to the last element in the span. More...
 
iterator begin () const noexcept
 Returns a non-const iterator the start of the span. More...
 
const_iterator cbegin () const noexcept
 Returns a cons iterator to the start of the span. More...
 
iterator end () const noexcept
 Returns a non-const iterator to the end of the span. More...
 
const_iterator cend () const noexcept
 Returns a const iterator to the end of the span. More...
 
reverse_iterator rbegin () const noexcept
 Returns a non-const reverse iterator the start of the span. More...
 
const_reverse_iterator crbegin () const noexcept
 Returns a cons reverse iterator to the start of the span. More...
 
reverse_iterator rend () const noexcept
 Returns a non-const reverse iterator to the end of the span. More...
 
const_reverse_iterator crend () const noexcept
 Returns a const reverse iterator to the end of the span. More...
 
TfSpan< Tsubspan (difference_type offset, difference_type count=-1) const
 
TfSpan< Tfirst (size_t count) const
 Return a subspan consisting of the first count elements of this span. More...
 
TfSpan< Tlast (size_t count) const
 Return a subspan consisting of the last count elements of this span. More...
 

Detailed Description

template<typename T>
class TfSpan< T >

Represents a range of contiguous elements.

This simply pairs a pointer with a size, while adding a common array interface.

A span allows ranges of elements to be referenced in a container-neutral manner. While it is possible to achieve that effect by simply passing around raw pointers, a span has the advantage of carrying around additional size information, both enabling use of common array patterns, as well as providing sufficient information to perform boundary tests.

A TfSpan is implicitly convertible from common array types, as well as from other spans, but preserves const-ness:

std::vector<int> data;
TfSpan<int> span(data); // Okay
VtIntArray data;
TfSpan<int> span = data; // Okay
const std::vector<int> data;
TfSpan<const int> span = data; // Okay
const std::vector<int> data;
TfSpan<int> span = data; // Error! Discards cv-qualifier.

Helper methods TfMakeSpan and TfMakeConstSpan are also provided to enable auto-typing when constructing spans:

VtIntArray data;
auto readOnlySpan = TfMakeConstSpan(data); // TfSpan<const int>
auto readWriteSpan = TfMakeSpan(data); // TfSpan<int>

Spans do not own the data they reference. It is up to the user of the span to ensure that the underlying data is not destructed while the span is in use.

This is modelled after std::span (C++20), but does not currently include any specialization for static extents.

Definition at line 87 of file span.h.

Member Typedef Documentation

template<typename T>
using TfSpan< T >::const_iterator = const T*

Definition at line 98 of file span.h.

template<typename T>
using TfSpan< T >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Definition at line 100 of file span.h.

template<typename T>
using TfSpan< T >::difference_type = std::ptrdiff_t

Definition at line 95 of file span.h.

template<typename T>
using TfSpan< T >::element_type = T

Definition at line 90 of file span.h.

template<typename T>
using TfSpan< T >::index_type = std::size_t

Definition at line 94 of file span.h.

template<typename T>
using TfSpan< T >::iterator = T*

Definition at line 97 of file span.h.

template<typename T>
using TfSpan< T >::pointer = T*

Definition at line 92 of file span.h.

template<typename T>
using TfSpan< T >::reference = T&

Definition at line 93 of file span.h.

template<typename T>
using TfSpan< T >::reverse_iterator = std::reverse_iterator<iterator>

Definition at line 99 of file span.h.

template<typename T>
using TfSpan< T >::value_type = typename std::remove_cv<T>::type

Definition at line 91 of file span.h.

Constructor & Destructor Documentation

template<typename T>
TfSpan< T >::TfSpan ( )
defaultnoexcept
template<typename T>
TfSpan< T >::TfSpan ( pointer  ptr,
index_type  count 
)
inline

Construct a span over the range of [ptr, ptr+count). In debug builds, a runtime assertion will fail if count > 0 and ptr is null. The behavior is otherwise undefined for invalid ranges.

Definition at line 107 of file span.h.

template<typename T>
TfSpan< T >::TfSpan ( pointer  first,
pointer  last 
)
inline

Construct a span over the range [first, last).

Definition at line 114 of file span.h.

template<typename T>
template<class Container >
TfSpan< T >::TfSpan ( Container &  cont,
typename std::enable_if< !std::is_const< element_type >::value &&std::is_same< typename Container::value_type, value_type >::value, Container >::type = 0 
)
inline

Construct a span from a container. The resulting span has a range of [cont.data(), cont.data()+cont.size())

Definition at line 124 of file span.h.

template<typename T>
template<class Container >
TfSpan< T >::TfSpan ( const Container &  cont,
typename std::enable_if< std::is_same< typename Container::value_type, value_type >::value, Container >::type = 0 
)
inline

Construct a span from a container. The resulting span has a range of [cont.data(), cont.data()+cont.size())

Definition at line 139 of file span.h.

Member Function Documentation

template<typename T>
reference TfSpan< T >::back ( void  ) const
inline

Return a reference to the last element in the span.

Definition at line 173 of file span.h.

template<typename T>
iterator TfSpan< T >::begin ( void  ) const
inlinenoexcept

Returns a non-const iterator the start of the span.

Definition at line 179 of file span.h.

template<typename T>
const_iterator TfSpan< T >::cbegin ( ) const
inlinenoexcept

Returns a cons iterator to the start of the span.

Definition at line 182 of file span.h.

template<typename T>
const_iterator TfSpan< T >::cend ( ) const
inlinenoexcept

Returns a const iterator to the end of the span.

Definition at line 188 of file span.h.

template<typename T>
const_reverse_iterator TfSpan< T >::crbegin ( ) const
inlinenoexcept

Returns a cons reverse iterator to the start of the span.

Definition at line 195 of file span.h.

template<typename T>
const_reverse_iterator TfSpan< T >::crend ( ) const
inlinenoexcept

Returns a const reverse iterator to the end of the span.

Definition at line 203 of file span.h.

template<typename T>
pointer TfSpan< T >::data ( ) const
inlinenoexcept

Return a pointer to the first element of the span.

Definition at line 150 of file span.h.

template<typename T>
bool TfSpan< T >::empty ( void  ) const
inlinenoexcept

Returns true if this span contains no elements, false otherwise.

Definition at line 156 of file span.h.

template<typename T>
iterator TfSpan< T >::end ( void  ) const
inlinenoexcept

Returns a non-const iterator to the end of the span.

Definition at line 185 of file span.h.

template<typename T>
TfSpan<T> TfSpan< T >::first ( size_t  count) const
inline

Return a subspan consisting of the first count elements of this span.

Definition at line 222 of file span.h.

template<typename T>
reference TfSpan< T >::front ( void  ) const
inline

Return a reference to the first element in the span.

Definition at line 167 of file span.h.

template<typename T>
TfSpan<T> TfSpan< T >::last ( size_t  count) const
inline

Return a subspan consisting of the last count elements of this span.

Definition at line 227 of file span.h.

template<typename T>
reference TfSpan< T >::operator[] ( index_type  idx) const
inline

Returns a reference to the idx'th element of the span. In debug builds, a runtime assertion will fail if idx is out of range. The behavior is otherwise undefined if idx is out of range.

Definition at line 161 of file span.h.

template<typename T>
reverse_iterator TfSpan< T >::rbegin ( ) const
inlinenoexcept

Returns a non-const reverse iterator the start of the span.

Definition at line 191 of file span.h.

template<typename T>
reverse_iterator TfSpan< T >::rend ( ) const
inlinenoexcept

Returns a non-const reverse iterator to the end of the span.

Definition at line 199 of file span.h.

template<typename T>
index_type TfSpan< T >::size ( void  ) const
inlinenoexcept

Return the total number of elements in the span.

Definition at line 153 of file span.h.

template<typename T>
TfSpan<T> TfSpan< T >::subspan ( difference_type  offset,
difference_type  count = -1 
) const
inline

Returns a new span referencing a sub-range of this span. If count == -1 (or std::dynamic_extent in C++20), the new span has a range of [data()+offset, data()+size()). Otherwise, the new span has range [data()+offset, data()+offset+count).

Definition at line 210 of file span.h.


The documentation for this class was generated from the following file: