Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CPLStringList and CPLJSONObject: implement move constructor #6534

Merged
merged 1 commit into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,15 @@ namespace tut
oCopy.Clear();
ensure( "c3", EQUAL(oCopy2[0],"test") );

// Test move constructor
CPLStringList oMoved(std::move(oCopy2));
ensure( "c_move_constructor", EQUAL(oMoved[0],"test") );

// Test move assignment operator
CPLStringList oMoved2;
oMoved2 = std::move(oMoved);
ensure( "c_move_assignment", EQUAL(oMoved2[0],"test") );

// Test sorting
CPLStringList oTestSort;
oTestSort.AddNameValue("Z", "1");
Expand Down Expand Up @@ -2224,17 +2233,25 @@ namespace tut
{
// Copy constructor
CPLJSONDocument oDocument;
ensure( oDocument.LoadMemory(std::string("true")) );
oDocument.GetRoot();
CPLJSONDocument oDocument2(oDocument);
CPLJSONObject oObj;
CPLJSONObject oObj(oDocument.GetRoot());
ensure( oObj.ToBool() );
CPLJSONObject oObj2(oObj);
ensure( oObj2.ToBool() );
// Assignment operator
oDocument2 = oDocument;
auto& oDocument2Ref(oDocument2);
oDocument2 = oDocument2Ref;
oObj2 = oObj;
auto& oObj2Ref(oObj2);
oObj2 = oObj2Ref;
CPLJSONObject oObj3(std::move(oObj2));
ensure( oObj3.ToBool() );
CPLJSONObject oObj4;
oObj4 = std::move(oObj3);
ensure( oObj4.ToBool() );
}
{
// Move constructor
Expand Down
7 changes: 7 additions & 0 deletions port/cpl_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,13 @@ CPLJSONObject::CPLJSONObject(const CPLJSONObject &other) :
{
}

CPLJSONObject::CPLJSONObject(CPLJSONObject &&other) :
m_poJsonObject(other.m_poJsonObject),
m_osKey(std::move(other.m_osKey))
{
other.m_poJsonObject = nullptr;
}

CPLJSONObject &CPLJSONObject::operator=(const CPLJSONObject &other)
{
if( this == &other )
Expand Down
1 change: 1 addition & 0 deletions port/cpl_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CPL_DLL CPLJSONObject
explicit CPLJSONObject(const std::string &osName, const CPLJSONObject &oParent);
~CPLJSONObject();
CPLJSONObject(const CPLJSONObject &other);
CPLJSONObject(CPLJSONObject &&other);
CPLJSONObject &operator=(const CPLJSONObject &other);
CPLJSONObject &operator=(CPLJSONObject &&other);

Expand Down
1 change: 1 addition & 0 deletions port/cpl_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ class CPL_DLL CPLStringList
explicit CPLStringList( char **papszList, int bTakeOwnership=TRUE );
explicit CPLStringList( CSLConstList papszList );
CPLStringList( const CPLStringList& oOther );
CPLStringList( CPLStringList&& oOther );
~CPLStringList();

CPLStringList &Clear();
Expand Down
12 changes: 12 additions & 0 deletions port/cplstringlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,18 @@ CPLStringList::CPLStringList( const CPLStringList &oOther ):
operator=(oOther);
}

/************************************************************************/
/* CPLStringList() */
/************************************************************************/

//! Move constructor
CPLStringList::CPLStringList( CPLStringList&& oOther ):
CPLStringList()

{
operator=(std::move(oOther));
}

/************************************************************************/
/* operator=() */
/************************************************************************/
Expand Down