sbkenum.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2016 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
  3. #ifndef SBKENUM_H
  4. #define SBKENUM_H
  5. #include "sbkpython.h"
  6. #include "shibokenmacros.h"
  7. extern "C"
  8. {
  9. LIBSHIBOKEN_API bool PyEnumMeta_Check(PyObject *ob);
  10. /// exposed for the signature module
  11. LIBSHIBOKEN_API void init_enum();
  12. struct SbkConverter;
  13. struct SbkEnumType;
  14. struct SbkEnumTypePrivate
  15. {
  16. SbkConverter *converter;
  17. SbkConverter *flagsConverter;
  18. };
  19. /// PYSIDE-1735: Pass on the Python enum/flag information.
  20. LIBSHIBOKEN_API void initEnumFlagsDict(PyTypeObject *type);
  21. /// PYSIDE-1735: Make sure that we can import the Python enum implementation.
  22. LIBSHIBOKEN_API PyTypeObject *getPyEnumMeta();
  23. /// PYSIDE-1735: Helper function supporting QEnum
  24. LIBSHIBOKEN_API int enumIsFlag(PyObject *ob_enum);
  25. }
  26. namespace Shiboken::Enum {
  27. enum : int {
  28. ENOPT_OLD_ENUM = 0x00, // PySide 6.6: no longer supported
  29. ENOPT_NEW_ENUM = 0x01,
  30. ENOPT_INHERIT_INT = 0x02,
  31. ENOPT_GLOBAL_SHORTCUT = 0x04,
  32. ENOPT_SCOPED_SHORTCUT = 0x08,
  33. ENOPT_NO_FAKESHORTCUT = 0x10,
  34. ENOPT_NO_FAKERENAMES = 0x20,
  35. ENOPT_NO_ZERODEFAULT = 0x40,
  36. ENOPT_NO_MISSING = 0x80,
  37. };
  38. LIBSHIBOKEN_API extern int enumOption;
  39. using EnumValueType = long long;
  40. LIBSHIBOKEN_API bool check(PyObject *obj);
  41. LIBSHIBOKEN_API bool checkType(PyTypeObject *pyTypeObj);
  42. LIBSHIBOKEN_API PyObject *newItem(PyTypeObject *enumType, EnumValueType itemValue,
  43. const char *itemName = nullptr);
  44. LIBSHIBOKEN_API EnumValueType getValue(PyObject *enumItem);
  45. LIBSHIBOKEN_API PyObject *getEnumItemFromValue(PyTypeObject *enumType,
  46. EnumValueType itemValue);
  47. /// Sets the enum/flag's type converter.
  48. LIBSHIBOKEN_API void setTypeConverter(PyTypeObject *type, SbkConverter *converter,
  49. SbkConverter *flagsConverter = nullptr);
  50. /// Creating Python enums for different types.
  51. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  52. const char *fullName, const char *enumItemStrings[], const int64_t enumValues[]);
  53. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  54. const char *fullName, const char *enumItemStrings[], const uint64_t enumValues[]);
  55. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  56. const char *fullName, const char *enumItemStrings[], const int32_t enumValues[]);
  57. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  58. const char *fullName, const char *enumItemStrings[], const uint32_t enumValues[]);
  59. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  60. const char *fullName, const char *enumItemStrings[], const int16_t enumValues[]);
  61. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  62. const char *fullName, const char *enumItemStrings[], const uint16_t enumValues[]);
  63. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  64. const char *fullName, const char *enumItemStrings[], const int8_t enumValues[]);
  65. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(PyObject *module,
  66. const char *fullName, const char *enumItemStrings[], const uint8_t enumValues[]);
  67. /// This template removes duplication by inlining necessary type casts.
  68. template <typename IntT>
  69. inline PyTypeObject *createPythonEnum(PyTypeObject *scope,
  70. const char *fullName, const char *enumItemStrings[], const IntT enumValues[])
  71. {
  72. auto *obScope = reinterpret_cast<PyObject *>(scope);
  73. return createPythonEnum(obScope, fullName, enumItemStrings, enumValues);
  74. }
  75. /**
  76. * @brief Creates a Python enum type from a set of provided key/values pairs
  77. *
  78. * @param fullName The full name (including module and package depth) to be used for the newly
  79. * created enum type.
  80. * @param pyEnumItems The key/value pairs to be used for the enum.
  81. * @param enumTypeName The name of the enum type to be used (i.e., "PyIntEnum", "PyFlag", etc)
  82. * from Python's enum module.
  83. * @param callDict The dictionary to be used for the call, allowing for additional keyword
  84. * arguments to be passed, such as "boundary=KEEP".
  85. */
  86. LIBSHIBOKEN_API PyTypeObject *createPythonEnum(const char *fullName,
  87. PyObject *pyEnumItems,
  88. const char *enumTypeName = "Enum",
  89. PyObject *callDict = nullptr);
  90. } // namespace Shiboken::Enum
  91. #endif // SKB_PYENUM_H