Skip to content

Commit

Permalink
Merge pull request #6534 from rouault/move_constructor
Browse files Browse the repository at this point in the history
CPLStringList and CPLJSONObject: implement move constructor
  • Loading branch information
rouault committed Oct 16, 2022
2 parents cec221b + e6e2d48 commit 592be23
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
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

0 comments on commit 592be23

Please sign in to comment.