https://mooseframework.inl.gov
Classes | Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
UniqueStorage< T > Class Template Reference

Storage container that stores a vector of unique pointers of T, but represents most of the public facing accessors (iterators, operator[]) as a vector of T. More...

#include <UniqueStorage.h>

Classes

struct  DereferenceIterator
 Iterator that adds an additional dereference to BaseIterator. More...
 

Public Types

using values_type = typename std::vector< std::unique_ptr< T > >
 
using iterator = DereferenceIterator< typename values_type::iterator >
 
using const_iterator = DereferenceIterator< typename values_type::const_iterator >
 

Public Member Functions

 UniqueStorage ()=default
 
 UniqueStorage (UniqueStorage &&)=default
 
 UniqueStorage (const UniqueStorage &mp)=delete
 
UniqueStorageoperator= (const UniqueStorage &)=delete
 
std::size_t size () const
 
bool empty () const
 
bool hasValue (const std::size_t i) const
 
iterator begin ()
 Begin and end iterators to the underlying data. More...
 
iterator end ()
 
const_iterator begin () const
 
const_iterator end () const
 
const T & operator[] (const std::size_t i) const
 
T & operator[] (const std::size_t i)
 
const T * queryValue (const std::size_t i) const
 
T * queryValue (const std::size_t i)
 

Protected Member Functions

void setPointer (const std::size_t i, std::unique_ptr< T > &&ptr)
 Sets the underlying unique_ptr at index i to ptr. More...
 
T & addPointer (std::unique_ptr< T > &&ptr)
 Adds the given object in ptr to the storage. More...
 
void resize (const std::size_t size)
 Resizes the underlying vector. More...
 
void clear ()
 Clears the underlying vector. More...
 

Private Member Functions

const std::unique_ptr< T > & pointerValue (const std::size_t i) const
 Returns a read-only reference to the underlying unique pointer at index i. More...
 
std::unique_ptr< T > & pointerValue (const std::size_t i)
 Returns a reference to the underlying unique pointer at index i. More...
 

Private Attributes

values_type _values
 The underlying data. More...
 

Friends

void storeHelper (std::ostream &stream, UniqueStorage< T > &, void *)
 UniqueStorage helper routine. More...
 
void loadHelper (std::istream &stream, UniqueStorage< T > &, void *)
 UniqueStorage helper routine. More...
 

Detailed Description

template<class T>
class UniqueStorage< T >

Storage container that stores a vector of unique pointers of T, but represents most of the public facing accessors (iterators, operator[]) as a vector of T.

That is, these accessors dereference the underlying storage. More importantly, if data is not properly initialized using setValue(), this dereferencing will either lead to an assertion or a nullptr dereference.

Definition at line 18 of file UniqueStorage.h.

Member Typedef Documentation

◆ const_iterator

template<class T>
using UniqueStorage< T >::const_iterator = DereferenceIterator<typename values_type::const_iterator>

Definition at line 72 of file UniqueStorage.h.

◆ iterator

template<class T>
using UniqueStorage< T >::iterator = DereferenceIterator<typename values_type::iterator>

Definition at line 71 of file UniqueStorage.h.

◆ values_type

template<class T>
using UniqueStorage< T >::values_type = typename std::vector<std::unique_ptr<T> >

Definition at line 70 of file UniqueStorage.h.

Constructor & Destructor Documentation

◆ UniqueStorage() [1/3]

template<class T>
UniqueStorage< T >::UniqueStorage ( )
default

◆ UniqueStorage() [2/3]

template<class T>
UniqueStorage< T >::UniqueStorage ( UniqueStorage< T > &&  )
default

◆ UniqueStorage() [3/3]

template<class T>
UniqueStorage< T >::UniqueStorage ( const UniqueStorage< T > &  mp)
delete

Member Function Documentation

◆ addPointer()

template<class T>
T& UniqueStorage< T >::addPointer ( std::unique_ptr< T > &&  ptr)
inlineprotected

Adds the given object in ptr to the storage.

Definition at line 153 of file UniqueStorage.h.

Referenced by RestartableDataMap::Data::addPointer().

154  {
155  mooseAssert(ptr, "Null object");
156  return *_values.emplace_back(std::move(ptr));
157  }
values_type _values
The underlying data.

◆ begin() [1/2]

template<class T>
iterator UniqueStorage< T >::begin ( )
inline

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.

Definition at line 82 of file UniqueStorage.h.

Referenced by RestartableDataMap::begin(), and RestartableDataMap::findData().

