Issue with QLatin1String and nested QHash implementations.
-
Hi,
- Why can't you create that container ?
@QHash<KeyType, QHash<KeyType, ValueType> >@
is valid.
AFAIK, QLatin1String use cases doesn't fit your need.
-
It can't create a default key because QLatin1String doesn't provide a default constructor (nor should it)
-
Same as 2
-
For #1, I tried it before and got a intellisense error (in Visual Studio). So I looked it up online and I took the advise I found there. I suppose it is possible but perhaps Visual Studio doesn't like it, or at least it didn't like the key/QHash combo I was using. I will have to figure that part out. I actually do like inheriting from QHash though because it allows me to do some other necessary bookkeeping tasks in a less convoluted way.
For your answers to 2 and 3: You said (I am not able to get quote to work for this quote for some reason...)
"It can’t create a default key because QLatin1String doesn’t provide a default constructor (nor should it)"
If that is the case, why did it work fine when I wasn't using a nested class? Why is a default key needed in one case but not the other. In my "earlier post":http://qt-project.org/forums/viewthread/49071/ I explained that it worked fine before. I was able to use QLatin1String as a key. Why?
Also... Why would QLatin1STring not fit my use case (other than a lack of a default constructor)? I can't find a qHash override method (for finding a hash code) in the list provided that handles multiiple character input for char* (a single byte character string) other than QByteArray& which would require conversion and create other issues. So is that my only choice? What else could I use? (I know I can use QString but I would prefer a single byte character). I am willing to use another key type if it fits my needs (which is to base the hash code off a string of single byte wide characters).
-
For 1 did you wrote the > > with the space between ?
When you said it was working, were you actively using that class or was it just declared ?
Because QLatin1String is meant to be used for "String Literals":en.wikipedia.org/wiki/String_literal and a hash key is not a string literal.
QString sounds like what you need, if you think not can you explain what exactly will get put in that key ?
-
I don't remember if I used a space or not.
I did not add the nested class until after the reinstall (a week or so ago). So before I did not use a nested class at all. But I did have several QHash<QLatin1String, someValueClass> instances (such as the one I put in my first post in the other thread). All of them worked fine.
I know what a hash key is. I have built my own hashing methods before. My point is that a hash method exists for QLatin1String so why has this been done if QLatin1String is a bad choice? From the QHash 5.3 documentation:
@uint qHash(QLatin1String key, uint seed = 0)@
Seeing the above hash method is why I chose QLatin1String in the first place. I would really prefer NOT to use QString for one reason. The key string is always based on an object name defined in xml. Even when other languages are used in the future, the actual names of objects will only need the standard ascii set (one byte wide characters). So I would prefer a hash key that is calculated using a string of single byte wide characters.
-
Because you can create a QHash in code using string literals as key which is fine AFAIK but your use case doesn't involve string literals you are creating these string dynamically so you would have to also ensure that the original data doesn't get deleted since QLatin1String doesn't make any copy and requires that the original data lives as long as the QLatin1String object itself.
What issues would you have with QByteArray ?
As for the nesting problem: the type used as value must have a default constructor (more about "here":http://qt-project.org/doc/qt-5/containers.html). If you try to get a value using a key not found in the hash you'll get a default constructed value which is not possible using QLatin1String as key
-
Here is my problem with QByteArray. From the documentation:
[quote]QByteArray makes a deep copy of the string data.[/quote]
I need these info classes because my application is module based and each module is potentially vastly different. The only thing standardized about them is how they define their components (by way of the info files) and how those components are organized.
However, each module will be able to make use of its own infos using string literals (because a module designer will know the names of the infos they use). In addition, any info that gets converted into an object managed by the object manager must have a char* that can be used without duplication because I have to manage two different string types from two different libraries (Qt and Irrlicht) and Irrlicht does not have a wrapper class like Qt does. I was relying on QLatin1String because that allowed me to wrap irrlicht's pointers to their object names to use as a key for lookup without duplicating the string.
I don't want to waste processor time by duplicating a string every time I need to look up an object from code. Instead, I would like to directly control if and when a string gets duplicated (so that it happens only when necessary).
[quote author="SGaist" date="1414744311"]If you try to get a value using a key not found in the hash you'll get a default constructed value which is not possible using QLatin1String as key [/quote]
That makes sense. Is their a way I could somehow supply this default value?
-
Then fromRawData looks like what you need, no ?
-
Yeah! Thanks. That WILL work. I don't always remember to look for static "constructors."
-
On second thought, No it won't. I was in a hurry before and didn't pay close attention. The problem is that it still requires unnecessary processing in order to calculate the size of the array a priori so that the QByteArray knows how long the array is (since a string isn't guaranteed). A normal hash code calculator for a string doesn't need to know how long the string is because it just keeps operating on the hash code until it encounters the null terminator.
I am beginning to think I will just have to create my own hash table class and use the qHash(QLatin1String) method for calculating the hash value.
-
Do I have any other options?