From b5616561c2a3bab4290438373a30160b5ba938ad Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Sun, 7 Jan 2024 13:34:30 +0100 Subject: [PATCH] Support use of of std::pair --- .../cafPdmXml/cafInternalPdmStreamOperators.h | 21 +++++++++++++++---- .../AggregatedTypesInField.cpp | 19 +++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h index 024c67b269..8983658a4b 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h +++ b/Fwk/AppFwk/cafProjectDataModel/cafPdmXml/cafInternalPdmStreamOperators.h @@ -79,6 +79,22 @@ QTextStream& operator<<( QTextStream& str, const std::pair& sobj ) template QTextStream& operator>>( QTextStream& str, std::pair& sobj ) +{ + T first; + U second; + + str >> first >> second; + + sobj = std::make_pair( first, second ); + + return str; +} + +//================================================================================================== +/// Explicit specialization for std::pair, as the string can contain spaces +//================================================================================================== +template +QTextStream& operator>>( QTextStream& str, std::pair& sobj ) { T first; @@ -100,10 +116,7 @@ QTextStream& operator>>( QTextStream& str, std::pair& sobj ) } } - QVariant v = restOfString; - U second = v.value(); - - sobj = std::make_pair( first, second ); + sobj = std::make_pair( first, restOfString ); return str; } diff --git a/Fwk/AppFwk/cafProjectDataModel/cafProjectDataModel_UnitTests/AggregatedTypesInField.cpp b/Fwk/AppFwk/cafProjectDataModel/cafProjectDataModel_UnitTests/AggregatedTypesInField.cpp index 31032b898b..4d00b3067c 100644 --- a/Fwk/AppFwk/cafProjectDataModel/cafProjectDataModel_UnitTests/AggregatedTypesInField.cpp +++ b/Fwk/AppFwk/cafProjectDataModel/cafProjectDataModel_UnitTests/AggregatedTypesInField.cpp @@ -52,11 +52,15 @@ class AggregatedTypes : public caf::PdmObject auto pair = std::make_pair( false, 12.0 ); CAF_PDM_InitField( &m_checkableDouble, "CheckableDouble", pair, "label text" ); + + auto strPair = std::make_pair( false, QString( "msj" ) ); + CAF_PDM_InitField( &m_checkableString, "CheckableString", strPair, "label text" ); } ~AggregatedTypes() {} - caf::PdmField> m_checkableDouble; + caf::PdmField> m_checkableDouble; + caf::PdmField> m_checkableString; }; CAF_PDM_SOURCE_INIT( AggregatedTypes, "AggregatedTypes" ); @@ -79,14 +83,19 @@ TEST( AggregatedTypeTest, MyTest ) QXmlStreamWriter xmlStream( &xml ); xmlStream.setAutoFormatting( true ); - const double testValue = 0.0012; + const double testValue = 0.0012; + const QString testString = "string with spaces"; { AggregatedTypes myObj; auto testPair = std::make_pair( true, testValue ); myObj.m_checkableDouble = testPair; - xml = myObj.writeObjectToXmlString(); + + auto testStrPair = std::make_pair( true, testString ); + myObj.m_checkableString = testStrPair; + + xml = myObj.writeObjectToXmlString(); } { @@ -95,8 +104,10 @@ TEST( AggregatedTypeTest, MyTest ) myObj.readObjectFromXmlString( xml, caf::PdmDefaultObjectFactory::instance() ); auto fieldValue = myObj.m_checkableDouble(); - ASSERT_TRUE( fieldValue.first ); ASSERT_DOUBLE_EQ( testValue, fieldValue.second ); + + auto fieldStrValue = myObj.m_checkableString(); + ASSERT_STREQ( testString.toStdString().data(), fieldStrValue.second.toStdString().data() ); } }