82 { return iterator(_values.begin()); }
DereferenceIterator< typename values_type::iterator > iterator
Definition: UniqueStorage.h:71
values_type _values
The underlying data.

◆ begin() [2/2]

template<class T>
const_iterator UniqueStorage< T >::begin ( ) const
inline

Definition at line 84 of file UniqueStorage.h.

84 { return const_iterator(_values.begin()); }
DereferenceIterator< typename values_type::const_iterator > const_iterator
Definition: UniqueStorage.h:72
values_type _values
The underlying data.

◆ clear()

template<class T>
void UniqueStorage< T >::clear ( )
inlineprotected

Clears the underlying vector.

Definition at line 167 of file UniqueStorage.h.

167 { _values.clear(); }
values_type _values
The underlying data.

◆ empty()

template<class T>
bool UniqueStorage< T >::empty ( ) const
inline
Returns
Whether or not the underlying storage is empty.

Definition at line 116 of file UniqueStorage.h.

Referenced by RestartableDataMap::empty().

116 { return _values.empty(); }
values_type _values
The underlying data.

◆ end() [1/2]

template<class T>
iterator UniqueStorage< T >::end ( )
inline

Definition at line 83 of file UniqueStorage.h.

Referenced by RestartableDataMap::end(), and RestartableDataMap::findData().

83 { return iterator(_values.end()); }
DereferenceIterator< typename values_type::iterator > iterator
Definition: UniqueStorage.h:71
values_type _values
The underlying data.

◆ end() [2/2]

template<class T>
const_iterator UniqueStorage< T >::end ( ) const
inline

Definition at line 85 of file UniqueStorage.h.

85 { return const_iterator(_values.end()); }
DereferenceIterator< typename values_type::const_iterator > const_iterator
Definition: UniqueStorage.h:72
values_type _values
The underlying data.

◆ hasValue()

template<class T>
bool UniqueStorage< T >::hasValue ( const std::size_t  i) const
inline
Returns
whether or not the underlying object at index is initialized

Definition at line 121 of file UniqueStorage.h.

Referenced by MaterialPropertyStorage::shallowSwapData(), MaterialPropertyStorage::shallowSwapDataBack(), and storeHelper().

121 { return pointerValue(i) != nullptr; }
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.

◆ operator=()

template<class T>
UniqueStorage& UniqueStorage< T >::operator= ( const UniqueStorage< T > &  )
delete

◆ operator[]() [1/2]

template<class T>
const T& UniqueStorage< T >::operator[] ( const std::size_t  i) const
inline
Returns
A reference to the underlying data at index i.

Note that the underlying data may not necessarily be initialized, in which case this will throw an assertion or dereference a nullptr.

You can check whether or not the underlying data is initialized with hasValue(i).

Definition at line 98 of file UniqueStorage.h.

99  {
100  mooseAssert(hasValue(i), "Null object");
101  return *pointerValue(i);
102  }
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.
bool hasValue(const std::size_t i) const

◆ operator[]() [2/2]

template<class T>
T& UniqueStorage< T >::operator[] ( const std::size_t  i)
inline

Definition at line 103 of file UniqueStorage.h.

103 { return const_cast<T &>(std::as_const(*this)[i]); }

◆ pointerValue() [1/2]

template<class T>
const std::unique_ptr<T>& UniqueStorage< T >::pointerValue ( const std::size_t  i) const
inlineprivate

Returns a read-only reference to the underlying unique pointer at index i.

Definition at line 174 of file UniqueStorage.h.

Referenced by UniqueStorage< RestartableDataValue >::hasValue(), loadHelper(), and storeHelper().

175  {
176  mooseAssert(size() > i, "Invalid size");
177  return _values[i];
178  }
std::size_t size() const
values_type _values
The underlying data.

◆ pointerValue() [2/2]

template<class T>
std::unique_ptr<T>& UniqueStorage< T >::pointerValue ( const std::size_t  i)
inlineprivate

Returns a reference to the underlying unique pointer at index i.

Definition at line 183 of file UniqueStorage.h.

184  {
185  return const_cast<std::unique_ptr<T> &>(std::as_const(*this).pointerValue(i));
186  }

◆ queryValue() [1/2]

template<class T>
const T* UniqueStorage< T >::queryValue ( const std::size_t  i) const
inline
Returns
A pointer to the underlying data at index i

The pointer will be nullptr if !hasValue(i), that is, if the unique_ptr at index i is not initialized

