mirror of
https://git.dev.opencascade.org/repos/occt.git
synced 2025-08-09 13:22:24 +03:00
0029258: Foundation Classes - provide move constructors for string classes
New macro OCCT_NO_RVALUE_REFERENCE is introduced to disable methods using move semantics on obsolete compilers that do not support rvalue references. TCollection_AsciiString, TCollection_ExtendedString, NCollection_UtfString - added method Swap(), move constructor, and move assignment operator. Draw command QATestArrayMove is added to test for memory corruption if NCollection_Array1<> bound to local C buffer is returned from function by value.
This commit is contained in:
@@ -23,9 +23,11 @@
|
||||
#include <TCollection_ExtendedString.hxx>
|
||||
#include <TCollection_HAsciiString.hxx>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
// Shortcuts to standard allocate and reallocate functions
|
||||
static inline Standard_PCharacter Allocate(const Standard_Size aLength)
|
||||
{
|
||||
@@ -416,6 +418,15 @@ void TCollection_AsciiString::Copy(const TCollection_AsciiString& fromwhere)
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Swap
|
||||
// ----------------------------------------------------------------------------
|
||||
void TCollection_AsciiString::Swap (TCollection_AsciiString& theOther)
|
||||
{
|
||||
std::swap (mystring, theOther.mystring);
|
||||
std::swap (mylength, theOther.mylength);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Destroy
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@@ -80,6 +80,17 @@ public:
|
||||
|
||||
//! Initializes a AsciiString with another AsciiString.
|
||||
Standard_EXPORT TCollection_AsciiString(const TCollection_AsciiString& astring);
|
||||
|
||||
#ifndef OCCT_NO_RVALUE_REFERENCE
|
||||
//! Move constructor
|
||||
Standard_EXPORT TCollection_AsciiString (TCollection_AsciiString&& theOther)
|
||||
: mystring (theOther.mystring),
|
||||
mylength (theOther.mylength)
|
||||
{
|
||||
theOther.mystring = NULL;
|
||||
theOther.mylength = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
//! Initializes a AsciiString with copy of another AsciiString
|
||||
//! concatenated with the message character.
|
||||
@@ -270,7 +281,15 @@ void operator = (const TCollection_AsciiString& fromwhere)
|
||||
{
|
||||
Copy(fromwhere);
|
||||
}
|
||||
|
||||
|
||||
//! Exchange the data of two strings (without reallocating memory).
|
||||
Standard_EXPORT void Swap (TCollection_AsciiString& theOther);
|
||||
|
||||
#ifndef OCCT_NO_RVALUE_REFERENCE
|
||||
//! Move assignment operator
|
||||
TCollection_AsciiString& operator= (TCollection_AsciiString&& theOther) { Swap (theOther); return *this; }
|
||||
#endif
|
||||
|
||||
//! Frees memory allocated by AsciiString.
|
||||
Standard_EXPORT ~TCollection_AsciiString();
|
||||
|
||||
|
@@ -23,6 +23,7 @@
|
||||
#include <Standard_OutOfRange.hxx>
|
||||
#include <TCollection_AsciiString.hxx>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
|
||||
@@ -373,6 +374,15 @@ void TCollection_ExtendedString::Copy (const TCollection_ExtendedString& fromwhe
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Swap
|
||||
// ----------------------------------------------------------------------------
|
||||
void TCollection_ExtendedString::Swap (TCollection_ExtendedString& theOther)
|
||||
{
|
||||
std::swap (mystring, theOther.mystring);
|
||||
std::swap (mylength, theOther.mylength);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Destroy
|
||||
// ----------------------------------------------------------------------------
|
||||
|
@@ -100,7 +100,18 @@ public:
|
||||
|
||||
//! Initializes a ExtendedString with another ExtendedString.
|
||||
Standard_EXPORT TCollection_ExtendedString(const TCollection_ExtendedString& astring);
|
||||
|
||||
|
||||
#ifndef OCCT_NO_RVALUE_REFERENCE
|
||||
//! Move constructor
|
||||
Standard_EXPORT TCollection_ExtendedString (TCollection_ExtendedString&& theOther)
|
||||
: mystring (theOther.mystring),
|
||||
mylength (theOther.mylength)
|
||||
{
|
||||
theOther.mystring = NULL;
|
||||
theOther.mylength = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
//! Creation by converting an Ascii string to an extended
|
||||
//! string. The string is treated as having UTF-8 coding.
|
||||
//! If it is not a UTF-8 then each character is copied to ExtCharacter.
|
||||
@@ -140,7 +151,15 @@ void operator = (const TCollection_ExtendedString& fromwhere)
|
||||
{
|
||||
Copy(fromwhere);
|
||||
}
|
||||
|
||||
|
||||
//! Exchange the data of two strings (without reallocating memory).
|
||||
Standard_EXPORT void Swap (TCollection_ExtendedString& theOther);
|
||||
|
||||
#ifndef OCCT_NO_RVALUE_REFERENCE
|
||||
//! Move assignment operator
|
||||
TCollection_ExtendedString& operator= (TCollection_ExtendedString&& theOther) { Swap (theOther); return *this; }
|
||||
#endif
|
||||
|
||||
//! Frees memory allocated by ExtendedString.
|
||||
Standard_EXPORT ~TCollection_ExtendedString();
|
||||
|
||||
|
Reference in New Issue
Block a user