| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- #include "RunnableThread.h"
- #include <QDebug>
- class CustomRunnablePrivate
- {
- Q_DISABLE_COPY(CustomRunnablePrivate)
- public:
- explicit CustomRunnablePrivate(CustomRunnable* parent);
- ~CustomRunnablePrivate();
- void (*m_Callback)(void *);
- void *m_Parameter;
- private:
- CustomRunnable* m_Parent;
- };
- CustomRunnable::CustomRunnable()
- : QRunnable()
- , m_Private(new CustomRunnablePrivate(this))
- {
- setAutoDelete(true);
- }
- CustomRunnable::~CustomRunnable()
- {
- }
- void CustomRunnable::setCallbackFunction(void (*callback)(void *), void *parameter)
- {
- m_Private->m_Callback = callback;
- m_Private->m_Parameter = parameter;
- }
- void CustomRunnable::run()
- {
- if (NULL != m_Private->m_Callback) {
- m_Private->m_Callback(m_Private->m_Parameter);
- }
- }
- CustomRunnablePrivate::CustomRunnablePrivate(CustomRunnable *parent)
- : m_Parent(parent)
- {
- m_Callback = NULL;
- m_Parameter = NULL;
- }
- CustomRunnablePrivate::~CustomRunnablePrivate()
- {
- }
- class CustomThreadPrivate
- {
- Q_DISABLE_COPY(CustomThreadPrivate)
- public:
- explicit CustomThreadPrivate(CustomThread* parent);
- ~CustomThreadPrivate();
- void (*m_Callback)(void *);
- void *m_Parameter;
- bool m_EventLoop;
- private:
- CustomThread* m_Parent;
- };
- CustomThread::CustomThread(QObject *parent)
- : QThread(parent)
- , m_Private(new CustomThreadPrivate(this))
- {
- }
- CustomThread::~CustomThread()
- {
- }
- void CustomThread::setCallbackFunction(void (*callback)(void *), void *parameter)
- {
- m_Private->m_Callback = callback;
- m_Private->m_Parameter = parameter;
- }
- void CustomThread::setEventLoop(const bool exec)
- {
- m_Private->m_EventLoop = exec;
- }
- void CustomThread::setusleep(const unsigned long value)
- {
- usleep(value);
- }
- void CustomThread::callbackFunctionSlots()
- {
- if (NULL != m_Private->m_Callback) {
- m_Private->m_Callback(m_Private->m_Parameter);
- }
- }
- void CustomThread::run()
- {
- if (m_Private->m_EventLoop) {
- exec();
- } else {
- if (NULL != m_Private->m_Callback) {
- m_Private->m_Callback(m_Private->m_Parameter);
- }
- }
- }
- CustomThreadPrivate::CustomThreadPrivate(CustomThread* parent)
- : m_Parent(parent)
- {
- m_Callback = NULL;
- m_Parameter = NULL;
- m_EventLoop = false;
- }
- CustomThreadPrivate::~CustomThreadPrivate()
- {
- }
|