PSCF v1.5.0
DRArray.h
1#ifndef UTIL_DR_ARRAY_H
2#define UTIL_DR_ARRAY_H
3
4/*
5* Util Package - C++ Utilities for Scientific Computation
6*
7* Copyright 2010 - 2017, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/containers/Array.h> // base class
12#include <util/misc/ReferenceCounter.h> // base class
13#include <util/misc/CountedReference.h> // member
14#include <util/global.h>
15
16namespace Util {
17
72 template <typename Data>
73 class DRArray : public Array<Data>, public ReferenceCounter
74 {
75
76 public:
77
81 DRArray();
82
90 DRArray(int capacity);
91
97 DRArray(DRArray<Data> const & other);
98
108 ~DRArray();
109
126
142 DRArray<Data>& operator = (Array<Data> const & other);
143
156 void allocate(int capacity);
157
166 void deallocate();
167
182 void associate(DRArray<Data>& arr, int beginId, int capacity);
183
192 void dissociate();
193
201 bool isOwner() const;
202
208 bool isAssociated() const;
209
210 /*
211 * A DRArray is considered allocated if it has non-null pointer
212 * to a C array, which may either be an array that it owns or a
213 * slice of an array that is owned by another DRArray object.
214 */
215
216 using Array<Data>::isAllocated;
217
218 private:
219
221 CountedReference ref_;
222
223 // Prohibit public access to the reference counter.
226
227 // Note: ReferenceCounter::nRef_ is declared mutable, so changes
228 // to this variable should not be visible in the public interface.
229
230 using Array<Data>::data_;
231 using Array<Data>::capacity_;
232
233 };
234
235 // Inline member function definitions
236
237 /*
238 * Does this object own data?
239 */
240 template <typename Data> inline
242 { return ((bool) data_ && !ref_.isAssociated()); }
243
244 /*
245 * Does this object reference data that it does not own?
246 */
247 template <typename Data> inline
249 { return ((bool) data_ && ref_.isAssociated()); }
250
251 // Non-inline member function definitions
252
253 /*
254 * Default constructor.
255 */
256 template <typename Data>
259
260 /*
261 * Allocating constructor.
262 */
263 template <typename Data>
266
267 /*
268 * Copy constructor.
269 */
270 template <typename Data>
272 {
273 if (!other.isAllocated()) {
274 UTIL_THROW("Other DRArray must be allocated.");
275 }
276 allocate(other.capacity_);
277 for (int i = 0; i < capacity_; ++i) {
278 data_[i] = other.data_[i];
279 }
280 }
281
282 /*
283 * Destructor.
284 */
285 template <typename Data>
287 {
288 if (data_) {
289 if (ref_.isAssociated()) {
290 ref_.dissociate();
291 } else {
293 int nr = ReferenceCounter::nRef();
294 std::cout
295 << std::endl
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."
302 << std::endl;
303 }
304 try {
306 } catch (...) {
307 std::cout
308 << std::endl
309 << "Error during array deletion in DRArray destructor"
310 << std::endl;
311 }
312 }
313 }
314 data_ = nullptr;
315 capacity_ = 0;
316 }
317
318 /*
319 * Assignment from another DRArray<Data> (deep copy).
320 */
321 template <typename Data>
323 {
324 // Check for self assignment
325 if (this == &other) return *this;
326
327 // Precondition - other array must be allocated
328 UTIL_CHECK (other.isAllocated());
329
330 // If this is not allocated, then allocate
331 if (!isAllocated()) {
332 allocate(other.capacity());
333 }
334
335 // Copy elements
337 for (int i = 0; i < capacity_; ++i) {
338 data_[i] = other[i];
339 }
340
341 return *this;
342 }
343
344 /*
345 * Assignment from an Array<Data> (deep copy).
346 */
347 template <typename Data>
348 DRArray<Data>& DRArray<Data>::operator = (Array<Data> const & other)
349 {
350 // Check for self assignment
351 if (dynamic_cast< Array<Data>* >(this) == &other) return *this;
352
353 // Precondition - other array must be allocated
354 UTIL_CHECK(other.capacity() > 0);
355
356 // If this is not allocated, then allocate
357 if (!isAllocated()) {
358 allocate(other.capacity());
359 }
360
361 // Copy elements
362 UTIL_CHECK(capacity_ == other.capacity());
363 for (int i = 0; i < capacity_; ++i) {
364 data_[i] = other[i];
365 }
366
367 return *this;
368 }
369
370 /*
371 * Allocate an underlying C array, which this container then owns.
372 */
373 template <typename Data>
375 {
376 if (capacity <= 0) {
377 UTIL_THROW("Attempt to allocate with capacity <= 0");
378 }
379 if (isAllocated()) {
380 UTIL_THROW("Attempt to allocate a DRArray that already has data.");
381 }
384 }
385
386 /*
387 * Deallocate a C array that is owned by this container.
388 */
389 template <typename Data>
391 {
393 UTIL_CHECK(!ref_.isAssociated());
395 std::cout
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");
403 }
405 capacity_ = 0;
406 }
407
408 /*
409 * Associate this object with a slice of a different DRArray.
410 */
411 template <typename Data>
413 int beginId, int capacity)
414 {
415 UTIL_CHECK(source.isAllocated());
416 UTIL_CHECK(source.isOwner());
417 UTIL_CHECK(beginId >= 0);
418 UTIL_CHECK(capacity > 0);
419 UTIL_CHECK(beginId + capacity <= source.capacity());
421 UTIL_CHECK(!ref_.isAssociated());
422
423 // Copy data pointer and capacity
424 data_ = source.cArray() + beginId;
426
427 // Associate ReferencecCounter base class subobject of source
428 // with the CountedReference ref_ member variable
429 ref_.associate(source);
430
431 // On exit from CountedReference::associate, the ReferenceCounter
432 // base sub-object of source is incremented and CountedReference
433 // ref_ holds a pointer to that ReferenceCounter.
434 }
435
436 /*
437 * Dissociate this object from array sliced owned by another object.
438 */
439 template <typename Data>
441 {
443 UTIL_CHECK(ref_.isAssociated());
444
445 data_ = nullptr;
446 capacity_ = 0;
447 ref_.dissociate();
448
449 // The CountedReference::dissociate() function decrements the
450 // the reference counter of the associated array owner.
451 }
452
453} // namespace Util
454#endif
Data * cArray()
Return a pointer to the underlying C array (non-const data).
Definition Array.h:222
Data * data_
Pointer to an array of Data elements.
Definition Array.h:141
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
int capacity_
Allocated size of the data_ array.
Definition Array.h:144
void allocate(int capacity)
Allocate an underlying C array, which this container then owns.
Definition DRArray.h:374
void associate(DRArray< Data > &arr, int beginId, int capacity)
Associate this object with a slice of a different DRArray.
Definition DRArray.h:412
DRArray()
Default constructor.
Definition DRArray.h:257
bool isOwner() const
Does this container own a dynamically allocated C array?
Definition DRArray.h:241
~DRArray()
Destructor.
Definition DRArray.h:286
DRArray< Data > & operator=(DRArray< Data > const &other)
Assignment from another DRArray<Data> container.
Definition DRArray.h:322
bool isAssociated() const
Is this container associated with a C array it does not own?
Definition DRArray.h:248
void deallocate()
Dellocate an underlying C array that is owned by this container.
Definition DRArray.h:390
void dissociate()
Dissociate this object from an externally owned array slice.
Definition DRArray.h:440
static void deallocate(Data *&ptr, size_t size)
Deallocate a C++ array.
Definition Memory.h:202
static void allocate(Data *&ptr, size_t size)
Allocate a C++ array.
Definition Memory.h:172
bool hasRefs() const
Does this counter have references (is nRef > 0)?
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.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
Utility classes for scientific computation.