Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

Qt-interest Archive, July 2007
Forcing a QString deep copy without changing the data


Message 1 in thread

Version: QT4

QByteArray and QString have a fromRawData.  The pointer to the data  
is not interpreted so it is a nice way of loading raw bytes into the  
string.  The docs say that the QString/ByteArray will do a deep copy  
of the data if there is a change.  I want to force that deep copy  
without changing the contents.


So you might ask why.  I am reading a series of bytes.  The first one  
or two bytes, depending on those bytes, indicate Unicode or Ascii  
strings.  I read the raw data into a QByteArray.   If I discover that  
the data actually represents Unicode, I can do this.  QString  
aString; aString.fromRawData(aByteArray.data(), aByteArray.size());   
The problem is that the data pointer returned by aByteArray is owned  
by aByteArray.  How do I force the deep copy?

Best practice?

Michael

--
 [ signature omitted ] 

Message 2 in thread

On Wednesday 04 July 2007 04:35:55 Michael Simpson wrote:
> Version: QT4
>
> QByteArray and QString have a fromRawData.  The pointer to the data
> is not interpreted so it is a nice way of loading raw bytes into the
> string.  The docs say that the QString/ByteArray will do a deep copy
> of the data if there is a change.  I want to force that deep copy
> without changing the contents.
>
> So you might ask why.  I am reading a series of bytes.  The first one
> or two bytes, depending on those bytes, indicate Unicode or Ascii
> strings.  I read the raw data into a QByteArray.   If I discover that
> the data actually represents Unicode, I can do this.  QString
> aString; aString.fromRawData(aByteArray.data(), aByteArray.size());
> The problem is that the data pointer returned by aByteArray is owned
> by aByteArray.  How do I force the deep copy?
>
> Best practice?

I would do something like this:

    QString aString;
    aString = QString::fromRawData(aByteArray.constData(), aByteArray.size());

    ...

    // force a deep copy
    aString = QString::fromUtf16(aString.utf16());

-- 
 [ signature omitted ] 

Message 3 in thread

PERFECT!  And thank you!

M

On Jul 3, 2007, at 10:11 PM, Bradley T Hughes wrote:

> On Wednesday 04 July 2007 04:35:55 Michael Simpson wrote:
>> Version: QT4
>>
>> QByteArray and QString have a fromRawData.  The pointer to the data
>> is not interpreted so it is a nice way of loading raw bytes into the
>> string.  The docs say that the QString/ByteArray will do a deep copy
>> of the data if there is a change.  I want to force that deep copy
>> without changing the contents.
>>
>> So you might ask why.  I am reading a series of bytes.  The first one
>> or two bytes, depending on those bytes, indicate Unicode or Ascii
>> strings.  I read the raw data into a QByteArray.   If I discover that
>> the data actually represents Unicode, I can do this.  QString
>> aString; aString.fromRawData(aByteArray.data(), aByteArray.size());
>> The problem is that the data pointer returned by aByteArray is owned
>> by aByteArray.  How do I force the deep copy?
>>
>> Best practice?
>
> I would do something like this:
>
>     QString aString;
>     aString = QString::fromRawData(aByteArray.constData(),  
> aByteArray.size());
>
>     ...
>
>     // force a deep copy
>     aString = QString::fromUtf16(aString.utf16());
>
> -- 
> Bradley T. Hughes - bhughes at trolltech.com
> Trolltech ASA - Sandakervn. 116, P.O. Box 4332 Nydalen, 0402 Oslo,  
> Norway

--
 [ signature omitted ] 

Message 4 in thread

On 7/4/07, Michael Simpson <michaelsimpson@xxxxxxx> wrote:
>
> Version: QT4
>
> QByteArray and QString have a fromRawData.  The pointer to the data
> is not interpreted so it is a nice way of loading raw bytes into the
> string.  The docs say that the QString/ByteArray will do a deep copy
> of the data if there is a change.  I want to force that deep copy
> without changing the contents.
>
AFAIK, calling any non-const method, will cause a deep copy. Kinda dirty
trick, but should work fine.

You could create several overloads for different implicitly shared classed
like:
QString detach(const QString& str) {
   QString detached(str);
   detached.data();
   return detached;
}

then, you can use it like this:
  foo(detach(someOtherStr)); //foo receives a uniq string (hopefully)

-- 
 [ signature omitted ] 

Message 5 in thread

I like it but is the a risk that Trolltech could change this  
implementation and break it later?  I'll have to look at the code.

Thanks,
M

On Jul 4, 2007, at 8:05 AM, Pavel Antokolsky aka Zigmar wrote:

> On 7/4/07, Michael Simpson <michaelsimpson@xxxxxxx> wrote:
> Version: QT4
>
> QByteArray and QString have a fromRawData.  The pointer to the data
> is not interpreted so it is a nice way of loading raw bytes into the
> string.  The docs say that the QString/ByteArray will do a deep copy
> of the data if there is a change.  I want to force that deep copy
> without changing the contents.
> AFAIK, calling any non-const method, will cause a deep copy. Kinda  
> dirty trick, but should work fine.
>
> You could create several overloads for different implicitly shared  
> classed like:
> QString detach(const QString& str) {
>    QString detached(str);
>    detached.data();
>    return detached;
> }
>
> then, you can use it like this:
>   foo(detach(someOtherStr)); //foo receives a uniq string (hopefully)
>
> -- 
> Best regards,
> Zigmar