[Interest] QExplictlySharedDataPointer vs QSharedPointer

Elvis Stansvik elvstone at gmail.com
Thu Sep 8 21:08:37 CEST 2016


2016-09-08 20:29 GMT+02:00 Thiago Macieira <thiago.macieira at intel.com>:
> On quinta-feira, 8 de setembro de 2016 18:50:48 PDT Elvis Stansvik wrote:
>> QExplicitlySharedDataPointer (QESDP) is a cousin of QISDP with the
>> important difference that you have to explicitly call detach() to
>> detach from the underlying data.
>>
>> QSharedPointer is Qt's own "smart pointer" to shared data, similar to
>> std::shared_ptr.
>>
>> I'm curious about the role/use for QESDP compared to QSharedPointer.
>> The main differences I can see is
>>
>>  * QESDP requires that the pointed-at object derive from QSharedData.
>>  * QSharedPointer provide no easy means to detach/make copies of the
>> underlying object
>
> That's the difference in what they're sharing: QSharedPointer shares a
> *pointer*, whereas QSharedData shares data (QSharedDataPointer points to the
> QSharedData).

Ah yes, I wasn't trying to compare QSP to QSharedData, but QSP to QESDP.

The reason I brought it up is that, when you take the implicit part
out of QSDP (turning it into QESDP), it started to look to me quite
similar to QSP. I thus started to wonder why QESDP exists at all. But
I think it's starting to sink in now.

> QESDP doesn't actually require that you derive from QSharedData,
> only that you have an atomic integer called "ref" in your structure.

I see, that makes sense. And I see now that that's really all that
QSharedData brings, the ref member.

>
> If you want to make copies of the object QSharedPointer holds, just do it like
> you've always done in C++. Or to create a new QSP:
>
>         auto copy = QSharedPointer::create<MyClass>(origin.constData());
>
> Do you want a convenience function? It's easy to add it, so please submit your
> contribution.

Oh, I wasn't really asking for a convenience function. In my
comparison to QESDP, I was just trying to say that with a QSP, you can
also do a copy of the underlying data, albeit not with the convenience
of a simple call to detach().

>
>> But regarding this second difference, wouldn't it be easy enough to
>> just take the raw pointer to the underlying object with data(), and
>> make a copy of that?
>
> Yes.
>
>> In short, I fail to see a strong use case for QESDP compared to using
>> a QSharedPointer. I'm probably missing something.
>
> The fact that it's a smart pointer, not a raw one.

This I don't quite understand. Aren't they both smart pointers? They
still seem quite similar to me. The difference mainly seems to be the
slightly different API, and where the refcount is stored
(external/internal).

>
>> Does anyone know of a code base that makes liberal use of QESDP, or
>> could you share how you're making use of it? It's easy to find uses
>> for the implicit variant (QISDP).
>
> qtxmlpatterns, I believe.

Thanks, I'll have a look at that and see how it's put to use there.

>
>> In general I'm curious to hear advise about the do's and don'ts when
>> it comes to QISDP, QESDP and QSharedPointer when designing the
>> innermost parts (data model) of a Qt application. I've heard some
>> warning words about using QISDP too library, because of the risks of
>> "accidental detach".
>
> QSharedPointer is std::shared_ptr; QSDP and QESDP are (non-existent, but in
> discussion) std::intrusive_ptr, with the former doing implicit detaching.

Thanks for pointering out std::intrusive_ptr, didn't know such a think
had been proposed.

With the above I was mainly looking for design advise: E.g. like when
would I choose to use QESDP vs QSDP, and when I would use QSP instead
of QESDP. I had also heard on IRC that some people preferred to always
work with QESDP instead of QSDP, thinking the lack of control with
QSDP was "dangerous", so was looking for opinions on that.

Thanks a lot for answering and helping me get sorted!

Elvis

>
> --
> Thiago Macieira - thiago.macieira (AT) intel.com
>   Software Architect - Intel Open Source Technology Center
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list