helper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 HELPER_H
  4. #define HELPER_H
  5. #include "sbkpython.h"
  6. #include "shibokenmacros.h"
  7. #include "autodecref.h"
  8. #include <iosfwd>
  9. #define SBK_UNUSED(x) (void)(x);
  10. namespace Shiboken
  11. {
  12. /**
  13. * It transforms a python sequence into two C variables, argc and argv.
  14. * This function tries to find the application (script) name and put it into argv[0], if
  15. * the application name can't be guessed, defaultAppName will be used.
  16. *
  17. * No memory is allocated is an error occur.
  18. *
  19. * \note argc must be a valid address.
  20. * \note The argv array is allocated using new operator and each item is allocated using malloc.
  21. * \returns True on sucess, false otherwise.
  22. */
  23. LIBSHIBOKEN_API bool listToArgcArgv(PyObject *argList, int *argc, char ***argv, const char *defaultAppName = nullptr);
  24. /// Delete a a list of arguments created by listToArgcArgv()
  25. LIBSHIBOKEN_API void deleteArgv(int argc, char **argv);
  26. /**
  27. * Convert a python sequence into a heap-allocated array of ints.
  28. *
  29. * \returns The newly allocated array or NULL in case of error or empty sequence. Check with PyErr_Occurred
  30. * if it was successfull.
  31. */
  32. LIBSHIBOKEN_API int *sequenceToIntArray(PyObject *obj, bool zeroTerminated = false);
  33. /// Fix a type name returned by typeid(t).name(), depending on compiler.
  34. /// \returns Fixed name (allocated).
  35. LIBSHIBOKEN_API const char *typeNameOf(const char *typeIdName);
  36. /// Returns whether \a method is a compiled method (Nuitka).
  37. LIBSHIBOKEN_API bool isCompiledMethod(PyObject *method);
  38. /**
  39. * Creates and automatically deallocates C++ arrays.
  40. */
  41. template<class T>
  42. class ArrayPointer
  43. {
  44. public:
  45. ArrayPointer(const ArrayPointer &) = delete;
  46. ArrayPointer(ArrayPointer &&) = delete;
  47. ArrayPointer &operator=(const ArrayPointer &) = delete;
  48. ArrayPointer &operator=(ArrayPointer &&) = delete;
  49. explicit ArrayPointer(Py_ssize_t size) : data(new T[size]) {}
  50. T &operator[](Py_ssize_t pos) { return data[pos]; }
  51. operator T *() const { return data; }
  52. ~ArrayPointer() { delete[] data; }
  53. private:
  54. T *data;
  55. };
  56. template <class T>
  57. using AutoArrayPointer = ArrayPointer<T>; // deprecated
  58. using ThreadId = unsigned long long;
  59. LIBSHIBOKEN_API ThreadId currentThreadId();
  60. LIBSHIBOKEN_API ThreadId mainThreadId();
  61. LIBSHIBOKEN_API int pyVerbose();
  62. /**
  63. * An utility function used to call PyErr_WarnEx with a formatted message.
  64. */
  65. LIBSHIBOKEN_API int warning(PyObject *category, int stacklevel, const char *format, ...);
  66. struct LIBSHIBOKEN_API debugPyObject
  67. {
  68. explicit debugPyObject(PyObject *o);
  69. PyObject *m_object;
  70. };
  71. struct LIBSHIBOKEN_API debugSbkObject
  72. {
  73. explicit debugSbkObject(SbkObject *o);
  74. SbkObject *m_object;
  75. };
  76. struct LIBSHIBOKEN_API debugPyTypeObject
  77. {
  78. explicit debugPyTypeObject(PyTypeObject *o);
  79. PyTypeObject *m_object;
  80. };
  81. struct debugPyBuffer;
  82. struct debugPyArrayObject
  83. {
  84. explicit debugPyArrayObject(PyObject *object) : m_object(object) {}
  85. PyObject *m_object;
  86. };
  87. LIBSHIBOKEN_API std::ostream &operator<<(std::ostream &str, const debugPyObject &o);
  88. LIBSHIBOKEN_API std::ostream &operator<<(std::ostream &str, const debugSbkObject &o);
  89. LIBSHIBOKEN_API std::ostream &operator<<(std::ostream &str, const debugPyTypeObject &o);
  90. LIBSHIBOKEN_API std::ostream &operator<<(std::ostream &str, const debugPyBuffer &b);
  91. LIBSHIBOKEN_API std::ostream &operator<<(std::ostream &str, const debugPyArrayObject &b);
  92. LIBSHIBOKEN_API std::ios_base &debugVerbose(std::ios_base &s);
  93. LIBSHIBOKEN_API std::ios_base &debugBrief(std::ios_base &s);
  94. } // namespace Shiboken
  95. #endif // HELPER_H