| 262 | |
| 263 | template <typename T> |
| 264 | struct BitCombinedEnumWrapper { |
| 265 | PyEnumHead std::string to_string() const { |
| 266 | uint32_t value_int = static_cast<uint32_t>(value); |
| 267 | if (value_int == 0) { |
| 268 | return "None"; |
| 269 | } else { |
| 270 | std::string ret; |
| 271 | bool first = true; |
| 272 | for (uint32_t i = 0; i < 32; i++) { |
| 273 | if (value_int >> i & 1) { |
| 274 | if (!first) { |
| 275 | ret += " + "; |
| 276 | } else { |
| 277 | first = false; |
| 278 | } |
| 279 | ret += (std::string(name) + "." + members[i]); |
| 280 | } |
| 281 | } |
| 282 | return ret; |
| 283 | } |
| 284 | } |
| 285 | static PyObject* py_new_combined_enum( |
| 286 | PyTypeObject* type, PyObject* args, PyObject*) { |
| 287 | if (!PyTuple_Size(args)) { |
| 288 | PyObject* obj = type->tp_alloc(type, 0); |
| 289 | reinterpret_cast<BitCombinedEnumWrapper*>(obj)->value = T(); |
| 290 | return obj; |
| 291 | } else { |
| 292 | PyObject* input; |
| 293 | if (!PyArg_ParseTuple(args, "|O", &input)) { |
| 294 | return nullptr; |
| 295 | } |
| 296 | T value; |
| 297 | if (load(input, value)) { |
| 298 | return cast(value); |
| 299 | } else { |
| 300 | PyErr_SetString( |
| 301 | PyExc_RuntimeError, |
| 302 | mgb::ssprintf( |
| 303 | "Cannot convert type %s to type %s\n", |
| 304 | input->ob_type->tp_name, name) |
| 305 | .c_str()); |
| 306 | return nullptr; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | static PyObject* py_repr(PyObject* self) { |
| 311 | return py::cast(reinterpret_cast<BitCombinedEnumWrapper*>(self)->to_string()) |
| 312 | .release() |