[pygccxml-development] Order of enum values
Brought to you by:
mbaas,
roman_yakovenko
From: Matthias B. <ba...@ir...> - 2006-07-06 14:02:57
|
Hi, as I still have the problem that the order of enum values changes between two identical runs of pyplusplus (when the cache is used) I had a closer look at why this is the case. The problem is that the values of an enum are stored inside a dict and the order of those values may change when the dict is pickled and unpickled (usually this is ok as there is no order defined on the values of a dict). Now I'm not sure what the best way to fix this is. The values in enumeration_t are stored as a dict: name(str) -> value(int). For example, the enum enum MColorType { kRGB, kHSV, kCMY, kCMYK }; is stored as the following dict (in an arbitrary order): {u'kCMY': u'2', u'kHSV': u'1', u'kRGB': u'0', u'kCMYK': u'3'} Now I have several questions: - A comment in the source code says that the value part is stored as an int but obviously it's a unicode string containing an int. Is this a bug? Could the value actually also be something else than an int? - Are those integer values ever used? The generated code looks like this: bp::enum_<MColor::MColorType>("MColorType") .value("kCMY", MColor::kCMY) .value("kHSV", MColor::kHSV) .value("kRGB", MColor::kRGB) .value("kCMYK", MColor::kCMYK) .export_values() ; Now what I'd like to have is a scheme that always produces the same order. There are several options how this could be implemented: - Store the values inside a list instead of a dict. The items can be either just the enum names or tuples (name, value) if the value is still required somewhen. - The code creator could sort the enum names according to their value and then use this order to write them out. To me the first option seems to be the right one as an enum actually *is* an ordered construct. But I don't know the entire pyplusplus code, so maybe there are cases where a value has to be looked up quickly which justifies using a dict. So, Roman, which approach would you prefer? (I might have a go at fixing it myself then) - Matthias - |