[Development] RFC: Deprecating setSharable / isSharable in our containers and QString

Thiago Macieira thiago.macieira at intel.com
Thu Feb 20 18:12:29 CET 2014


Em qui 20 fev 2014, às 16:45:19, Tony Van Eerd escreveu:
> > -----Original Message-----
> > On Behalf Of Thiago Macieira
> > Subject: [Development] RFC: Deprecating setSharable / isSharable in our
> > containers and QString
> > 
> > ... and removing them in Qt 6
> > 
> > Please raise your hand if you knew we had that feature.
> 
> They aren't even documented, are they? (ie
> http://qt-project.org/doc/qt-5.0/qtcore/qvector-members.html )
> 
> And I couldn't find it on QString
> http://code.woboq.org/qt5/qtbase/src/corelib/tools/qstring.h.html

I'm guessing they're not.

> what about:
> 
> detach()
> isDetached()
> isSharedWith()
> 
> couldn't all these be "noops" and/or return true/false, etc, without
> breaking any code?  Isn't that the point of COW - you don't need to know?

I agree on detach(): you can easily trigger a detach() by calling data(). 
Well, as long as you ensure that your object is not const.

But we can't remove it because it's used by everything that does detaching. 
For example:

	T *data() { detach(); return d->data(); }

The others are obscure indeed. As part of my efforts, I want to make QString / 
QByteArray / QVector keep a null d pointer for anything that doesn't require 
deallocation and reference counting -- that is, everything that is a static 
literal and the null / empty situations. If I do that, then:

	QString hello = QStringLiteral("Hello");
	QString world = QStringLiteral("World");

	hello.d == world.d == nullptr;

So is hello.isSharedWith(world)? So I agree with you, we should also deprecate 
isSharedWith.

As for isDetached(), I have a use for it right now:

QByteArray QString::toLatin1_helper_inplace(QString &s)
{
    if (!s.isDetached())
        return s.toLatin1();

    // We can return our own buffer to the caller.
    // Conversion to Latin-1 always shrinks the buffer by half.
    const ushort *data = reinterpret_cast<const ushort *>(s.constData());
    uint length = s.size();

    // Swap the d pointers.
    // Kids, avert your eyes. Don't try this at home.
    [...]
}


-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center




More information about the Development mailing list