Direct comparing of two QVariant variables
-
[quote author="Volker" date="1326642057"]There cannot be a clean solution for an in principle unsolvable problem. QVariant can hold types which are plain not comparable. For int an QStringList one can imagine something fancy, but comparing a QVariantMap against a QImage is ... weird[/quote]
If QVariant knows (and it does) about the type of the value it stores, why can't it return this value with QVariant::value() without type specification just as it returns with value<int>(), value<float>(), etc ad nauseum?
[quote author="Volker" date="1326642057"]For using a template based solution, something like this comes into mind[/quote]
This is not a real solution. As said before, it's better to make type casting directly in the if statement for comparison. In both cases you have to know the right type to which to convert at compile-time or to cover all possible (allowed) types. This is a real nuisance and in tight loops just a waste of cycles
-
[quote author="deisik" date="1326646561"]If QVariant knows (and it does) about the type of the value it stores, why can't it return this value with QVariant::value() without type specification just as it returns with value<int>(), value<float>(), etc ad nauseum?[/quote]
It can't do that, because C++ won't let you. You cannot overload a method by only changing its return type. The only way to do it, is the way it has been done: as a templated method. How on earth is the compiler supposed to know what the return type of QVariant::value() is?[quote author="deisik" date="1326646561"]This is not a real solution. As said before, it's better to make type casting directly in the if statement for comparison. In both cases you have to know the right type to which to convert at compile-time or to cover all possible (allowed) types. This is a real nuisance and in tight loops just a waste of cycles[/quote]
I am wondering if QVariant really is the type for you. If you know you are storing a certain type, why not use that type directly then? QVariant has its uses, but in the end, it still is little more than a glorified void* after all.
-
[quote author="Andre" date="1326656285"]It can't do that, because C++ won't let you. You cannot overload a method by only changing its return type. The only way to do it, is the way it has been done: as a templated method. How on earth is the compiler supposed to know what the return type of QVariant::value() is?[/quote]
Why should the compiler know anything if the return type can just be a void* pointer of that very QVariant::value() without explicit type cast (or call it valueVoid() for that matter)? I don't mean overloading and again, I see no problem here - qt knows the right type, so does the programmer
[quote author="Andre" date="1326656285"]I am wondering if QVariant really is the type for you. If you know you are storing a certain type, why not use that type directly then? QVariant has its uses, but in the end, it still is little more than a glorified void* after all. [/quote]
I have the same thoughts but I don't like the idea of doing some very low level (read nasty) things in qt/c++. Look, in c/c++ you can easily reinterpret some portion of memory as you like on the fly and you cheerfully hope that the exact way you do this can just as easy be determined by some other bytes in memory you point to, but nope, you can't do this
-
[quote author="deisik" date="1326660493"]
Why should the compiler know anything if the return type can just be a void* pointer of that very QVariant::value() (or call it valueVoid() for thet matter) without explicit type cast? Again, I see no problem here - qt knows the right type, so does the programmer
[/quote]The signature of method value is
@
template <typename T>
T QVariant::value();
@T is a type and must be know at compile time. The pure reason for the existence of QVariant is that its type is not know at compile time, but only during runtime. So we have an unresolvable problem here. This is a limitation of C++, as Andre already stated, so Qt cannot be of any help here.
-
[quote author="deisik" date="1326660493"]Why should the compiler know anything if the return type can just be a void* pointer of that very QVariant::value() without explicit type cast (or call it valueVoid() for that matter)? I don't mean overloading and again, I see no problem here - qt knows the right type, so does the programmer[/quote]
But that would not help you solve your problem, I think? I mean, comparing two void pointers would compare their addresses, not their contents. So, that would force to to start casting again, and then you're back to where you started. -
[quote author="Andre" date="1326661453"]But that would not help you solve your problem, I think? I mean, comparing two void pointers would compare their addresses, not their contents. So, that would force to to start casting again, and then you're back to where you started. [/quote]
Yes, there is a problem here but it deserves and requires thorough thinking over. At least we are no longer bound to QVariant without losing its advantages
-
[quote author="deisik" date="1326661825"]
Yes, there is a problem here but it deserves and requires thorough thinking over. At least we are no longer bound to QVariant without losing its advantages[/quote]When passing a void pointer to another method (operators are methods too) you even loos the information that it actually is a QVariant. We're all looking forward to see your implementation outline of a lessThan implementation taking to void pointers, though. I'm pretty sure neither Andre, nor me or anyone else we both know a decent solution, be we're all eager to learn something new (as everyone doing software development should do).
-
[quote author="deisik" date="1326662003"][quote author="Volker" date="1326661563"]And what do you want to do with the void pointer? That is as opaque as the QVariant itself...[/quote]
I can ask you another question, how QVariant itself stores its value data type?
[/quote]
http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358
-
[quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]
This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question
-
[quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]
I expected to see something like that
-
[quote author="deisik" date="1326662787"][quote author="Andre" date="1326662334"]A void* caries less information than QVariant. I don't see how that would bring you any closer to a solution. [/quote]
This information about data type is known beside QVariant (read saved somewhere else) - this is the whole point of my question. How to use it for comparison and whether it is possible in c++ at all is another question[/quote]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.
[quote author="deisik" date="1326662903"][quote author="Volker" date="1326662484"]http://qt.gitorious.org/qt/qt/blobs/4.8/src/corelib/kernel/qvariant.h#line358[/quote]
I expected to see something like that
[/quote]
Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?
-
[quote author="Volker" date="1326663446"]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours. Our opinion is known (void pointers make matters worse). The other constraints, set by C++, have already been pointed out.[/quote]Ok, I will make a try
[quote author="Volker" date="1326663446"]Reading source code is the best way of understanding things. What else would you have hoped to see instead of your expectations?[/quote]
To hope for something out of nothing is the most popular form of hope. Black magic indeed
-
[quote author="Volker" date="1326663446"]
You put void pointers into the game, so it's your turn to show how it eases solving the problem, not ours[/quote]My try as promised. I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known. This means that we know beforehand the length the byte array representing this or that value takes in memory. So the algorithm for comparing two values is to regard them as, say, 64 bits integers, filling the higher missing bits of the shorter values (copy of) with zeroes (to clean up garbage)
This technique should work even for real numbers. In fact, interpreting the bit-pattern of a floating-point number as integer is one of the methods for comparing floats and doubles (as a side note, you cannot correctly compare two floats by comparing them "out of the box" as you suggested in the code snippet, but I think you know this yourself)
There still remains a minor problem with negative numbers but as it happens I am not much interested in them (in my case there are none), furthermore I guess this can be fixed somehow
Now you give me a void pointer to a QVariant value and I show you the code
-
[quote]I am interested in comparing ints, floats and strings and as I said earlier data type for each value is known.[/quote]
Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)
-
[quote author="peppe" date="1326672292"]Do you mean that the variants you want to compare are only of these three types? An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]
There can be no comparison between defferent types (imagine a table where you compare only between values of the same column). And yes, columns of this imaginary table are almost all kinds of unsigned base types but they are all interpreted as quint64 integer for saving in a binary file (well, this is a waste of space but it saves time when retrieving them and when we come to file sizes over a few GBs it makes no difference - you just save the value in place of otherwise a 64 bit pointer)
[quote author="peppe" date="1326672292"]An arbitrary string or a string representing a number as well? (In the former case, how do you compare it f.i. against a number?)[/quote]
Yes, arbitrary text strings. In case there is a "number" inside it is valid to consider it as text. So "75" would actually be 55 53 as a byte array
There are also QDateTimes but they are converted to uints with QDateTime::toTime_t(). In fact, all these types pass in QVariant as raw bytes (by QByteArray) and converted to real int, floats, strings and dates only for representation through QAbstractItemModel and building indexes (where comparison comes into play). It is a matter of interpretation