Skip to content

Commit

Permalink
BUG: DataSet assignment operator is missing
Browse files Browse the repository at this point in the history
* Some compilers complain if the copy constructor is given explicitly
  but the assignment operator is implicitly set to default.

* Explicitly defining the assignment operator allows us to properly
  handle reference counters for shared resources.
  • Loading branch information
Leengit committed Mar 23, 2021
1 parent 69f3cde commit 1e1e267
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
21 changes: 21 additions & 0 deletions c++/src/H5DataSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,27 @@ DataSet::DataSet(const DataSet &original) : H5Object(), AbstractDs(), id(origina
incRefCount(); // increment number of references to this id
}

//--------------------------------------------------------------------------
// Function: DataSet assignment operator
///\brief Assignment operator: same HDF5 object as \a original
///\param original - IN: DataSet instance to copy
// Programmer Lee Newberg - 2021
//--------------------------------------------------------------------------
DataSet &
DataSet::operator=(const DataSet &original)
{
// Wrap the changes to `*this` by an incremented reference count for
// `original` to prevent trouble in the case that `*this` and
// `original` share reference counts or memory in any relevant
// sense.
original.incRefCount();
decRefCount(); // for old value of id
id = original.id;
incRefCount(); // for new value of id
original.decRefCount();
return *this;
}

//--------------------------------------------------------------------------
// Function: DataSet overload constructor - dereference
///\brief Given a reference, ref, to an hdf5 location, creates a
Expand Down
3 changes: 3 additions & 0 deletions c++/src/H5DataSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ class H5_DLLCPP DataSet : public H5Object, public AbstractDs {
// Copy constructor - same as the original DataSet.
DataSet(const DataSet &original);

// Assignment operator
DataSet &operator=(const DataSet &original);

// Creates a copy of an existing DataSet using its id.
DataSet(const hid_t existing_id);

Expand Down

0 comments on commit 1e1e267

Please sign in to comment.