Results 1 to 6 of 6

Thread: returning Qlist of QDomDocument

  1. #1
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default returning Qlist of QDomDocument

    Hi

    I would like to know if when I return QList<QDomDocument> from function, is it copied in memory, or only some reference to this xml's is returned?
    These xml's contains files (base64 encoded) so they are really big.

    Simplified example is like this

    Qt Code:
    1. QList<QDomDocument> MyClass::createXmls() {
    2. QList<QDomDocument> xmls;
    3. for(int i=0;i<something;i++) {
    4. //create xmls
    5. // fill in xml...
    6. xmls.append(xml);
    7. }
    8. return xmls;
    9. }
    10. void MyClass::sendXmls() {
    11. QList<QDomDocument> xmls=this->createXmls();
    12. // process xmls in loop
    13. }
    To copy to clipboard, switch view to plain text mode 

    best regards
    Marek

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: returning Qlist of QDomDocument

    Nothing (well, almost) is copied. All relevant classes share data between their instances.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: returning Qlist of QDomDocument

    Thanks,

    Is this non-copying behaviour due to QDomDocument itself?
    As I read somewhere in docs, QDomDocument stays in memory until last variable is assigned to its memory area, and if all variables that were assigned are deleted then memory is freed. Is this why in case above nothing is copied upon returning from function? Because "normal" return from function is done by copying value

    Marek

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: returning Qlist of QDomDocument

    Quote Originally Posted by franki View Post
    Is this non-copying behaviour due to QDomDocument itself?
    Both to QList and QDomDocument. QList is an implicitly shared class, QDomDocument is an explicitly shared class.

    Because "normal" return from function is done by copying value
    Only the facade is copied, the data itself is a separate object hidden behind the facade and it's shared between instances thus the copy is cheap if you don't modify the data.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    31
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: returning Qlist of QDomDocument

    OK, so I suppose this is because QSharedData is used inside objects like QString or QList, and in docs there is said that this classes are "implicitly shared".
    So every class in Qt that is implicitly shared can be passed as argument to function or returned from function with minimum copying, and to determine that I should read about class constructor to check whether data is shared or not, right?
    I've found implicitly shared class list, but what about explicitly shared: QDomDocument, QMemArray<T>, QImage, and QMovie any other? and where to check if class is explicitly shared, there is nothing about this in QDomDocument class reference.

    best regards
    Marek

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: returning Qlist of QDomDocument

    Quote Originally Posted by franki View Post
    OK, so I suppose this is because QSharedData is used inside objects like QString or QList, and in docs there is said that this classes are "implicitly shared".
    So every class in Qt that is implicitly shared can be passed as argument to function or returned from function with minimum copying, and to determine that I should read about class constructor to check whether data is shared or not, right?
    Essentially yes. Almost all "value-based" classes in Qt are implicitly shared. These that are not, are simpler (faster) to copy than to share. So in all cases you can be sure copying the object is optimal.

    I've found implicitly shared class list, but what about explicitly shared: QDomDocument, QMemArray<T>, QImage, and QMovie any other? and where to check if class is explicitly shared, there is nothing about this in QDomDocument class reference.
    The difference between an implicitly shared class and an explicitly shared class is that if you modify a copy of an implicitly shared object, the object is automatically detached (the data is copied) from the original so that only the new object gets modified. For an explicitly shared class modifying one incarnation of the object, modifies them all. Regarding QDomDocument, the class is not marked as explicitly shared but you can see by its behaviour that it is. Besides, the docs for the copy constructor of QDomDocument says:

    Quote Originally Posted by QDomDocument
    The data of the copy is shared (shallow copy): modifying one node will also change the other.
    which effectively makes it an explicitly shared class.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    franki (20th April 2012)

Similar Threads

  1. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  2. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  3. Replies: 4
    Last Post: 20th August 2010, 13:54
  4. Split QDomDocument to new QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 4
    Last Post: 28th January 2009, 09:59
  5. QDomDocument inside other QDomDocument
    By estanisgeyer in forum Qt Programming
    Replies: 1
    Last Post: 13th November 2008, 15:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.