NEML2 2.0.0
|
Storage container that stores a vector of unique pointers of T, but represents most of the public facing accessors (iterators, operator[]).
That is, these accessors dereference the underlying storage. More importantly, if data is not properly initialized using set_pointer(), this dereferencing will either lead to an assertion or a nullptr dereference.
#include <Storage.h>
Classes | |
struct | DereferenceIterator |
Public Types | |
using | values_type = typename std::map<I, std::unique_ptr<T>> |
using | iterator = DereferenceIterator<typename values_type::iterator> |
using | const_iterator = DereferenceIterator<typename values_type::const_iterator> |
Public Member Functions | |
Storage ()=default | |
std::size_t | size () const |
bool | empty () const |
bool | has_key (const I &i) const |
T * | set_pointer (const I &i, std::unique_ptr< T > &&ptr) |
Iterators | |
Begin and end iterators to the underlying data. Note that dereferencing these iterators may lead to an assertion or the dereference of a nullptr whether or not the underlying data is initialized. | |
iterator | begin () |
iterator | end () |
const_iterator | begin () const |
const_iterator | end () const |
T & | operator[] (const I &i) const |
T & | operator[] (const I &i) |
const T * | query_value (const I &i) const |
T * | query_value (const I &i) |
using const_iterator = DereferenceIterator<typename values_type::const_iterator> |
using iterator = DereferenceIterator<typename values_type::iterator> |
|
inline |
|
inline |
i
is initialized i
.Note that the underlying data may not necessarily be initialized, in which case this will throw an assertion error.
You can check whether or not the underlying data is intialized with has_key(i).
i
The pointer will be nullptr if !has_key(i), that is, if the unique_ptr at index i
is not initialized
Sets the underlying unique_ptr at index i
to ptr
.
This can be used to construct objects in the storage, i.e., set_pointer(0, std::make_unique<T>(...));
This is the only method that allows for the modification of ownership in the underlying vector. Protect it wisely.