[Interest] QExplictlySharedDataPointer vs QSharedPointer

Elvis Stansvik elvstone at gmail.com
Thu Sep 8 22:21:51 CEST 2016


(I think you forgot to include the list in your reply)

2016-09-08 21:48 GMT+02:00 Konstantin Shegunov <kshegunov at gmail.com>:
> On Thu, Sep 8, 2016 at 8:13 PM, Elvis Stansvik <elvstone at gmail.com> wrote:
>>
>> Hm, okay. Perhaps a dumb question, but what are the consequences
>> internal vs external refcounting for me as a user of these classes?
>> Reading the docs for both of them, both seem to have a thread-safe
>> refcounting mechanism.
>
>
> As Thiago mentioned, one is referring to the data (internal ref counting)
> while the other is referring to a pointer referencing the data (external ref
> counting).

Right, and I see now that the consequence for me as a user is that in
the internal case, my data class must have the right structure (have
the ref member) if I'm to use QESDP.

So in a way I'm more free when using QSP as I don't have to think about that.

I know that it's mentioned in the docs what you bring up (and that
Thiago also brought up), that a QSP manages a *pointer*, while QESDP
manages *data*. But I still can't say that I'm 100% sure why I can't
do with QSP what I can do with QESDP...

>
>>
>> I see, so could this be the main reason for QESDP's existance (which I
>> didn't see at first)? The use-case is the same as for the implicit
>> variant, only when you want more control over when detach happens?
>
>
> I'd think so, yes.

Okay, but couldn't you do the same, and have the same control if using a QSP?

>
>>
>> Could you give some example of when you really want that kind of
>> control?
>
>
> Suppose I had a big piece of raw data (e.g. events from a nuclear detector
> system) that I'd want to operate on, but I don't want it to detach on each
> subsequent non-const call. It is of course true that I could pass a
> reference to that data through a pointer, but I like the pass-by-value way
> of doing things. If you're really crafty you could in principle put
> something similar to expression templates (semantics-wise) with the control
> explicit sharing gives you over the detach, but that's somewhat speculative
> as I haven't actually tried it.

In this use case, I can't quite see why I couldn't use a QSP instead:

1. The underlying data would not be implicitly copied on non-const
calls, same as with QESDP.
2. I'd have automatic clean-up when the last QSP is destroyed, same as
with QESDP.
3. I could do an explicit copy if I really wanted to, just in a
slightly different way than with QESDP.
4. I could pass the QSP by value (copying the QSP is cheap), e.g:

    void crunch(QSharedPointer<BigData> data);

  instead of

    void crunch(BigData data);

So is the difference really just the slightly different API and
slightly different way of doing a deep copy? (I consider the
external/internal refcount mostly a technical difference).

Elvis

>
> Kind regards.



More information about the Interest mailing list