11#include <util/containers/Array.h>
12#include <util/misc/ReferenceCounter.h>
13#include <util/misc/CountedReference.h>
72 template <
typename Data>
221 CountedReference ref_;
240 template <
typename Data>
inline
242 {
return ((
bool)
data_ && !ref_.isAssociated()); }
247 template <
typename Data>
inline
249 {
return ((
bool)
data_ && ref_.isAssociated()); }
256 template <
typename Data>
263 template <
typename Data>
270 template <
typename Data>
274 UTIL_THROW(
"Other DRArray must be allocated.");
285 template <
typename Data>
289 if (ref_.isAssociated()) {
296 <<
"Error: Destruction of a DRArray that is referenced"
297 <<
"by " << nr <<
" other(s), thereby creating one or"
298 <<
"more dangling pointers." << std::endl
299 <<
"To fix this, DRArray::dissociate must be called"
300 <<
"on all referencing (user) DRArray objects before"
301 <<
"the source DRArray (owner) object is destroyed."
309 <<
"Error during array deletion in DRArray destructor"
321 template <
typename Data>
325 if (
this == &other)
return *
this;
347 template <
typename Data>
351 if (
dynamic_cast< Array<Data>*
>(
this) == &other)
return *
this;
373 template <
typename Data>
377 UTIL_THROW(
"Attempt to allocate with capacity <= 0");
380 UTIL_THROW(
"Attempt to allocate a DRArray that already has data.");
389 template <
typename Data>
396 <<
"Error: DRArray::deallocate called on an array that is "
397 <<
"referenced by one or more counted references, thereby "
398 <<
"creating one or more dangling pointers." << std::endl
399 <<
"To fix this, DRArray::dissociate must be called on all "
400 <<
"referencing DRArray objects before the source array is "
401 <<
"de-allocated or destroyed." << std::endl;
402 UTIL_THROW(
"Error: Creating dangling reference(s) by deallocation");
411 template <
typename Data>
429 ref_.associate(source);
439 template <
typename Data>
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Data * data_
Pointer to an array of Data elements.
int capacity() const
Return allocated size.
bool isAllocated() const
Does this array have associated data?
int capacity_
Allocated size of the data_ array.
void allocate(int capacity)
Allocate an underlying C array, which this container then owns.
void associate(DRArray< Data > &arr, int beginId, int capacity)
Associate this object with a slice of a different DRArray.
DRArray()
Default constructor.
bool isOwner() const
Does this container own a dynamically allocated C array?
DRArray< Data > & operator=(DRArray< Data > const &other)
Assignment from another DRArray<Data> container.
bool isAssociated() const
Is this container associated with a C array it does not own?
void deallocate()
Dellocate an underlying C array that is owned by this container.
void dissociate()
Dissociate this object from an externally owned array slice.
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
bool hasRefs() const
Does this counter have references (is nRef > 0)?
ReferenceCounter()
Constructor.
int nRef() const
Get the number of references to a shareable resource.
File containing preprocessor macros for error handling.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Utility classes for scientific computation.