Celeritas 0.7+c0d26b9
Loading...
Searching...
No Matches
Public Member Functions | Friends | List of all members
celeritas::OpaqueId< ItemT, SizeT > Class Template Reference

Type-safe index for accessing an array or collection of data. More...

#include <OpaqueId.hh>

Public Types

Type aliases
using value_type = ItemT
 
using size_type = SizeT
 

Public Member Functions

constexpr OpaqueId ()
 Default to null state.
 
constexpr OpaqueId (size_type index)
 Construct explicitly with stored value.
 
constexpr operator bool () const
 Whether this ID is in a valid (assigned) state.
 
constexpr OpaqueIdoperator++ ()
 Pre-increment of the ID.
 
constexpr OpaqueId operator++ (int)
 Post-increment of the ID.
 
constexpr OpaqueIdoperator-- ()
 Pre-decrement of the ID.
 
constexpr OpaqueId operator-- (int)
 Post-decrement of the ID.
 
constexpr size_type get () const
 Get the ID's value.
 
constexpr size_type unchecked_get () const
 Get the value without checking for validity (atypical)
 
constexpr size_type constdata () const
 Access the underlying data for more efficient loading from memory.
 

Friends

SizeT operator- (OpaqueId self, OpaqueId other)
 Get the distance between two opaque IDs.
 
template<class U >
auto operator+ (OpaqueId id, U offset) -> std::enable_if_t< std::is_integral_v< U >, OpaqueId >
 Increment an opaque ID by an offset, checking against underflow.
 
template<class U >
auto operator+ (U offset, OpaqueId id) -> std::enable_if_t< std::is_integral_v< U >, OpaqueId >
 Increment an opaque ID by an offset (symmetric)
 
template<class U >
auto operator- (OpaqueId id, U offset) -> std::enable_if_t< std::is_integral_v< U >, OpaqueId >
 Decrement an opaque ID by an offset.
 
constexpr friend bool operator== (OpaqueId lhs, OpaqueId rhs) noexcept
 
constexpr friend bool operator!= (OpaqueId lhs, OpaqueId rhs) noexcept
 
constexpr friend bool operator< (OpaqueId lhs, OpaqueId rhs) noexcept
 
constexpr friend bool operator> (OpaqueId lhs, OpaqueId rhs) noexcept
 
constexpr friend bool operator<= (OpaqueId lhs, OpaqueId rhs) noexcept
 
constexpr friend bool operator>= (OpaqueId lhs, OpaqueId rhs) noexcept
 
template<class U >
constexpr friend auto operator< (OpaqueId lhs, U rhs) noexcept -> std::enable_if_t< std::is_unsigned_v< U >, bool >
 
template<class U >
constexpr friend auto operator<= (OpaqueId lhs, U rhs) noexcept -> std::enable_if_t< std::is_unsigned_v< U >, bool >
 

Detailed Description

template<class ItemT, class SizeT = ::celeritas::size_type>
class celeritas::OpaqueId< ItemT, SizeT >

Type-safe index for accessing an array or collection of data.

Template Parameters
ItemTType of an item at the index corresponding to this ID
SizeTUnsigned integer index

It's common for classes and functions to take multiple indices, especially for O(1) indexing for performance. By annotating these values with a type, we give them semantic meaning, and we gain compile-time type safety.

If this class is used for indexing into an array, then ValueT argument should usually be the value type of the array: Foo operator[](OpaqueId<Foo>)

An OpaqueId object evaluates to true if it has a value, or false if it does not (a "null" ID, analogous to a null pointer: it does not correspond to a valid value). A "true" ID will always compare less than a "false" ID: you can use std::partition and erase to remove invalid IDs from a vector.

See also id_cast below for checked construction of OpaqueIds from generic integer values (avoid compile-time warnings or errors from signed/truncated integers).

Note
Comparators are defined as inline friend functions to allow ADL-assisted conversion, including from LdgWrapper (see Cached device loading).
Todo:
This interface will be changed to be more like std::optional : size_type will become value_type (the value of a 'dereferenced' ID) and operator* or value will be used to access the integer.

Friends And Related Symbol Documentation

◆ operator!=

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator!= ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type

◆ operator< [1/2]

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator< ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type

◆ operator< [2/2]

template<class ItemT , class SizeT = ::celeritas::size_type>
template<class U >
constexpr friend auto operator< ( OpaqueId< ItemT, SizeT lhs,
rhs 
) -> std::enable_if_t<std::is_unsigned_v<U>, bool>
friend

Allow less-than comparison with unsigned int for containers

◆ operator<= [1/2]

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator<= ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type

◆ operator<= [2/2]

template<class ItemT , class SizeT = ::celeritas::size_type>
template<class U >
constexpr friend auto operator<= ( OpaqueId< ItemT, SizeT lhs,
rhs 
) -> std::enable_if_t<std::is_unsigned_v<U>, bool>
friend

Allow less-than comparison with unsigned int for containers

◆ operator==

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator== ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type

◆ operator>

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator> ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type

◆ operator>=

template<class ItemT , class SizeT = ::celeritas::size_type>
constexpr friend bool operator>= ( OpaqueId< ItemT, SizeT lhs,
OpaqueId< ItemT, SizeT rhs 
)
friend

Compare two OpaqueId of the same type


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