Commit 6e573ad7 authored by Henry Weller's avatar Henry Weller
Browse files

UList: Rationalize assignment (shallow-copy vs deep-copy)

    //- Disallow default shallow-copy assignment
    //
    //  Assignment of UList<T> may need to be either shallow (copy pointer)
    //  or deep (copy elements) depending on context or the particular type
    //  of list derived from UList and it is confusing and prone to error
    //  for the default assignment to be either.  The solution is to
    //  disallow default assignment and provide separate 'shallowCopy' and
    //  'deepCopy' member functions.
    void operator=(const UList<T>&) = delete;

    //- Copy the pointer held by the given UList.
    inline void shallowCopy(const UList<T>&);

    //- Copy elements of the given UList.
    void deepCopy(const UList<T>&);
parent 4831f814
......@@ -56,8 +56,8 @@ int main(int argc, char *argv[])
rowSizes[1] = row1.size();
cll1.resize(rowSizes);
cll1[0].assign(row0); //note: operator= will not work since UList
cll1[1].assign(row1);
cll1[0].deepCopy(row0);
cll1[1].deepCopy(row1);
Info<< "cll1:" << cll1 << endl;
forAll(cll1.m(), i)
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -64,10 +64,8 @@ int main(int argc, char *argv[])
// Create field with my local data
pointField coords(globalPointSlavesMap.constructSize());
SubList<point>(coords, coupledPatch.nPoints()).assign
(
coupledPatch.localPoints()
);
SubList<point>(coords, coupledPatch.nPoints()) =
coupledPatch.localPoints();
// Exchange data. Apply positional transforms.
globalPointSlavesMap.distribute
......@@ -185,8 +183,7 @@ int main(int argc, char *argv[])
label nBnd = mesh.nFaces()-mesh.nInternalFaces();
pointField fc(globalPointBoundaryFacesMap.constructSize());
SubList<point>(fc, nBnd).assign
(
SubList<point>(fc, nBnd) =
primitivePatch
(
SubList<face>
......@@ -196,8 +193,7 @@ int main(int argc, char *argv[])
mesh.nInternalFaces()
),
mesh.points()
).faceCentres()
);
).faceCentres();
// Exchange data
globalPointBoundaryFacesMap.distribute
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -240,7 +240,7 @@ bool setFaceFieldType
field.boundaryField()[patchi].size(),
field.boundaryField()[patchi].patch().start()
- mesh.nInternalFaces()
).assign(field.boundaryField()[patchi]);
) = field.boundaryField()[patchi];
}
// Override
......
......@@ -424,14 +424,14 @@ int main(int argc, char *argv[])
(
availablePoints,
upp.faceCentres().size()
).assign(upp.faceCentres());
) = upp.faceCentres();
SubList<point>
(
availablePoints,
upp.localPoints().size(),
upp.faceCentres().size()
).assign(upp.localPoints());
) = upp.localPoints();
point cfo = cf;
scalar dist = GREAT;
......@@ -592,8 +592,8 @@ int main(int argc, char *argv[])
DynamicList<label> compactPatchId(map.constructSize());
// Insert my coarse local values
SubList<point>(compactCoarseSf, nCoarseFaces).assign(localCoarseSf);
SubList<point>(compactCoarseCf, nCoarseFaces).assign(localCoarseCf);
SubList<point>(compactCoarseSf, nCoarseFaces) = localCoarseSf;
SubList<point>(compactCoarseCf, nCoarseFaces) = localCoarseCf;
// Insert my fine local values
label compactI = 0;
......
......@@ -87,6 +87,9 @@ public:
//- Allow cast to a const List<T>&
inline operator const Foam::List<T>&() const;
//- Assignment of all entries to the given sub-list
inline void operator=(const SubList<T>&);
//- Assignment of all entries to the given list
inline void operator=(const UList<T>&);
......
......@@ -87,10 +87,17 @@ inline Foam::SubList<T>::operator const Foam::List<T>&() const
}
template<class T>
inline void Foam::SubList<T>::operator=(const SubList<T>& sl)
{
UList<T>::deepCopy(sl);
}
template<class T>
inline void Foam::SubList<T>::operator=(const UList<T>& l)
{
UList<T>::assign(l);
UList<T>::deepCopy(l);
}
......
......@@ -34,7 +34,7 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
void Foam::UList<T>::assign(const UList<T>& a)
void Foam::UList<T>::deepCopy(const UList<T>& a)
{
if (a.size_ != this->size_)
{
......
......@@ -80,6 +80,19 @@ class UList
T* __restrict__ v_;
// Private Member Functions
//- Disallow default shallow-copy assignment
//
// Assignment of UList<T> may need to be either shallow (copy pointer)
// or deep (copy elements) depending on context or the particular type
// of list derived from UList and it is confusing and prone to error
// for the default assignment to be either. The solution is to
// disallow default assignment and provide separate 'shallowCopy' and
// 'deepCopy' member functions.
void operator=(const UList<T>&) = delete;
public:
// Related types
......@@ -90,11 +103,13 @@ public:
//- Declare friendship with the SubList class
friend class SubList<T>;
// Static Member Functions
//- Return a null UList
inline static const UList<T>& null();
// Public classes
//- Less function class that can be used for sorting
......@@ -198,15 +213,18 @@ public:
inline void checkIndex(const label i) const;
//- Copy the pointer held by the given UList.
inline void shallowCopy(const UList<T>&);
//- Copy elements of the given UList.
void deepCopy(const UList<T>&);
//- Write the UList as a dictionary entry.
void writeEntry(Ostream&) const;
//- Write the UList as a dictionary entry with keyword.
void writeEntry(const word& keyword, Ostream&) const;
//- Assign elements to those from UList.
void assign(const UList<T>&);
// Member operators
......@@ -327,7 +345,7 @@ public:
// STL member operators
//- Equality operation on ULists of the same type.
// Returns true when the ULists are elementwise equal
// Returns true when the ULists are element-wise equal
// (using UList::value_type::operator==). Takes linear time.
bool operator==(const UList<T>&) const;
......
......@@ -152,6 +152,14 @@ inline T* Foam::UList<T>::data()
}
template<class T>
inline void Foam::UList<T>::shallowCopy(const UList<T>& a)
{
size_ = a.size_;
v_ = a.v_;
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
......
......@@ -158,7 +158,7 @@ slicedBoundaryField
DimensionedField<Type, GeoMesh>::null()
)
);
bf[patchi].UList<Type>::operator=(bField[patchi]);
bf[patchi].shallowCopy(bField[patchi]);
}
}
......@@ -193,7 +193,7 @@ DimensionedInternalField::DimensionedInternalField
)
{
// Set the internalField to the slice of the complete field
UList<Type>::operator=
UList<Type>::shallowCopy
(
typename Field<Type>::subField(iField, GeoMesh::size(mesh))
);
......@@ -227,7 +227,7 @@ SlicedGeometricField
)
{
// Set the internalField to the slice of the complete field
UList<Type>::operator=
UList<Type>::shallowCopy
(
typename Field<Type>::subField(completeField, GeoMesh::size(mesh))
);
......@@ -271,7 +271,7 @@ SlicedGeometricField
)
{
// Set the internalField to the slice of the complete field
UList<Type>::operator=
UList<Type>::shallowCopy
(
typename Field<Type>::subField(completeIField, GeoMesh::size(mesh))
);
......@@ -305,7 +305,7 @@ SlicedGeometricField
)
{
// Set the internalField to the supplied internal field
UList<Type>::operator=(gf.internalField());
UList<Type>::shallowCopy(gf.internalField());
correctBoundaryConditions();
}
......@@ -334,7 +334,7 @@ SlicedGeometricField
)
{
// Set the internalField to the supplied internal field
UList<Type>::operator=(gf.internalField());
UList<Type>::shallowCopy(gf.internalField());
}
......@@ -352,7 +352,7 @@ Foam::SlicedGeometricField<Type, PatchField, SlicedPatchField, GeoMesh>::
{
// Set the internalField storage pointer to NULL before its destruction
// to protect the field it a slice of.
UList<Type>::operator=(UList<Type>(NULL, 0));
UList<Type>::shallowCopy(UList<Type>(NULL, 0));
}
......@@ -368,7 +368,7 @@ DimensionedInternalField::~DimensionedInternalField()
{
// Set the internalField storage pointer to NULL before its destruction
// to protect the field it a slice of.
UList<Type>::operator=(UList<Type>(NULL, 0));
UList<Type>::shallowCopy(UList<Type>(NULL, 0));
}
......
......@@ -51,7 +51,7 @@ void Foam::LUscalarMatrix::solve
(
X,
x.size()
).assign(x);
) = x;
for
(
......
......@@ -497,14 +497,13 @@ void Foam::GAMGSolver::procAgglomerateMatrix
if (coarsestMatrix.hasDiag())
{
scalarField& allDiag = allMatrix.diag();
SubList<scalar>
(
allDiag,
coarsestMatrix.diag().size()
).assign
(
coarsestMatrix.diag()
);
) = coarsestMatrix.diag();
forAll(otherMats, i)
{
SubList<scalar>
......@@ -512,10 +511,7 @@ void Foam::GAMGSolver::procAgglomerateMatrix
allDiag,
otherMats[i].diag().size(),
cellOffsets[i+1]
).assign
(
otherMats[i].diag()
);
) = otherMats[i].diag();
}
}
if (coarsestMatrix.hasLower())
......
......@@ -295,7 +295,7 @@ void Foam::GAMGSolver::Vcycle
// used
if (nPreSweeps_)
{
preSmoothedCoarseCorrField.assign(coarseCorrFields[leveli]);
preSmoothedCoarseCorrField = coarseCorrFields[leveli];
}
agglomeration_.prolongField
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2013-2015 OpenFOAM Foundation
\\ / A nd | Copyright (C) 2013-2016 OpenFOAM Foundation
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -44,7 +44,7 @@ void Foam::globalIndex::gather
allFld.setSize(off.last());
// Assign my local data
SubList<Type>(allFld, fld.size(), 0).assign(fld);
SubList<Type>(allFld, fld.size(), 0) = fld;
if (commsType == Pstream::scheduled || commsType == Pstream::blocking)
{
......@@ -207,7 +207,7 @@ void Foam::globalIndex::scatter
{
if (Pstream::myProcNo(comm) == procIDs[0])
{
fld.assign(SubList<Type>(allFld, off[1]-off[0]));
fld.deepCopy(SubList<Type>(allFld, off[1]-off[0]));
if (commsType == Pstream::scheduled || commsType == Pstream::blocking)
{
......
......@@ -582,7 +582,7 @@ operator=
{
clearOut();
FaceList<Face>::operator=(pp);
FaceList<Face>::shallowCopy(pp);
}
......
......@@ -88,7 +88,7 @@ void Foam::UPstream::allToAll
const label communicator
)
{
recvData.assign(sendData);
recvData.deepCopy(sendData);
}
......
......@@ -316,7 +316,7 @@ void Foam::UPstream::allToAll
if (!UPstream::parRun())
{
recvData.assign(sendData);
recvData.deepCopy(sendData);
}
else
{
......
......@@ -566,10 +566,8 @@ void Foam::fvMeshDistribute::getNeighbourData
}
// Which processor they will end up on
SubList<label>(nbrNewNbrProc, pp.size(), offset).assign
(
UIndirectList<label>(distribution, pp.faceCells())()
);
SubList<label>(nbrNewNbrProc, pp.size(), offset) =
UIndirectList<label>(distribution, pp.faceCells())();
}
}
......@@ -1074,7 +1072,7 @@ void Foam::fvMeshDistribute::sendMesh
if (myZoneID != -1)
{
zonePoints[nameI].assign(pointZones[myZoneID]);
zonePoints[nameI].deepCopy(pointZones[myZoneID]);
}
}
}
......@@ -1106,8 +1104,8 @@ void Foam::fvMeshDistribute::sendMesh
if (myZoneID != -1)
{
zoneFaces[nameI].assign(faceZones[myZoneID]);
zoneFaceFlip[nameI].assign(faceZones[myZoneID].flipMap());
zoneFaces[nameI].deepCopy(faceZones[myZoneID]);
zoneFaceFlip[nameI].deepCopy(faceZones[myZoneID].flipMap());
}
}
}
......@@ -1137,7 +1135,7 @@ void Foam::fvMeshDistribute::sendMesh
if (myZoneID != -1)
{
zoneCells[nameI].assign(cellZones[myZoneID]);
zoneCells[nameI].deepCopy(cellZones[myZoneID]);
}
}
}
......
......@@ -38,7 +38,7 @@ Foam::slicedFvPatchField<Type>::slicedFvPatchField
fvPatchField<Type>(p, iF, Field<Type>())
{
// Set the fvPatchField to a slice of the given complete field
UList<Type>::operator=(p.patchSlice(completeField));
UList<Type>::shallowCopy(p.patchSlice(completeField));
}
......@@ -92,7 +92,7 @@ Foam::slicedFvPatchField<Type>::slicedFvPatchField
fvPatchField<Type>(ptf.patch(), iF, Field<Type>())
{
// Transfer the slice from the argument
UList<Type>::operator=(ptf);
UList<Type>::shallowCopy(ptf);
}
......@@ -121,7 +121,7 @@ Foam::slicedFvPatchField<Type>::slicedFvPatchField
)
{
// Transfer the slice from the argument
UList<Type>::operator=(ptf);
UList<Type>::shallowCopy(ptf);
}
......@@ -144,7 +144,7 @@ Foam::slicedFvPatchField<Type>::~slicedFvPatchField()
{
// Set the fvPatchField storage pointer to NULL before its destruction
// to protect the field it a slice of.
UList<Type>::operator=(UList<Type>(NULL, 0));
UList<Type>::shallowCopy(UList<Type>(NULL, 0));
}
......
......@@ -38,7 +38,7 @@ Foam::slicedFvsPatchField<Type>::slicedFvsPatchField
fvsPatchField<Type>(p, iF, Field<Type>())
{
// Set the fvsPatchField to a slice of the given complete field
UList<Type>::operator=(p.patchSlice(completeField));
UList<Type>::shallowCopy(p.patchSlice(completeField));
}
......@@ -92,7 +92,7 @@ Foam::slicedFvsPatchField<Type>::slicedFvsPatchField
fvsPatchField<Type>(ptf.patch(), iF, Field<Type>())
{
// Transfer the slice from the argument
UList<Type>::operator=(ptf);
UList<Type>::shallowCopy(ptf);
}
......@@ -121,7 +121,7 @@ Foam::slicedFvsPatchField<Type>::slicedFvsPatchField
)
{
// Transfer the slice from the argument
UList<Type>::operator=(ptf);
UList<Type>::shallowCopy(ptf);
}
......@@ -144,7 +144,7 @@ Foam::slicedFvsPatchField<Type>::~slicedFvsPatchField()
{
// Set the fvsPatchField storage pointer to NULL before its destruction
// to protect the field it a slice of.
UList<Type>::operator=(UList<Type>(NULL, 0));
UList<Type>::shallowCopy(UList<Type>(NULL, 0));
}
......
Markdown is supported
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