PSCF v1.5.0
Util::DRArray< Data > Class Template Reference

Array that can own or reference a dynamically allocated C array. More...

#include <DRArray.h>

Inheritance diagram for Util::DRArray< Data >:
Util::Array< Data > Util::ReferenceCounter

Public Member Functions

 DRArray ()
 Default constructor.
 DRArray (int capacity)
 Allocating constructor.
 DRArray (DRArray< Data > const &other)
 Copy constructor.
 ~DRArray ()
 Destructor.
DRArray< Data > & operator= (DRArray< Data > const &other)
 Assignment from another DRArray<Data> container.
DRArray< Data > & operator= (Array< Data > const &other)
 Assignment from an Array<Data> container.
void allocate (int capacity)
 Allocate an underlying C array, which this container then owns.
void deallocate ()
 Dellocate an underlying C array that is owned by this container.
void associate (DRArray< Data > &arr, int beginId, int capacity)
 Associate this object with a slice of a different DRArray.
void dissociate ()
 Dissociate this object from an externally owned array slice.
bool isOwner () const
 Does this container own a dynamically allocated C array?
bool isAssociated () const
 Is this container associated with a C array it does not own?
Public Member Functions inherited from Util::Array< Data >
Data & operator[] (int i)
 Get an element by non-const reference.
Data const & operator[] (int i) const
 Get an element by const reference.
void begin (ArrayIterator< Data > &iterator)
 Set an iterator to begin this Array.
void begin (ConstArrayIterator< Data > &iterator) const
 Set a const iterator to begin this array.
Data * cArray ()
 Return a pointer to the underlying C array (non-const data).
Data const * cArray () const
 Return pointer to the underlying C array (const data).
bool isAllocated () const
 Does this array have associated data?
int size () const
 Return logical size of array.
int capacity () const
 Return allocated size.
Public Member Functions inherited from Util::ReferenceCounter
 ReferenceCounter ()
 Constructor.
int nRef () const
 Get the number of references to a shareable resource.
bool hasRefs () const
 Does this counter have references (is nRef > 0)?

Additional Inherited Members

Protected Member Functions inherited from Util::Array< Data >
 Array ()
 Constructor (protected to prevent direct instantiation).
 ~Array ()
 Destructor (protected to prevent direct destruction).
Protected Attributes inherited from Util::Array< Data >
Data * data_
 Pointer to an array of Data elements.
int capacity_
 Allocated size of the data_ array.

Detailed Description

template<typename Data>
class Util::DRArray< Data >

Array that can own or reference a dynamically allocated C array.

A DRArray<Data> object wraps a dynamically allocated C array with elements of type Data, and stores the array capacity. A DRArray may either own a C array that it is responsible for de-allocating, or it may reference a slice of an array that is owned by another DRArray. In either case, individual elements may be accessed via a subscript operator (an overloaded operator []) that is inherited from the Array<Data> base class. Member functions for memory management allow a DRArray to allocate or deallocate a C array that it owns, or to become associated with or dissociated from an array slice that it does not own.

A DRArray may be in any of three states:

(1) Unallocated: In this state, there is no associated memory, so capacity() returns 0, while isAllocated(), isOwner() and isAssociated() all return false.

(2) An array owner: In this case, this object owns a C array that it is responsible for de-allocating. In this state, capacity() returns a positive integer, isAllocated() and isOwner() return true, and isAssociated() returns false.

(3) An array view: In this case, this object has a pointer to a C array that is owned by a different DRArray object. We describe this by saying that this DRArray (the array view) is "associated" with a C array that is owned by another object (the array owner), or that the array view "references" that array. In this state, capacity() returns a positive value, isAllocated() and isAssociated() return true, and isOwner() returns false.

A DRArray that owns a C array that is referenced by one or more other associated DRArray objects maintains a count of how many other such objects reference its data. This counter is automatically incremented when a reference is created and decremented when an existing reference is destroyed.

It is a logical error to invoke the deallocate() member function of a DRArray that is unallocated or that references data that it does not own. In either case, an Exception is thrown. It is also an error to attempt to deallocate a C array that is owned by one DRArray but also referenced by one or more other associated DRArray objects. Attempts to deallocate such a shared array will either cause an Exception to be thrown, if deallocation is attempted by invoking the deallocate() member function, or cause an error message to be written to std::cout, if deallocation is attempted during destruction of the array owner object. Such references must instead be released by invoking the dissociate() member function of every associated data user object before the array owner is destroyed.

Definition at line 73 of file DRArray.h.

Constructor & Destructor Documentation

◆ DRArray() [1/3]

template<typename Data>
Util::DRArray< Data >::DRArray ( )

Default constructor.

Definition at line 257 of file DRArray.h.

Referenced by associate(), DRArray(), operator=(), and operator=().

◆ DRArray() [2/3]

template<typename Data>
Util::DRArray< Data >::DRArray ( int capacity)

Allocating constructor.

This function calls allocate(capacity) internally.

Parameters
capacitynumber of elements to allocate

Definition at line 264 of file DRArray.h.

References allocate(), and Util::Array< Data >::capacity().

◆ DRArray() [3/3]

template<typename Data>
Util::DRArray< Data >::DRArray ( DRArray< Data > const & other)

Copy constructor.

Parameters
otherthe DRArray to be copied

Definition at line 271 of file DRArray.h.

References allocate(), Util::Array< Data >::capacity_, Util::Array< Data >::data_, DRArray(), Util::Array< Data >::isAllocated(), and UTIL_THROW.

◆ ~DRArray()

