|
Celeritas 0.7.0-dev.162+develop.12e60fa0f
|
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. | |
| 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.
| ItemT | Type of an item at the index corresponding to this ID |
| 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 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).
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.