Celeritas
0.5.0-86+4a8eea4
|
Type-safe index for accessing an array or collection of data. More...
#include <OpaqueId.hh>
Public Types | |
Type aliases | |
using | value_type = ValueT |
using | size_type = SizeT |
Public Member Functions | |
constexpr | OpaqueId () |
Default to invalid state. | |
constexpr | OpaqueId (size_type index) |
Construct explicitly with stored value. | |
constexpr | operator bool () const |
Whether this ID is in a valid (assigned) state. | |
OpaqueId & | operator++ () |
Pre-increment of the ID. | |
OpaqueId | operator++ (int) |
Post-increment of the ID. | |
OpaqueId & | operator-- () |
Pre-decrement of the ID. | |
OpaqueId | operator-- (int) |
Post-decrement of the ID. | |
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 const * | data () const |
Access the underlying data for more efficient loading from memory. | |
Type-safe index for accessing an array or collection of data.
ValueT | Type of each item in an array |
SizeT | Unsigned 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 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 (i.e. it has an "invalid" value).
See also id_cast
below for checked construction of OpaqueIds from generic integer values (avoid compile-time warnings or errors from signed/truncated integers).