Page 1 of 1

QxSerializeQDataStream_QSharedPointer

PostPosted: Tue Jul 24, 2018 8:01 pm
by Kasuax
The following code fails to compile when QxSerialization is turned off. Qt Serialization takes over and does not understand abstract types. I'm not sure how to turn Qt Serialization off too. It looks like it's too closely baked into the framework and needed for ancillary operations.

Code: Select all
template <typename T>
QDataStream & operator<< (QDataStream & stream, const QSharedPointer<T> & t)
{
{
   qint8 iIsNull = 0;
   stream >> iIsNull;
   if (! iIsNull) { t = QSharedPointer<T>(new T()); stream >> (* t); }
   else { t = QSharedPointer<T>(); }
   return stream;
}

Re: QxSerializeQDataStream_QSharedPointer

PostPosted: Mon Aug 20, 2018 9:37 am
by qxorm
Hello,

The following code fails to compile when QxSerialization is turned off.

What compilation options have you defined to turn it off ?
I'm not able to reproduce your issue.

Re: QxSerializeQDataStream_QSharedPointer

PostPosted: Mon Aug 20, 2018 3:47 pm
by Kasuax
This was my solution.

Code: Select all
template <typename T>
QDataStream & operator<< (QDataStream & stream, const QSharedPointer<T> & t)
{
   qint8 iIsNull = (t ? 0 : 1);

   if (boost::is_abstract<T>::value)
       iIsNull = 0;

   stream << iIsNull;
   if (t) { stream << (* t); }
   return stream;
}

Re: QxSerializeQDataStream_QSharedPointer

PostPosted: Mon Aug 20, 2018 3:47 pm
by Kasuax
It's not a good solution but it moved me a bit further...

Re: QxSerializeQDataStream_QSharedPointer

PostPosted: Tue Aug 21, 2018 8:02 am
by qxorm
boost serialization provides more features than Qt serialization.
For abstract classes, boost serialization iterates over all derived classes to check real C++ instance type ==> this is why it works with boost serialization.

With Qt serialization, you can't serialize a pointer to an abstract class for example : you have to cast it to real type before starting the serialization process.