Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | List of all members
Teuchos::Workspace< T > Class Template Reference

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...
 

Detailed Description

template<class T>
class Teuchos::Workspace< T >

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 177 of file Teuchos_Workspace.hpp.

Constructor & Destructor Documentation

template<class T >
Teuchos::Workspace< T >::Workspace ( WorkspaceStore workspace_store,
size_t  num_elements,
bool  call_constructors = true 
)
inline

Allocates a num_elements array of temporary objects.

Parameters
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.
  • [num_elements > 0 && call_constructors==true] 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 354 of file Teuchos_Workspace.hpp.

template<class T >
Teuchos::Workspace< T >::~Workspace ( )
inline

The destructor on the elements will only be called if call_constructors == true was passed to the constructor.

Definition at line 366 of file Teuchos_Workspace.hpp.

Member Function Documentation

template<class T >
size_t Teuchos::Workspace< T >::size ( ) const
inline

Return the number of elements in the array.

Definition at line 378 of file Teuchos_Workspace.hpp.

template<class T >
T * Teuchos::Workspace< T >::getRawPtr ( )
inline

Return a raw pointer to the beginning of the array or null if unsized.

Definition at line 385 of file Teuchos_Workspace.hpp.

template<class T >
const T * Teuchos::Workspace< T >::getRawPtr ( ) const
inline

Return a raw pointer to the beginning of the array or null if unsized.

Definition at line 392 of file Teuchos_Workspace.hpp.

template<class T >
T & Teuchos::Workspace< T >::operator[] ( size_t  i)
inline

Non-const zero based element access.

Preconditions:

  • 0 <= i && i < size() (throw std::invalid_argument)

Definition at line 399 of file Teuchos_Workspace.hpp.

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

Const zero based element access.

Preconditions:

  • 0 <= i && i < size() (throw std::invalid_argument)

Definition at line 409 of file Teuchos_Workspace.hpp.

template<class T >
ArrayView< T > Teuchos::Workspace< T >::operator() ( )
inline

Return a non-const array view.

Definition at line 416 of file Teuchos_Workspace.hpp.

template<class T >
ArrayView< const T > Teuchos::Workspace< T >::operator() ( ) const
inline

Return a const array view.

Definition at line 426 of file Teuchos_Workspace.hpp.

template<class T >
Teuchos::Workspace< T >::operator ArrayView< T > ( )
inline

Return a non-const array view.

Definition at line 435 of file Teuchos_Workspace.hpp.

template<class T >
Teuchos::Workspace< T >::operator ArrayView< const T > ( ) const
inline

Return a const view object.

Definition at line 442 of file Teuchos_Workspace.hpp.


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