Mercurial > dwindows
diff dw.hpp @ 2933:3cdb02171b01
C++: Implement Thread class and add the last page Thread/Event.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 31 Dec 2022 00:41:28 +0000 |
parents | 3f660f47a45f |
children | 75f6e21f5a02 |
line wrap: on
line diff
--- a/dw.hpp Fri Dec 30 22:53:19 2022 +0000 +++ b/dw.hpp Sat Dec 31 00:41:28 2022 +0000 @@ -37,6 +37,8 @@ namespace DW { +#define THREAD_STACK 10000 + // Forward declare these so they can be referenced class Render; class Pixmap; @@ -1972,6 +1974,91 @@ } }; +class Thread : public Handle +{ +private: + DWTID tid; +#ifdef DW_LAMBDA + std::function<void(Thread *)> _ConnectThread; +#endif + void (*_ConnectThreadOld)(Thread *); + static void _OnThread(void *data) { + Thread *classptr = reinterpret_cast<Thread *>(data); + +#ifdef DW_LAMBDA + if(classptr->_ConnectThread) + classptr->_ConnectThread(classptr); + else +#endif + if(classptr->_ConnectThreadOld) + classptr->_ConnectThreadOld(classptr); + else + classptr->OnThread(classptr); + + delete classptr; + } +public: + // Constructors +#ifdef DW_LAMBDA + Thread(std::function<void(Thread *)> userfunc) { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThread = userfunc; + _ConnectThreadOld = 0; + } + Thread(std::function<void(Thread *)> userfunc, int stack) { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThread = userfunc; + _ConnectThreadOld = 0; + } +#endif + Thread(void (*userfunc)(Thread *)) { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThreadOld = userfunc; +#ifdef DW_LAMBDA + _ConnectThread = 0; +#endif + } + Thread(void (*userfunc)(Thread *), int stack) { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThreadOld = userfunc; +#ifdef DW_LAMBDA + _ConnectThread = 0; +#endif + } + Thread(int stack) { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThreadOld = 0; +#ifdef DW_LAMBDA + _ConnectThread = 0; +#endif + } + Thread() { + tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK); + SetHandle(reinterpret_cast<void *>(tid)); + _ConnectThreadOld = 0; +#ifdef DW_LAMBDA + _ConnectThread = 0; +#endif + } + // Destructor + virtual ~Thread() { if(tid != 0 && dw_thread_id() == tid) dw_thread_end(); tid = 0; } + + // User functions + void End() { if(tid != 0 && dw_thread_id() == tid) delete this; } + DWTID GetTID() { return tid; } +protected: + // Our signal handler functions to be overriden... + // If they are not overridden and an event is generated, remove the unused handler + virtual void OnThread(Thread *classptr) { + delete this; + } +}; + class App { protected: @@ -1999,6 +2086,7 @@ void Main() { dw_main(); } void MainIteration() { dw_main_iteration(); } void MainQuit() { dw_main_quit(); } + void MainSleep(int milliseconds) { dw_main_sleep(milliseconds); } void Exit(int exitcode) { dw_exit(exitcode); } void Shutdown() { dw_shutdown(); } int MessageBox(const char *title, int flags, const char *format, ...) {