Teuchos - Trilinos Tools Package
Version of the Day
|
Templated class for workspace creation. More...
#include <Teuchos_Workspace.hpp>
Public Member Functions | |
Workspace (WorkspaceStore *workspace_store, size_t num_elements, bool call_constructors=true) | |
Allocates a num_elements array of temporary objects. More... | |
~Workspace () | |
The destructor on the elements will only be called if call_constructors == true was passed to the constructor. More... | |
size_t | size () const |
Return the number of elements in the array. More... | |
T * | getRawPtr () |
Return a raw pointer to the beginning of the array or null if unsized. More... | |
const T * | getRawPtr () const |
Return a raw pointer to the beginning of the array or null if unsized. More... | |
T & | operator[] (size_t i) |
Non-const zero based element access. More... | |
const T & | operator[] (size_t i) const |
Const zero based element access. More... | |
ArrayView< T > | operator() () |
Return a non-const array view. More... | |
ArrayView< const T > | operator() () const |
Return a const array view. More... | |
operator ArrayView< T > () | |
Return a non-const array view. More... | |
operator ArrayView< const T > () const | |
Return a const view object. More... | |
Templated class for workspace creation.
Objects of this type are what should be created by the user instead of RawWorkspace objects since this class will properly initialize memory using placement new and allows typed operator[] access to the array elements. The default constructor, copy constructor and assignment operations are not allowed and objects can not be allocated with new.
It is important to note that the constructors and destructors will only be called if call_constructors=true
(the default) is passed to the consructor Workspace(). For build-in types that do not need constructors and destructors called, the client should pass in call_constructors=false
. Otherwise we would have to call constructors and destructors on all of the memory and that could considerably slow things down.
With simple built-in data types (i.e. call_constructors=false
) the cost of creating and destroying one of these objects should be O(1) independent of how much data is requested. This is true as long as no dynamic memory has to be allocated (this is determined the object workspace_store
passed to the constructor Workspace()).
Definition at line 145 of file Teuchos_Workspace.hpp.
|
inline |
Allocates a num_elements array of temporary objects.
workspace_store | [in] Pointer to the workspace object to get the memory from. This can be NULL in which case new T[] and delete [] will be used instead. |
num_elements | [in] The number of bytes to allocate. |
call_consructors | [in] If true then constructors and destructors will be called on the allocated memory. |
Preconditions:
num_element >= 0
(throw std::invalid_argument)
Postconditons:
this->
size() == num_elements
num_elements > 0
] this->
operator[i], for i = 0,..num_elements-1
points to valid allocated object of type T
. this->
operator[i], for i = 0,..num_elements-1
was allocated as new (&this->operator[i]) T()
. When this object is created the
workspace_store
object will be used to get the raw memory if workspace_store != NULL
. If workspace_store == NULL || workspace_store->num_bytes_remaining()
< sizeof(T)*num_elements
then this memory will have to be dynamically allocated. The memory is default initialized (or uninitialized) using placement new. The constructor will only be called with placement new if call_constructor == ture
. Otherwise, the memory will be left uninitlaized. This is okay for integral types like double
and int
but not okay for class types like std::string
etc.
Definition at line 322 of file Teuchos_Workspace.hpp.
|
inline |
The destructor on the elements will only be called if call_constructors == true
was passed to the constructor.
Definition at line 334 of file Teuchos_Workspace.hpp.
|
inline |
Return the number of elements in the array.
Definition at line 346 of file Teuchos_Workspace.hpp.
|
inline |
Return a raw pointer to the beginning of the array or null if unsized.
Definition at line 353 of file Teuchos_Workspace.hpp.
|
inline |
Return a raw pointer to the beginning of the array or null if unsized.
Definition at line 360 of file Teuchos_Workspace.hpp.
|
inline |
Non-const zero based element access.
Preconditions:
0 <= i && i < size()
(throw std::invalid_argument
) Definition at line 367 of file Teuchos_Workspace.hpp.
|
inline |
Const zero based element access.
Preconditions:
0 <= i && i < size()
(throw std::invalid_argument
) Definition at line 377 of file Teuchos_Workspace.hpp.
|
inline |
Return a non-const array view.
Definition at line 384 of file Teuchos_Workspace.hpp.
|
inline |
Return a const array view.
Definition at line 394 of file Teuchos_Workspace.hpp.
|
inline |
Return a non-const array view.
Definition at line 403 of file Teuchos_Workspace.hpp.
|
inline |
Return a const view object.
Definition at line 410 of file Teuchos_Workspace.hpp.