From: kgv Date: Tue, 12 Mar 2019 08:14:15 +0000 (+0300) Subject: math_Vector, math_IntegerVector - added move constructors. X-Git-Url: http://git.dev.opencascade.org/gitweb/?a=commitdiff_plain;h=fca968d2fa64aff5650c2cadf8ebb11c74089567;p=occt-copy.git math_Vector, math_IntegerVector - added move constructors. --- diff --git a/src/NCollection/NCollection_LocalArray.hxx b/src/NCollection/NCollection_LocalArray.hxx index 06050493f7..cadfe2817a 100644 --- a/src/NCollection/NCollection_LocalArray.hxx +++ b/src/NCollection/NCollection_LocalArray.hxx @@ -59,6 +59,32 @@ public: return myPtr; } + //! Move assignment. + //! This array will borrow all the data from theOther. + NCollection_LocalArray& Move (NCollection_LocalArray& theOther) + { + if (&theOther == this) + { + return *this; + } + + Deallocate(); + mySize = theOther.mySize; + if (theOther.myPtr == theOther.myBuffer) + { + // deep copy + myPtr = myBuffer; + memcpy (myPtr, theOther.myPtr, sizeof(theItem) * theOther.mySize); + memset (theOther.myPtr, 0, sizeof(theItem) * theOther.mySize); + } + else + { + myPtr = theOther.myPtr; + theOther.myPtr = theOther.myBuffer; + } + return *this; + } + private: NCollection_LocalArray (const NCollection_LocalArray& ); diff --git a/src/math/math_IntegerVector.hxx b/src/math/math_IntegerVector.hxx index 5241fc1820..40e1e0e370 100644 --- a/src/math/math_IntegerVector.hxx +++ b/src/math/math_IntegerVector.hxx @@ -256,6 +256,37 @@ public: return theO; } +#ifndef OCCT_NO_RVALUE_REFERENCE + //! Move constructor + math_IntegerVector (math_IntegerVector&& theOther) + { + Move (theOther); + } + + //! Move assignment operator; @sa Move() + math_IntegerVector& operator= (math_IntegerVector&& theOther) + { + return Move (theOther); + } +#endif + + //! Move assignment. + //! This array will borrow all the data from theOther, so that theOther should not be used anymore. + math_IntegerVector& Move (math_IntegerVector& theOther) + { + if (&theOther == this) + { + return *this; + } + + myLocArray.Move (theOther.myLocArray); + NCollection_Array1 aNewArray (myLocArray[0], theOther.Array.Lower(), theOther.Array.Upper()); + Array.Move (aNewArray); + NCollection_Array1 anEmptyArray; + theOther.Array.Move (anEmptyArray); + return *this; + } + protected: //! is used internally to set the Lower value of the IntegerVector. diff --git a/src/math/math_Vector.hxx b/src/math/math_Vector.hxx index 308d889880..91c666ed6b 100644 --- a/src/math/math_Vector.hxx +++ b/src/math/math_Vector.hxx @@ -331,6 +331,37 @@ public: friend class math_Matrix; +#ifndef OCCT_NO_RVALUE_REFERENCE + //! Move constructor + math_Vector (math_Vector&& theOther) + { + Move (theOther); + } + + //! Move assignment operator; @sa Move() + math_Vector& operator= (math_Vector&& theOther) + { + return Move (theOther); + } +#endif + + //! Move assignment. + //! This array will borrow all the data from theOther, so that theOther should not be used anymore. + math_Vector& Move (math_Vector& theOther) + { + if (&theOther == this) + { + return *this; + } + + myLocArray.Move (theOther.myLocArray); + NCollection_Array1 aNewArray (myLocArray[0], theOther.Array.Lower(), theOther.Array.Upper()); + Array.Move (aNewArray); + NCollection_Array1 anEmptyArray; + theOther.Array.Move (anEmptyArray); + return *this; + } + protected: //! Is used internally to set the "theLower" value of the vector.