PDA

View Full Version : QSharedDate - possible misunderstanding



kornicameister
2nd February 2011, 16:47
I need to solve the problem with sharing some data between my classes
I thought it would be good idea to check out how QSharedData works, so I cut out the data which used to be encapsulated and shared via static object and made my class public of QSharedData

but I think I misunderstood something because real sharing is not what I received...
what I need to have are classes with mutual access to the list of columns, list of tables (both lists contains cols,tabs from db as strings) which are not modified and there are three variables which can be modified in runtime...

I read the documentation and I think that it is achievable, therefore there must be something I've done wrong

class with data looks like below


class QueryHandlerData : public QSharedData{
public:
QueryHandlerData(){
codec = QTextCodec::codecForName("Windows-1250");
//appending columns and tables here
}
QString connectionName;
QString cStatus;
int lastInsertedId; //can be redundant,QSqlDatabase has the feature which allows to obtain it anywhere
QStringList columns;
QStringList tables;
QTextCodec *codec;
};

but I saw on forum, that it should implement detach() method, is it a problem here ?
Another problem can be that I can not fully understand the meaning of Implicit and Explicit sharing :)
Does the implicit means that shared data is like-hidden, because I that's what I found in the dictionary :p

high_flyer
3rd February 2011, 09:24
but I saw on forum, that it should implement detach() method, is it a problem here ?
Another problem can be that I can not fully understand the meaning of Implicit and Explicit sharing
Does the implicit means that shared data is like-hidden, because I that's what I found in the dictionary
Did you read this part (http://doc.qt.nokia.com/4.7-snapshot/implicit-sharing.html) of the documentation?

kornicameister
4th February 2011, 10:11
Yes, I did.
So I need to implement detach method and use it every time I want to modify shared data ?

But still. will I be able to benefit from true sharing od data ?
For example

I modify variable A in point X of the program in the object 1
And than I need to access A from the object 2 in the point Y of the runtime.
Will I get what was set in the point X ?

high_flyer
4th February 2011, 13:19
I modify variable A in point X of the program in the object 1
And than I need to access A from the object 2 in the point Y of the runtime.
Will I get what was set in the point X ?
Yes.


But still. will I be able to benefit from true sharing od data ?
Well again, the benefit of implicit sharing is:

The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.
So everywhere where you need to access the data but not change it, implicit sharing is a benefit.

kornicameister
4th February 2011, 18:26
but how implicit sharing differs from static object in which my data is encapsulated ?

high_flyer
4th February 2011, 20:54
because a static object is only one object.
If you copy it, you will have to create another full copy of that object.
You can have any number of implicit shared objects, and if you change the data in one of them, only that object will be changed (copy on demand).
So implicit sharing allows you to transparently use many "copies" that are really only one, so long they "are the same", but as soon as one is changed, it is not the same as the others any more, and that is done automatically.

kornicameister
4th February 2011, 21:41
you said copying ?
but does not Q_DISABLE_COPY macro prevent from copying ?
all the data I need to have are the data used in classes derived from QObject

high_flyer
6th February 2011, 18:51
you said copying ?
Yes. As in "by value".

but does not Q_DISABLE_COPY macro prevent from copying ?
No.
This macro is used for classes that want to forbid the use of a copy constructor:
http://doc.trolltech.com/4.7/qobject.html#Q_DISABLE_COPY


all the data I need to have are the data used in classes derived from QObject
This is not relevant to the issue of implicit sharing.

wysota
6th February 2011, 19:15
I think there is a misunderstanding here. "Sharing" data is about sharing data between different instances of the data itself and not sharing data between different parts of the code. For the latter you just need a regular variable that different parts of the program have access to. So let's say you have an object representing a point (x,y) and you make a copy of that point (p2 = p1). Implicit sharing means that as long as you don't modify neither p1 nor p2 they use the same block of memory to keep the x and y values of both points. So you reduce memory usage and also reduce the time required to make a copy of the object. But if you modify either p1 or p2, the shared block is automatically duplicated and each instance (p1 and p2) gets its own copy that since then lives its own life. This is done by QSharedDataPointer.

From what I understand what you want is to be able to access the same variable from different parts of your program -- effectively you want a global variable. Since "global variables are evil", find a way (e.g. by searching the forum) to get the same semantics without using global variables. QSharedData won't help you in that in any way.

By the way, your QueryHandlerData is not derived from QObject.

kornicameister
8th February 2011, 13:50
By the way, your QueryHandlerData is not derived from QObject.

yes, but classes which holds QSharedPointer are derived from QObject.
If you pointed that out, that means there is a difference, but what exactly ?


I think there is a misunderstanding here. "Sharing" data is about sharing data between different instances of the data itself and not sharing data between different parts of the code. For the latter you just need a regular variable that different parts of the program have access to. So let's say you have an object representing a point (x,y) and you make a copy of that point (p2 = p1). Implicit sharing means that as long as you don't modify neither p1 nor p2 they use the same block of memory to keep the x and y values of both points. So you reduce memory usage and also reduce the time required to make a copy of the object. But if you modify either p1 or p2, the shared block is automatically duplicated and each instance (p1 and p2) gets its own copy that since then lives its own life. This is done by QSharedDataPointer.

From what I understand what you want is to be able to access the same variable from different parts of your program -- effectively you want a global variable. Since "global variables are evil", find a way (e.g. by searching the forum) to get the same semantics without using global variables. QSharedData won't help you in that in any way.

Now I get it. I will check the forum and try to find what you told me to.
By the way, currently I am still using static object of QueryHandlerDate (not static pointer) and my goal is achieved.
I just wrote my own copy by reference constructor and I see that copying is not taking place whenever I use or modify something in it.

wysota
8th February 2011, 20:13
yes, but classes which holds QSharedPointer are derived from QObject.
This doesn't make sense, you can't copy those. QSharedDataPointer points to a private data payload object which is not referenced from anywhere outside its public counterpart. QObjects are identity based, not value based, they are not fit for sharing.

Again, "sharing" is different than "accessing". It means an object is owned by more than one object, not that it is accessed by more than one object.

kornicameister
10th February 2011, 19:10
it couldn't be more confusing I guess...