Results 1 to 4 of 4

Thread: QSharedPointer: How to get information about who is sharing the pointer?

  1. #1
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default QSharedPointer: How to get information about who is sharing the pointer?

    Hi

    I've been looking into the different kind of pointers available inside Qt in some detail over the last few weeks.

    It looks like QSharedPointer will fit my needs the best. To give a rough overview what I need:

    I have an observer class which observes subjects attached to it. I want to be able to see who takes part in the relationship from both sides.

    That is, from the observer's side I need to know which subjects are attached to it (I created a pointerlist for this which removes subjects as soon as they are destroyed), as well as who is pointing to the observer (For this I want to use QSharedPointer).

    From a subject's side I need to know which observers are observing it (I will also use the pointerlist for this), as well as who has references to it (Again, QSharedPointer is my plan).

    From what I found in the Qt docs, it seems like QSharedPointer is the best way to go. The only problem is that it does not seems to give any information about who is sharing the pointer, or what the reference count is.

    Is there an easier way to do this, or is the alternative to rewrite my own QSharedPointer / subclassing QSharedPointer to create my own.

    Thanks for your insights,
    Regards,
    Jaco

  2. #2
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSharedPointer: How to get information about who is sharing the pointer?

    Quote Originally Posted by JPNaude View Post
    The only problem is that it does not seems to give any information about who is sharing the pointer, or what the reference count is.
    You are right, it doesn't provide internal reference counting. But you can use QSharedData. You have to use this class with QSharedPointer to achieve what you want.

  3. The following user says thank you to yogeshgokul for this useful post:

    JPNaude (7th August 2009)

  4. #3
    Join Date
    Aug 2008
    Posts
    132
    Thanks
    23
    Thanked 3 Times in 3 Posts

    Default Re: QSharedPointer: How to get information about who is sharing the pointer?

    Aha, thanks for the pointer. It looks like the combination will do the trick. After playing around with your idea, this is what I came up with.

    Qt Code:
    1. class ObserverData : public QSharedData
    2. {
    3. public:
    4. ObserverData() : subject_limit(-1), id_counter(0), subject_type("Subject") {}
    5. ObserverData(const ObserverData &other) : QSharedData(other) {}
    6.  
    7. PointerList<QObject> subject_list;
    8. int subject_limit;
    9. int id_counter;
    10. QString subject_type;
    11. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class AbstractObserver
    2. {
    3. public:
    4. AbstractObserver() { d = new ObserverData; }
    5. AbstractObserver(const AbstractObserver &other) : d (other.d) {}
    6. virtual ~AbstractObserver() {}
    7.  
    8. void detach() { d.detach(); }
    9.  
    10. virtual void attachSubject() {}
    11. virtual void detachSubject() {}
    12.  
    13. private:
    14. QExplicitlySharedDataPointer<ObserverData> d;
    15. };
    To copy to clipboard, switch view to plain text mode 

    The above implementation will work very well for my needs. Thus, if an Observer wants to share the data with another observer it would just copy it. But when it wants to copy it and modify the data on its own it would call the detach() function.

    I've been looking at the Employee example in the Qt Docs in detail which shows how to use SharedData along with QSharedDataPointer. However I have some doubts when the example is modified as above to use QExplicitlySharedDataPointer.

    My doubt is related to the copy constructor. When using QExplicitlySharedDataPointer, is my copy constructor in the ObserverData class correct in the above code? I guess it is not necessary to initialize the internal variables since the data is shared explicitly. Or is it still necessary?

    My compilation runs through fine, but I would like to understand it 100% before moving on.

    Thanks for your time again,
    Jaco
    Last edited by JPNaude; 7th August 2009 at 12:54.

  5. #4
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSharedPointer: How to get information about who is sharing the pointer?

    Quote Originally Posted by JPNaude View Post
    My doubt is related to the copy constructor. When using QExplicitlySharedDataPointer, is my copy constructor in the ObserverData class correct in the above code? I guess it is not necessary to initialize the internal variables since the data is shared explicitly. Or is it still necessary?
    For better practice do it.

Similar Threads

  1. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13

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.