Support use of of std::pair<T, QString>

This commit is contained in:
Magne Sjaastad 2024-01-07 13:34:30 +01:00
parent d38ebf68f4
commit b5616561c2
2 changed files with 32 additions and 8 deletions

View File

@ -79,6 +79,22 @@ QTextStream& operator<<( QTextStream& str, const std::pair<T, U>& sobj )
template <typename T, typename U>
QTextStream& operator>>( QTextStream& str, std::pair<T, U>& sobj )
{
T first;
U second;
str >> first >> second;
sobj = std::make_pair( first, second );
return str;
}
//==================================================================================================
/// Explicit specialization for std::pair<T, QString>, as the string can contain spaces
//==================================================================================================
template <typename T>
QTextStream& operator>>( QTextStream& str, std::pair<T, QString>& sobj )
{
T first;
@ -100,10 +116,7 @@ QTextStream& operator>>( QTextStream& str, std::pair<T, U>& sobj )
}
}
QVariant v = restOfString;
U second = v.value<U>();
sobj = std::make_pair( first, second );
sobj = std::make_pair( first, restOfString );
return str;
}

View File

@ -52,11 +52,15 @@ public:
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<std::pair<bool, double>> m_checkableDouble;
caf::PdmField<std::pair<bool, double>> m_checkableDouble;
caf::PdmField<std::pair<bool, QString>> 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() );
}
}