PDA

View Full Version : QSharedPointer: How to get information about who is sharing the pointer?



JPNaude
5th August 2009, 13:58
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

yogeshgokul
5th August 2009, 14:13
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.

JPNaude
7th August 2009, 13:42
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.



class ObserverData : public QSharedData
{
public:
ObserverData() : subject_limit(-1), id_counter(0), subject_type("Subject") {}
ObserverData(const ObserverData &other) : QSharedData(other) {}

PointerList<QObject> subject_list;
int subject_limit;
int id_counter;
QString subject_type;
};




class AbstractObserver
{
public:
AbstractObserver() { d = new ObserverData; }
AbstractObserver(const AbstractObserver &other) : d (other.d) {}
virtual ~AbstractObserver() {}

void detach() { d.detach(); }

virtual void attachSubject() {}
virtual void detachSubject() {}

private:
QExplicitlySharedDataPointer<ObserverData> d;
};


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

yogeshgokul
7th August 2009, 14:05
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. ;)