char parser_name[] = "parser";The problem with this code is that the programmer must insure that the correct names are placed in the array in the correct order matching the enumeration values, otherwise the wrong name will be used. This is also basically why the Code and TokenStatus enumerations are automatically generated - to eliminated possible coding errors. Using QMap was a way to eliminate this possibility. This was the resulting code (the map is still indexed by the enumeration value) and the name map was used to access the names:
char expression_name[] = "expression";
char translator_name[] = "translator";
char *name[] = {
parser_name, expression_name, translator_name
};
QMap<testModeEnum, QString> name;However, the enumeration declaration had to moved outside the function or it would not compile (apparently, local enumerations can be used). In later considering this code, a better method would be to use a pre-sized QVector since a QMap has more overhead that is really not needed here and the enumeration values are in order. The code was changed to the nearly identical:
name[testParser] = "parser";
name[testExpression] = "expression";
name[testTranslator] = "translator";
QVector<QString> name(sizeofTestMode);Where the sizeofTestMode value was added to the end of the enumeration (which was moved back into the function) so that the vector could be allocated ahead of time. Using QMap would be needed if the indexes being associated were not in numerical order.
name[testParser] = "parser";
name[testExpression] = "expression";
name[testTranslator] = "translator";
[commit ad08201092]
No comments:
Post a Comment
All comments and feedback welcomed, whether positive or negative.
(Anonymous comments are allowed, but comments with URL links or unrelated comments will be removed.)