Definition at line 130 of file UniqueStorage.h.

Referenced by MaterialData::haveGenericProperty().

130 { return pointerValue(i).get(); }
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.

◆ queryValue() [2/2]

template<class T>
T* UniqueStorage< T >::queryValue ( const std::size_t  i)
inline

Definition at line 131 of file UniqueStorage.h.

132  {
133  return const_cast<T *>(std::as_const(*this).queryValue(i));
134  }

◆ resize()

template<class T>
void UniqueStorage< T >::resize ( const std::size_t  size)
inlineprotected

Resizes the underlying vector.

Definition at line 162 of file UniqueStorage.h.

Referenced by loadHelper(), and MaterialProperties::resize().

162 { _values.resize(size); }
std::size_t size() const
values_type _values
The underlying data.

◆ setPointer()

template<class T>
void UniqueStorage< T >::setPointer ( const std::size_t  i,
std::unique_ptr< T > &&  ptr 
)
inlineprotected

Sets the underlying unique_ptr at index i to ptr.

This can be used to construct objects in the storage, i.e., setPointer(0, std::make_unique<T>(...));

Definition at line 144 of file UniqueStorage.h.

Referenced by MaterialProperties::setPointer().

145  {
146  mooseAssert(size() > i, "Invalid size");
147  _values[i] = std::move(ptr);
148  }
std::size_t size() const
values_type _values
The underlying data.

◆ size()

template<class T>
std::size_t UniqueStorage< T >::size ( ) const
inline
Returns
The size of the underlying storage.

Note that this is not necessarily the size of constructed objects, as underlying objects could be uninitialized

Definition at line 112 of file UniqueStorage.h.

Referenced by RestartableDataMap::addData(), dataLoad(), dataStore(), RestartableDataMap::findData(), UniqueStorage< RestartableDataValue >::pointerValue(), UniqueStorage< RestartableDataValue >::resize(), UniqueStorage< RestartableDataValue >::setPointer(), MaterialPropertyStorage::shallowSwapData(), MaterialPropertyStorage::shallowSwapDataBack(), RestartableDataMap::size(), and storeHelper().

112 { return _values.size(); }
values_type _values
The underlying data.

Friends And Related Function Documentation

◆ loadHelper

template<class T>
void loadHelper ( std::istream &  stream,
UniqueStorage< T > &  ,
void  
)
friend

UniqueStorage helper routine.

UniqueStorage Helper Function.

The unique_ptr<T> loader is called to load the data. That is, you will likely need a specialization of unique_ptr<T> that will appropriately construct and then fill the piece of data.

Definition at line 1063 of file DataIO.h.

1064 {
1065  std::size_t size;
1066  dataLoad(stream, size, nullptr);
1067  data.resize(size);
1068 
1069  for (const auto i : index_range(data))
1070  loadHelper(stream, data.pointerValue(i), context);
1071 }
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.
friend void loadHelper(std::istream &stream, UniqueStorage< T > &, void *)
UniqueStorage helper routine.
Definition: DataIO.h:1063
std::size_t size() const
void resize(const std::size_t size)
Resizes the underlying vector.
void dataLoad(std::istream &stream, T &v, void *)
Definition: DataIO.h:540
auto index_range(const T &sizable)

◆ storeHelper

template<class T>
void storeHelper ( std::ostream &  stream,
UniqueStorage< T > &  ,
void  
)
friend

UniqueStorage helper routine.

The data within the UniqueStorage object cannot be null. The helper for unique_ptr<T> is called to store the data.

Definition at line 970 of file DataIO.h.

971 {
972  std::size_t size = data.size();
973  dataStore(stream, size, nullptr);
974 
975  for (const auto i : index_range(data))
976  {
977  mooseAssert(data.hasValue(i), "Data doesn't have a value");
978  storeHelper(stream, data.pointerValue(i), context);
979  }
980 }
const std::unique_ptr< T > & pointerValue(const std::size_t i) const
Returns a read-only reference to the underlying unique pointer at index i.
friend void storeHelper(std::ostream &stream, UniqueStorage< T > &, void *)
UniqueStorage helper routine.
Definition: DataIO.h:970
std::size_t size() const
void dataStore(std::ostream &stream, T &v, void *)
Definition: DataIO.h:183
bool hasValue(const std::size_t i) const
auto index_range(const T &sizable)

Member Data Documentation

◆ _values

template<class T>
values_type UniqueStorage< T >::_values
private

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