Results 1 to 12 of 12

Thread: when will be QSharedFromThis for QSharedPointer?

  1. #1
    Join Date
    Nov 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default when will be QSharedFromThis for QSharedPointer?

    When this feature https://bugreports.qt-project.org/browse/QTBUG-7287 will be released in Qt?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: when will be QSharedFromThis for QSharedPointer?

    How should we know?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by fsmoke View Post
    When this feature https://bugreports.qt-project.org/browse/QTBUG-7287 will be released in Qt?
    If someone finishes the patch, including the documentation, gets it past the basic code review process and argues the case for having it in the first place then it may get into a future Qt. Since nobody has touched the bug since 2010 I don't think anyone's need to scratch this itch is strong enough.

  4. #4
    Join Date
    Nov 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by ChrisW67 View Post
    If someone finishes the patch, including the documentation, gets it past the basic code review process and argues the case for having it in the first place then it may get into a future Qt. Since nobody has touched the bug since 2010 I don't think anyone's need to scratch this itch is strong enough.
    But without this functional - Qt shared pointer mechanizm is no full - it's not useful!! I port small tree class from boost to Qt, some like this

    class TreeNode
    {
    public:
    void addChild(QSharedPointer<TreeNode> p);
    private:
    QWeakPointer _parent;
    QVector<QSharedPointer<TreeNode>> _childs;
    };

    inside method addChild(boost realization) new child TreeNode field _parent was assigned to shared_from_this - in Qt realization i can't do this.
    Besides TreeNode is parent class to other types and they can add child nodes themself. So I need to rewrite common architecture of my application - I don't want do this.

  5. #5
    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: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by fsmoke View Post
    in Qt realization i can't do this.
    So implement something with the same functionality. However I don't see why you can't get the same behaviour with just QSharedPointer and QWeakPointer. I don't see a need for any other pointers here.

    Isn't it enough to just keep a weak pointer to itself in the object and promote it to strong ref if needed?

    I have an alternative approach in mind if the weak pointer approach doesn't work.
    Last edited by wysota; 19th November 2012 at 06:32.
    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.


  6. #6
    Join Date
    Nov 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by wysota View Post
    So implement something with the same functionality. However I don't see why you can't get the same behaviour with just QSharedPointer and QWeakPointer. I don't see a need for any other pointers here.

    Isn't it enough to just keep a weak pointer to itself in the object and promote it to strong ref if needed?
    oh my god facepalm.gif

    Simple realization of addChild
    Qt Code:
    1. TreeNode::addChild(boost::shared_ptr<TreeNode> p)
    2. {
    3. if (!p._parent.expired())
    4. throw std::runtime_error("TreeNode can't have multiple parents!");
    5. p._parent = shared_from_this(). //!!!!!!!!!
    6. _childs.push_back(p);
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: when will be QSharedFromThis for QSharedPointer?

    But without this functional - Qt shared pointer mechanizm is no full - it's not useful!!
    That's a bit like saying the C++ standard library is not useful because it does not have a "shared_from_this" mechanism either. Clearly that's why Boost implemented the functionality. You can use the Boost shared_ptr/weak and shared_from_this with a program that also uses Qt, so I'm not sure where the real problem is here.

  8. #8
    Join Date
    Nov 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by ChrisW67 View Post
    That's a bit like saying the C++ standard library is not useful because it does not have a "shared_from_this" mechanism either.
    Fun ) C++ standard library have "shared_from_this" mechanism
    http://en.cppreference.com/w/cpp/mem...ared_from_this


    Clearly that's why Boost implemented the functionality. You can use the Boost shared_ptr/weak and shared_from_this with a program that also uses Qt, so I'm not sure where the real problem is here.
    Yes it was written in this way(boost + Qt) but I don't like code style mixing, so I rewrote it in clear Qt, because Qt no uses default c/c++ code style like in stl or
    boost. It's difficult to support projects with multiple codestyles - it's very bad practice.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by fsmoke View Post
    Fun ) C++ standard library have "shared_from_this" mechanism
    ... since C++11, and still not universally available. So ultimately you don't even need a third-party extension to C++ if you have a suitably equipped tool chain. Prior to that of course C++ was useless and nobody ever wrote anything of value in C++

  10. #10
    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: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by fsmoke View Post
    oh my god facepalm.gif

    Simple realization of addChild
    Qt Code:
    1. TreeNode::addChild(boost::shared_ptr<TreeNode> p)
    2. {
    3. if (!p._parent.expired())
    4. throw std::runtime_error("TreeNode can't have multiple parents!");
    5. p._parent = shared_from_this(). //!!!!!!!!!
    6. _childs.push_back(p);
    7. }
    To copy to clipboard, switch view to plain text mode 
    If I want such constructions then I usually implement "explicit sharing" like approach. I have a public object that acts "like a pointer" and private object that holds the data. It is the private object that is shared and copying the public object only increases reference count of the private component. Then I don't need pointers to the public object because I can always copy it and destroying the last instance of the public object also destroys the private component.

    Qt Code:
    1. class Object {
    2. public:
    3. Object() { d = new ObjectPrivate; }
    4. Object(const Object &other) : d(other.d){ }
    5. int x() const { return d->x; }
    6. void setX(int x) { d->x = x; }
    7. Object childAt(int i) const { return d->children.at(i); }
    8. void addChild(Object o) { d->children.append(o); o.d->parent = *this; }
    9. Object& operator=(const Object &other) { d = other.d; return *this;}
    10. private:
    11. QSharedPointer<ObjectPrivate> d;
    12. };
    13.  
    14. class ObjectPrivate {
    15. public:
    16. int x;
    17. Object parent;
    18. QList<Object> children;
    19. };
    To copy to clipboard, switch view to plain text mode 

    Building a tree from such object is trivial then. The construction is more "OOP-ish" than using pointers with the exception that creating a copy of the object doesn't actually make a copy of the data (which is ok in this case).
    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.


  11. #11
    Join Date
    Nov 2012
    Posts
    15
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by wysota View Post

    Qt Code:
    1. class Object {
    2. public:
    3. Object() { d = new ObjectPrivate; }
    4. Object(const Object &other) : d(other.d){ }
    5. int x() const { return d->x; }
    6. void setX(int x) { d->x = x; }
    7. Object childAt(int i) const { return d->children.at(i); }
    8. void addChild(Object o) { d->children.append(o); o.d->parent = *this; }
    9. Object& operator=(const Object &other) { d = other.d; return *this;}
    10. private:
    11. QSharedPointer<ObjectPrivate> d;
    12. };
    13.  
    14. class ObjectPrivate {
    15. public:
    16. int x;
    17. Object parent;
    18. QList<Object> children;
    19. };
    To copy to clipboard, switch view to plain text mode 
    It's very interesting realization - it's pimpl

    But I see BUG in this code ) - it seems recursive parent destruction - you must use QWeakPointer for parent.

  12. #12
    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: when will be QSharedFromThis for QSharedPointer?

    Quote Originally Posted by fsmoke View Post
    It's very interesting realization - it's pimpl
    Yes, it's a kind of pimpl (not completely because there is no "implementation" in the private component, just data). I call this construction a "pointer without pointers".

    But I see BUG in this code ) - it seems recursive parent destruction - you must use QWeakPointer for parent.
    Could be, I didn't test the code before posting.
    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.


Similar Threads

  1. QSharedPointer with signals and slots
    By xtal256 in forum Qt Programming
    Replies: 20
    Last Post: 7th July 2012, 04:34
  2. QSharedPointer / QWeakPointer in QML?
    By centipede in forum Qt Quick
    Replies: 1
    Last Post: 23rd May 2012, 09:25
  3. emitting QSharedPointer
    By babu198649 in forum Newbie
    Replies: 11
    Last Post: 26th July 2010, 08:51
  4. Problem with QSharedPointer
    By weaver4 in forum Newbie
    Replies: 2
    Last Post: 19th April 2010, 14:22
  5. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48

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.