comparison 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
comparison
equal deleted inserted replaced
2932:3f660f47a45f 2933:3cdb02171b01
34 #define final 34 #define final
35 #endif 35 #endif
36 36
37 namespace DW 37 namespace DW
38 { 38 {
39
40 #define THREAD_STACK 10000
39 41
40 // Forward declare these so they can be referenced 42 // Forward declare these so they can be referenced
41 class Render; 43 class Render;
42 class Pixmap; 44 class Pixmap;
43 class MenuItem; 45 class MenuItem;
1970 delete this; 1972 delete this;
1971 return FALSE; 1973 return FALSE;
1972 } 1974 }
1973 }; 1975 };
1974 1976
1977 class Thread : public Handle
1978 {
1979 private:
1980 DWTID tid;
1981 #ifdef DW_LAMBDA
1982 std::function<void(Thread *)> _ConnectThread;
1983 #endif
1984 void (*_ConnectThreadOld)(Thread *);
1985 static void _OnThread(void *data) {
1986 Thread *classptr = reinterpret_cast<Thread *>(data);
1987
1988 #ifdef DW_LAMBDA
1989 if(classptr->_ConnectThread)
1990 classptr->_ConnectThread(classptr);
1991 else
1992 #endif
1993 if(classptr->_ConnectThreadOld)
1994 classptr->_ConnectThreadOld(classptr);
1995 else
1996 classptr->OnThread(classptr);
1997
1998 delete classptr;
1999 }
2000 public:
2001 // Constructors
2002 #ifdef DW_LAMBDA
2003 Thread(std::function<void(Thread *)> userfunc) {
2004 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK);
2005 SetHandle(reinterpret_cast<void *>(tid));
2006 _ConnectThread = userfunc;
2007 _ConnectThreadOld = 0;
2008 }
2009 Thread(std::function<void(Thread *)> userfunc, int stack) {
2010 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack);
2011 SetHandle(reinterpret_cast<void *>(tid));
2012 _ConnectThread = userfunc;
2013 _ConnectThreadOld = 0;
2014 }
2015 #endif
2016 Thread(void (*userfunc)(Thread *)) {
2017 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK);
2018 SetHandle(reinterpret_cast<void *>(tid));
2019 _ConnectThreadOld = userfunc;
2020 #ifdef DW_LAMBDA
2021 _ConnectThread = 0;
2022 #endif
2023 }
2024 Thread(void (*userfunc)(Thread *), int stack) {
2025 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack);
2026 SetHandle(reinterpret_cast<void *>(tid));
2027 _ConnectThreadOld = userfunc;
2028 #ifdef DW_LAMBDA
2029 _ConnectThread = 0;
2030 #endif
2031 }
2032 Thread(int stack) {
2033 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, stack);
2034 SetHandle(reinterpret_cast<void *>(tid));
2035 _ConnectThreadOld = 0;
2036 #ifdef DW_LAMBDA
2037 _ConnectThread = 0;
2038 #endif
2039 }
2040 Thread() {
2041 tid = dw_thread_new(DW_SIGNAL_FUNC(_OnThread), this, THREAD_STACK);
2042 SetHandle(reinterpret_cast<void *>(tid));
2043 _ConnectThreadOld = 0;
2044 #ifdef DW_LAMBDA
2045 _ConnectThread = 0;
2046 #endif
2047 }
2048 // Destructor
2049 virtual ~Thread() { if(tid != 0 && dw_thread_id() == tid) dw_thread_end(); tid = 0; }
2050
2051 // User functions
2052 void End() { if(tid != 0 && dw_thread_id() == tid) delete this; }
2053 DWTID GetTID() { return tid; }
2054 protected:
2055 // Our signal handler functions to be overriden...
2056 // If they are not overridden and an event is generated, remove the unused handler
2057 virtual void OnThread(Thread *classptr) {
2058 delete this;
2059 }
2060 };
2061
1975 class App 2062 class App
1976 { 2063 {
1977 protected: 2064 protected:
1978 App() { } 2065 App() { }
1979 static App *_app; 2066 static App *_app;
1997 2084
1998 // User functions 2085 // User functions
1999 void Main() { dw_main(); } 2086 void Main() { dw_main(); }
2000 void MainIteration() { dw_main_iteration(); } 2087 void MainIteration() { dw_main_iteration(); }
2001 void MainQuit() { dw_main_quit(); } 2088 void MainQuit() { dw_main_quit(); }
2089 void MainSleep(int milliseconds) { dw_main_sleep(milliseconds); }
2002 void Exit(int exitcode) { dw_exit(exitcode); } 2090 void Exit(int exitcode) { dw_exit(exitcode); }
2003 void Shutdown() { dw_shutdown(); } 2091 void Shutdown() { dw_shutdown(); }
2004 int MessageBox(const char *title, int flags, const char *format, ...) { 2092 int MessageBox(const char *title, int flags, const char *format, ...) {
2005 int retval; 2093 int retval;
2006 va_list args; 2094 va_list args;