Direct comparing of two QVariant variables
-
[quote author="Andre" date="1326703007"]How, you think, is a byte-level comparision going to help you compare two QStrings stored in that QVariant union? I mean: you do realize that the actual character array is not stored within the QVariant, right?
[/quote]Right, it is stored somewhere in memory, so rises the question of a void* pointer to this character array. You seem to be missing the whole point of what I say
-
Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it.
-
[quote author="Andre" date="1326707128"]Then, I guess we just don't understand each other. I wish you all the luck with your attempts to efficiently compare QVariants, but I don't think I can contribute to this discussion anymore. Please let us know if you find an efficient solution, I would be interested to see it. [/quote]
I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and to which it should provide this pointer (as a void*) - I talk about the latter
-
[quote author="deisik" date="1326707907"]I have already shown a clean solution which I am going to pick up myself. You probably confuse a pointer to a QVariant object as such with a pointer to data which this object wraps around and should provide as a void* - I talk about the latter[/quote]
If I understood correctly, you want to use the pointer to the memory to do the comparison, right?
How do you know, how to interpret the raw memory segment? The memory representation of a double is different from an int is different from .... So you again need the information to do correct comparison. Where is the benefit? -
[quote author="Gerolf" date="1326708249"]If I understood correctly, you want to use the pointer to the memory to do the comparison, right?[/quote]
Yes, you got it right
[quote author="Gerolf" date="1326708249"]How do you know, how to interpret the raw memory segment?[/quote]
There is no need to interpret this segment in the sense you mean by "interpreting". You just need to know its length, that's all
[quote author="Gerolf" date="1326708249"]The memory representation of a double is different from an int is different from .... So you again need the information to do correct comparison. Where is the benefit?[/quote]
I have already answered this question above - just read about how doubles and floats are accurately compared to each other
-
a double is represented by 8 bytes, mantisse and exponent. They have influence on how to read each other. If you do a byte compare, that does not work.
-
[quote author="Gerolf" date="1326709312"]a double is represented by 8 bytes, mantisse and exponent[/quote]
Yes, 64 bits
[quote author="Gerolf" date="1326709312"]They have influence on how to read each other. If you do a byte compare, that does not work.[/quote]
It will. Just use 64 bits integers. Check yourself
-
By the way, if all numbers are allowed (positive as well as negative) you can safely check the sign of the difference between the two values, so the method seems to be universal
-
If you do things like this, please leave a note for the next developer to start debugging right here if the application starts to crash somewhen.
@
void lessThan(void* left, void* right)
{
return (quint64)left < (quint64)right;
}
@
Another rule one might stick to: even if it is possible, it doesn't mean that it should be done that way. -
[quote author="Lukas Geyer" date="1326717017"]If you do things like this, please leave a note for the next developer to start debugging right here if the application starts to crash somewhen.
@
void lessThan(void* left, void* right)
{
return (quint64)left < (quint64)right;
}
@
Another rule one might stick to: even if it is possible, it doesn't mean that it should be done that way.[/quote]If the code is like this, it might crash if the value is not 64 bit long and not enough memory to read is there. Additionally, it might bring problems with little / big endianess, right?
-
As long as you stick to the OPs self-imposed limitations [limited set of datatypes, fixed size of 64 bit for every datatype, fixed floating point format, non-portable (including endianess)] this code actually does work. However, this usually does not justify the use of such code in production, as it will break on first occasion.
-
[quote author="Lukas Geyer" date="1326717017"]If you do things like this, please leave a note for the next developer to start debugging right here if the application starts to crash somewhen.
@
void lessThan(void* left, void* right)
{
return (quint64)left < (quint64)right;
}
@
Another rule one might stick to: even if it is possible, it doesn't mean that it should be done that way.[/quote]Just add the lengths of the byte arrays to the function declaration and make proper use of them within and almost all of the pitfalls you talk about will go away
You cannot take measures of safety against everything. If you try, you will inevitably fall into an endless loop
-
But if you add the length, you add information that is type specific. This means some if at call, which you wanted to remove.
-
[quote author="deisik" date="1326724760"]
You cannot take measures of safety against everything. If you try, you will inevitably fall into an endless loop[/quote]You can by avoiding a design that is broken from the very beginning.
I'm not now.
-
[quote author="Gerolf" date="1326726540"]But if you add the length, you add information that is type specific. This means some if at call, which you wanted to remove.[/quote]
For sanity check. I personally would not need it since I know that other constraints make it redundant. Period
This is what the word "integrity" stands for
-
[quote author="Volker" date="1326726676"]You can by avoiding a design that is broken from the very beginning[/quote]
Sorry, this won't help you. If it would, there would be no bugs in the software which is written with the aim "of avoiding a design that is broken from the very beginning"
-
[quote author="deisik" date="1326724760"]
Just add the lengths of the byte arrays to the function declaration and make proper use of them within and almost all of the pitfalls you talk about will go awayYou cannot take measures of safety against everything. If you try, you will inevitably fall into an endless loop[/quote]
You lose the limitation of fixed sizes, but you are still limited to some datatypes, you are still limited to a fixed floating point format and your code is non-portable by any means, because you rely on archtictural and implementational details.
What happens if the two sizes do differ? Do you start creating buffers within <code>lessThan()</code>? What if the size exceeds <code>sizeof(quint64)</code>, for example QINT64_MAX in string representation? Do you add your own comparison algorithm? How can you assure that <code>lessThan()</code> is only invoked with supported datatypes, as every datatype fits in void*? What if you compiler or FPU implementation does not support or use IEEE 754?
Of course you can't take measures of safety against everything, but you still have to assure that your code is as least error-prone as possible. One way to achieve this is to make use of C++s type safety, which is completely nullified by void pointers and C-style casts. Your code doesn't get flexible, it just gets error-prone.
It was a nice brain-twister, for all of us; and there is a use case where it actually works - but do yourself a favor and just use "correct" code. Everything else will lead to serious problems sooner or later.
@
bool lessThan(int left, int right)
{
return left < right;
}bool lessThan(double left, double right)
{
return left < right;
}bool lessThan(const char* left, const char* right)
{
// if you need further saftey or error handling use
// stringstream, boost::lexical_cast or just QStringreturn atoi(left) < atoi(right);
}
// An implementation for QVariant is found in other posts of this thread
@
This solution is just a few more lines (or, if you handle most of the limitations in your implementation, a lot less lines), absolutely type-safe, easily extensible and understandable and as least error-prone as possible. -
[quote author="Lukas Geyer" date="1326727414"]You lose the limitation of fixed sizes, but you are still limited to some datatypes, you are still limited to a fixed floating point format and your code is non-portable by any means, because you rely on archtictural and implementational details. What happens if the two sizes do differ? Do you start creating buffers within <code>lessThan()</code>? What if the size exceeds <code>sizeof(quint64)</code>, for example QINT64_MAX in string representation? Do you add your own comparison algorithm? How can you assure that <code>lessThan</code> is only invoked with supported datatypes, as every datatype fits in void? What if you compiler or FPU implementation does not support IEEE 754?[/quote]
It is for sanity sake only. As I said before there is no need for it because other limitations and conditions would exclude it to a certain degree. And at that you cannot write code that fits everyone or is suitable for every purpose or supposed to run everywhere, you have to make trade-offs - it is just a matter of understanding what is possible and what is not in the given conditions (please, note). Yes, conditions may change but they may also change in such a way that your software will come to be just a piece of garbage despite it being of a "correct design"
[quote author="Lukas Geyer" date="1326727414"]It was a nice brain-twister, for all of us; and there is a use case where it actually works - but do yourself a favor and just use "correct" code. Everything else will lead to serious problems sooner or later[/quote]
Everything potentially can lead to any kind of problems - it is just a matter of time
-
It is beyond me why anybody would add such a malicious piece of code on purpose, especially if I could add code with the same or even less effort that works in 95 out of 100 cases and is absolutely type-safe instead of code that breaks on 95 out of 100 cases and has a load of potential pitfalls and limitations attached. But I think this has been stressed enough. In the end it is your decision and your accountability, not mine.
-
[quote author="Lukas Geyer" date="1326781097"]It is beyond me why anybody would add such a malicious piece of code on purpose, especially if I could add code with the same or even less effort that works in 95 out of 100 cases and is absolutely type-safe instead of code that breaks on 95 out of 100 cases and has a load of potential pitfalls and limitations attached[/quote]
Do you actually think that type safety imposed by other means is always less strict?