RunnableThread.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "RunnableThread.h"
  2. #include <QDebug>
  3. class CustomRunnablePrivate
  4. {
  5. Q_DISABLE_COPY(CustomRunnablePrivate)
  6. public:
  7. explicit CustomRunnablePrivate(CustomRunnable* parent);
  8. ~CustomRunnablePrivate();
  9. void (*m_Callback)(void *);
  10. void *m_Parameter;
  11. private:
  12. CustomRunnable* m_Parent;
  13. };
  14. CustomRunnable::CustomRunnable()
  15. : QRunnable()
  16. , m_Private(new CustomRunnablePrivate(this))
  17. {
  18. setAutoDelete(true);
  19. }
  20. CustomRunnable::~CustomRunnable()
  21. {
  22. }
  23. void CustomRunnable::setCallbackFunction(void (*callback)(void *), void *parameter)
  24. {
  25. m_Private->m_Callback = callback;
  26. m_Private->m_Parameter = parameter;
  27. }
  28. void CustomRunnable::run()
  29. {
  30. if (NULL != m_Private->m_Callback) {
  31. m_Private->m_Callback(m_Private->m_Parameter);
  32. }
  33. }
  34. CustomRunnablePrivate::CustomRunnablePrivate(CustomRunnable *parent)
  35. : m_Parent(parent)
  36. {
  37. m_Callback = NULL;
  38. m_Parameter = NULL;
  39. }
  40. CustomRunnablePrivate::~CustomRunnablePrivate()
  41. {
  42. }
  43. class CustomThreadPrivate
  44. {
  45. Q_DISABLE_COPY(CustomThreadPrivate)
  46. public:
  47. explicit CustomThreadPrivate(CustomThread* parent);
  48. ~CustomThreadPrivate();
  49. void (*m_Callback)(void *);
  50. void *m_Parameter;
  51. bool m_EventLoop;
  52. private:
  53. CustomThread* m_Parent;
  54. };
  55. CustomThread::CustomThread(QObject *parent)
  56. : QThread(parent)
  57. , m_Private(new CustomThreadPrivate(this))
  58. {
  59. }
  60. CustomThread::~CustomThread()
  61. {
  62. }
  63. void CustomThread::setCallbackFunction(void (*callback)(void *), void *parameter)
  64. {
  65. m_Private->m_Callback = callback;
  66. m_Private->m_Parameter = parameter;
  67. }
  68. void CustomThread::setEventLoop(const bool exec)
  69. {
  70. m_Private->m_EventLoop = exec;
  71. }
  72. void CustomThread::setusleep(const unsigned long value)
  73. {
  74. usleep(value);
  75. }
  76. void CustomThread::callbackFunctionSlots()
  77. {
  78. if (NULL != m_Private->m_Callback) {
  79. m_Private->m_Callback(m_Private->m_Parameter);
  80. }
  81. }
  82. void CustomThread::run()
  83. {
  84. if (m_Private->m_EventLoop) {
  85. exec();
  86. } else {
  87. if (NULL != m_Private->m_Callback) {
  88. m_Private->m_Callback(m_Private->m_Parameter);
  89. }
  90. }
  91. }
  92. CustomThreadPrivate::CustomThreadPrivate(CustomThread* parent)
  93. : m_Parent(parent)
  94. {
  95. m_Callback = NULL;
  96. m_Parameter = NULL;
  97. m_EventLoop = false;
  98. }
  99. CustomThreadPrivate::~CustomThreadPrivate()
  100. {
  101. }