Commit 364fcbb3 authored by Kenneth Leiter's avatar Kenneth Leiter
Browse files

ENH: Add transferOwnership parameter to setValues(std::vector<T> & array) to...

ENH: Add transferOwnership parameter to setValues(std::vector<T> & array) to match other setValues(const T * const array) --- clarification of ownership for non-shared_ptr arrays.
parent 92702e6c
...@@ -177,9 +177,10 @@ public: ...@@ -177,9 +177,10 @@ public:
* ownership of the data and must ensure that the array is still valid for the entire time Xdmf needs it. * ownership of the data and must ensure that the array is still valid for the entire time Xdmf needs it.
* *
* @param array a vector to store in this array. * @param array a vector to store in this array.
* @param transferOwnership whether to transfer responsibility for deletion of the array to XdmfArray.
*/ */
template<typename T> template<typename T>
void setValues(std::vector<T> & array); void setValues(std::vector<T> & array, const bool transferOwnership = 0);
/** /**
* Sets the values of this array to the values stored in the vector. No copy is made. This array shares ownership with * Sets the values of this array to the values stored in the vector. No copy is made. This array shares ownership with
......
...@@ -131,14 +131,22 @@ void XdmfArray::setValues(const T * const arrayPointer, const int numValues, con ...@@ -131,14 +131,22 @@ void XdmfArray::setValues(const T * const arrayPointer, const int numValues, con
} }
template<typename T> template<typename T>
void XdmfArray::setValues(std::vector<T> & array) void XdmfArray::setValues(std::vector<T> & array, const bool transferOwnership)
{ {
if(mHaveArrayPointer) if(mHaveArrayPointer)
{ {
releaseArrayPointer(); releaseArrayPointer();
} }
if(transferOwnership)
{
boost::shared_ptr<std::vector<T> > newArray(&array);
mArray = newArray;
}
else
{
boost::shared_ptr<std::vector<T> > newArray(&array, NullDeleter()); boost::shared_ptr<std::vector<T> > newArray(&array, NullDeleter());
mArray = newArray; mArray = newArray;
}
mHaveArray = true; mHaveArray = true;
} }
......
...@@ -119,6 +119,19 @@ int main(int argc, char* argv[]) ...@@ -119,6 +119,19 @@ int main(int argc, char* argv[])
assert(array5->getSize() == 4); assert(array5->getSize() == 4);
assert(array5->getValuesString().compare("2 3 4 0 ") == 0); assert(array5->getValuesString().compare("2 3 4 0 ") == 0);
/**
* Set and Ownership transfer
*/
double * doubleValues = new double[3];
doubleValues[0] = 0;
doubleValues[1] = 1.1;
doubleValues[2] = 10.1;
array5->setValues(doubleValues, 3, 1);
assert(array5->getSize() == 3);
assert(array5->getType().compare("Float") == 0);
assert(array5->getPrecision() == 8);
assert(array5->getValuesString().compare("0 1.1 10.1 ") == 0);
// //
// SHARED ASSIGNMENTS // SHARED ASSIGNMENTS
// //
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment