Skip to content

Commit

Permalink
Support use of of std::pair<T, QString>
Browse files Browse the repository at this point in the history
  • Loading branch information
magnesj committed Jan 7, 2024
1 parent d38ebf6 commit b561656
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<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" );

Expand All @@ -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();
}

{
Expand All @@ -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() );
}
}

0 comments on commit b561656

Please sign in to comment.