Move operator (ctor and =).

This commit is contained in:
Jief L 2020-03-28 19:17:02 +03:00
parent 1cb19148e5
commit 324b0608e6
3 changed files with 41 additions and 21 deletions

View File

@ -33,7 +33,7 @@ const XString NullXString;
void XString::Init(xsize aSize)
{
m_data = (char*)malloc( (aSize+1)*sizeof(char) ); /* le 0 terminal n'est pas compté dans mSize */
m_data = (char*)malloc( (aSize+1)*sizeof(char) ); /* le 0 terminal n'est pas compté dans m_allocatedSize */
if ( !m_data ) {
DebugLog(2, "XString::Init(%llu) : Xalloc returned NULL. Cpu halted\n", (aSize+1)*sizeof(char));
panic();
@ -51,7 +51,7 @@ XString::XString()
XString::~XString()
{
//Debugf("Destructeur :%s\n", c);
delete m_data;
delete m_data; // delete nullptr do nothing
}
void XString::setLength(xsize len)
@ -67,7 +67,7 @@ char *XString::CheckSize(xsize nNewSize, xsize nGrowBy)
{
nNewSize += nGrowBy;
m_data = (char*)realloc(m_data, (nNewSize+1)*sizeof(char), m_allocatedSize*sizeof(wchar_t));
m_data = (char*)realloc(m_data, (nNewSize+1)*sizeof(char), m_allocatedSize*sizeof(wchar_t)); // realloc is identical to malloc if m_data is NULL
if ( !m_data ) {
DBG("XString::CheckSize(%d, %d) : Xrealloc(%d, %d, %d) returned NULL. System halted\n", nNewSize, nGrowBy, m_size, (nNewSize+1)*sizeof(char), c);
panic();
@ -349,6 +349,14 @@ XString::XString(const XString &aString)
StrnCpy(aString.data(), aString.length());
}
XString::XString(XString&& aString) // Move constructor
{
m_data = aString.m_data;
m_allocatedSize = aString.m_allocatedSize;
aString.m_data = 0;
aString.m_allocatedSize = 0;
}
//// Deactivate assignment during refactoring to avoid confusion
//XString::XString(const wchar_t *S)
//{
@ -828,23 +836,16 @@ const XString &XString::operator =(const XString &aString)
return *this;
}
#ifdef __AFXWIN_H__
const XString &XString::operator =(const CString &aCString)
XString& XString::operator =(XString&& aString)
{
//TRACE("Operator =const CString&\n");
StrnCpy(aCString, aCString.GetLength());
delete m_data; // delete does nothing if m_data is NULL
m_data = aString.m_data;
m_allocatedSize = aString.m_allocatedSize;
aString.m_data = 0;
aString.m_allocatedSize = 0;
return *this;
}
#endif
#ifdef _WX_WXSTRINGH__
const XString &XString::operator =(const wxString &awxString)
{
//TRACE("Operator =const wxString&\n");
StrnCpy(awxString.mb_str(), awxString.length());
return *this;
}
#endif
//
//const XString &XString::operator =(const XConstString &aConstString)
//{
@ -1082,7 +1083,7 @@ XString ToUpper(const char *S)
}
#endif
// Deactivate assignment during refactoring to avoid confusion
// Deactivate during refactoring to avoid confusion
//XString CleanCtrl(const XString &S)
//{
// XString ReturnValue;

View File

@ -43,6 +43,8 @@ class XString
void Init(xsize aSize=0);
XString();
XString(const XString &aString);
XString(XString&& aString); // Move constructor
// XString(const XConstString &aConstString);
// XString(const char *S);
@ -124,7 +126,8 @@ class XString
const XString& takeValueFrom(const char* S, xsize count) { StrnCpy(S, count); return *this; }
const XString& takeValueFrom(const wchar_t* S) { SPrintf("%ls", S); return *this; }
const XString &operator =(const XString &aString);
const XString& operator =(const XString& aString);
XString& operator =(XString&& aString);
// const XString &operator =(const XConstString &aConstString);
// Deactivate assignment during refactoring to avoid confusion
@ -226,7 +229,9 @@ class XString
// OpÈrateur +
// Chaines
friend XString operator + (const XString& p1, const XString& p2) { XString s; s=p1; s+=p2; return s; }
friend XString operator + (const XString& p1, const char *p2 ) { XString s; s=p1; s+=p2; return s; }
// friend XString operator + (const XString& p1, const char *p2 ) { XString s; s=p1; s+=p2; return s; }
XString operator + (const char *p2 ) { XString s(*this); s+=p2; return s; }
friend XString operator + (const char *p1, const XString& p2) { XString s; s.takeValueFrom(p1); s+=p2; return s; }
// friend XString operator + (const XConstString& p1, const XString& p2) { XString s; s=p1; s+=p2; return s; }
// friend XString operator + (const XString& p1, const XConstString& p2) { XString s; s=p1; s+=p2; return s; }

View File

@ -4,12 +4,13 @@
#include "global_test.h"
//#include <wchar.h>
int XString_tests()
{
#ifdef JIEF_DEBUG
#endif
#ifdef JIEF_DEBUG
// DebugLog(2, "XStringW_tests -> Enter\n");
#endif
@ -48,6 +49,19 @@ int XString_tests()
}
#endif
// Move assignment.
{
XString str1;
str1.takeValueFrom("str1");
XString str2(str1 + "b");
str2 += "a"; // this is more efficient than str2 = str2 + "a", of course
if ( str2 != "str1ba" ) return 120;
str2 = str2 + "a";
if ( str2 != "str1baa" ) return 121;
str2 = str1 + "a"; // this must call the move assignment operator
if ( str2 != "str1a" ) return 122;
}
// check [] operator
{
XString str;