template<typename Data>
Util::DRArray< Data >::~DRArray ( )

Destructor.

Deletes any C array that is owned by this object, and releases any association with a C array that is referred to but not owned by this object. If this object owns an array that is referred to by one or more other DRArray objects, an error message is written to std::cout, but no exception is thrown.

Definition at line 286 of file DRArray.h.

References Util::Array< Data >::capacity_, Util::Array< Data >::data_, Util::Memory::deallocate(), Util::ReferenceCounter::hasRefs(), and Util::ReferenceCounter::nRef().

Member Function Documentation

◆ operator=() [1/2]

template<typename Data>
DRArray< Data > & Util::DRArray< Data >::operator= ( DRArray< Data > const & other)

Assignment from another DRArray<Data> container.

Performs a deep copy, by copying values of all elements of another DRArray<Data> container. If this LHS array is already allocated on entry, it must have the same capacity as the other RHS array. If this LHS array is not allocated on entry, required memory is allocated before copying values. After exit, isAllocated() and isOwner() return true, while isAssociated() returns false.

Exceptions
Exceptionif other array is not allocated
Exceptionif arrays are allocated with unequal capacities
Parameters
otherarray container on RHS of assigment (input)

Definition at line 322 of file DRArray.h.

References allocate(), Util::Array< Data >::capacity(), Util::Array< Data >::capacity_, Util::Array< Data >::data_, DRArray(), Util::Array< Data >::isAllocated(), and UTIL_CHECK.

◆ operator=() [2/2]

template<typename Data>
DRArray< Data > & Util::DRArray< Data >::operator= ( Array< Data > const & other)

Assignment from an Array<Data> container.

Performs a deep copy, by copying values of all elements of an Array<Data> container. If this (LHS) array is already allocated on entry, it must have the same capacity as the other (RHS) array. If this LHS array is not allocated on entry, required memory is allocated before copying values. After exit, isAllocated() and isOwner() return true, while isAssociated() returns false.

Exceptions
Exceptionif other array is not allocated
Exceptionif arrays are allocated with unequal capacities
Parameters
otherarray container on RHS of assigment (input)

Definition at line 348 of file DRArray.h.

References allocate(), Util::Array< Data >::capacity(), Util::Array< Data >::capacity_, Util::Array< Data >::data_, DRArray(), Util::Array< Data >::isAllocated(), and UTIL_CHECK.

◆ allocate()

template<typename Data>
void Util::DRArray< Data >::allocate ( int capacity)

Allocate an underlying C array, which this container then owns.

On entry, this object must be unallocated, i.e., it must not have data that it either owns or references. After exit, isAllocated() and isOwner() will return true, while isAssociated() will return false.

Exceptions
Exceptionif this array is allocated on entry.
Parameters
capacitynumber of elements to allocate

Definition at line 374 of file DRArray.h.

References Util::Memory::allocate(), Util::Array< Data >::capacity(), Util::Array< Data >::capacity_, Util::Array< Data >::data_, Util::Array< Data >::isAllocated(), and UTIL_THROW.

Referenced by DRArray(), DRArray(), operator=(), and operator=().

◆ deallocate()

template<typename Data>
void Util::DRArray< Data >::deallocate ( )

Dellocate an underlying C array that is owned by this container.

After exit, isAllocated(), isOwner(), and isAssociated() will all return false.

Exceptions
Exceptionif this object does not own data.

Definition at line 390 of file DRArray.h.

References Util::Array< Data >::capacity_, Util::Array< Data >::data_, Util::Memory::deallocate(), Util::ReferenceCounter::hasRefs(), UTIL_CHECK, and UTIL_THROW.

◆ associate()

template<typename Data>
void Util::DRArray< Data >::associate ( DRArray< Data > & arr,
int beginId,
int capacity )

Associate this object with a slice of a different DRArray.

On entry, this object must be not be allocated, i.e., it must not have data that it either owns or references. After exit, isAllocated() and isAssociated() will return true, while isOwner() will return false.

Exceptions
Exceptionif this array is allocated on entry.
Parameters
arrparent array that owns the data
beginIdindex in the parent array at which this array starts
capacitynumber of elements associated with this container

Definition at line 412 of file DRArray.h.

References Util::Array< Data >::capacity(), Util::Array< Data >::capacity_, Util::Array< Data >::cArray(), Util::Array< Data >::data_, DRArray(), Util::Array< Data >::isAllocated(), isOwner(), and UTIL_CHECK.

◆ dissociate()

template<typename Data>
void Util::DRArray< Data >::dissociate ( )

Dissociate this object from an externally owned array slice.

After exit, isAllocated(), isOwner(), and isAssociated() will all return false.

Exceptions
Exceptionif this is not associated with external data

Definition at line 440 of file DRArray.h.

References Util::Array< Data >::capacity_, Util::Array< Data >::data_, and UTIL_CHECK.

◆ isOwner()

template<typename Data>
bool Util::DRArray< Data >::isOwner ( ) const
inline

Does this container own a dynamically allocated C array?

If isAllocated() is false, isOwner() is also false. If isAllocated() is true, either isOwner() or isAsssociated() must be true, but not both.

Definition at line 241 of file DRArray.h.

References Util::Array< Data >::data_.

Referenced by associate().

◆ isAssociated()

template<typename Data>
bool Util::DRArray< Data >::isAssociated ( ) const
inline

Is this container associated with a C array it does not own?

If isAllocated() is false, isAssociated() is also false.

Definition at line 248 of file DRArray.h.

References Util::Array< Data >::data_.


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