comparison dw.hpp @ 2905:0f5008c05134

C++: Implement Timer class, a bunch of destructors and functions in App.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 25 Dec 2022 16:02:59 +0000
parents 9fd16f694a77
children 9d6280f206bd
comparison
equal deleted inserted replaced
2904:2b7babf491e1 2905:0f5008c05134
1399 private: 1399 private:
1400 HMTX mutex; 1400 HMTX mutex;
1401 public: 1401 public:
1402 // Constructors 1402 // Constructors
1403 Mutex() { mutex = dw_mutex_new(); SetHandle(reinterpret_cast<void *>(mutex)); } 1403 Mutex() { mutex = dw_mutex_new(); SetHandle(reinterpret_cast<void *>(mutex)); }
1404 1404 // Destructor
1405 // User functions 1405 ~Mutex() { dw_mutex_close(mutex); mutex = 0; }
1406 void Close() { dw_mutex_close(mutex); delete this; } 1406
1407 // User functions
1408 void Close() { dw_mutex_close(mutex); mutex = 0; delete this; }
1407 void Lock() { dw_mutex_lock(mutex); } 1409 void Lock() { dw_mutex_lock(mutex); }
1408 int TryLock() { return dw_mutex_trylock(mutex); } 1410 int TryLock() { return dw_mutex_trylock(mutex); }
1409 void Unlock() { dw_mutex_unlock(mutex); } 1411 void Unlock() { dw_mutex_unlock(mutex); }
1410 }; 1412 };
1411 1413
1414 private: 1416 private:
1415 HEV event; 1417 HEV event;
1416 public: 1418 public:
1417 // Constructors 1419 // Constructors
1418 Event() { event = dw_event_new(); SetHandle(reinterpret_cast<void *>(event)); } 1420 Event() { event = dw_event_new(); SetHandle(reinterpret_cast<void *>(event)); }
1421 // Destructor
1422 virtual ~Event() { if(event) dw_event_close(&event); }
1419 1423
1420 // User functions 1424 // User functions
1421 int Close() { int retval = dw_event_close(&event); delete this; return retval; } 1425 int Close() { int retval = dw_event_close(&event); delete this; return retval; }
1422 int Post() { return dw_event_post(event); } 1426 int Post() { return dw_event_post(event); }
1423 int Reset() { return dw_event_reset(event); } 1427 int Reset() { return dw_event_reset(event); }
1424 int Wait(unsigned long timeout) { return dw_event_wait(event, timeout); } 1428 int Wait(unsigned long timeout) { return dw_event_wait(event, timeout); }
1429 };
1430
1431 class Timer : public Handle
1432 {
1433 private:
1434 HTIMER timer;
1435 #ifdef DW_LAMBDA
1436 std::function<int()> _ConnectTimer;
1437 #else
1438 int (*_ConnectTimer)();
1439 #endif
1440 static int _OnTimer(void *data) {
1441 if(reinterpret_cast<Timer *>(data)->_ConnectTimer)
1442 return reinterpret_cast<Timer *>(data)->_ConnectTimer();
1443 return reinterpret_cast<Timer *>(data)->OnTimer();
1444 }
1445 public:
1446 // Constructors
1447 Timer(int interval) { _ConnectTimer = 0; timer = dw_timer_connect(interval, DW_SIGNAL_FUNC(_OnTimer), this); SetHandle(reinterpret_cast<void *>(timer)); }
1448 #ifdef DW_LAMBDA
1449 Timer(int interval, std::function<int()> userfunc)
1450 #else
1451 Timer(int interval, int (*userfunc)())
1452 #endif
1453 {
1454 _ConnectTimer = userfunc;
1455 timer = dw_timer_connect(interval, DW_SIGNAL_FUNC(_OnTimer), this);
1456 SetHandle(reinterpret_cast<void *>(timer));
1457 }
1458 // Destructor
1459 virtual ~Timer() { if(timer) { dw_timer_disconnect(timer); timer = 0; } }
1460
1461 // User functions
1462 HTIMER GetHTIMER() { return timer; }
1463 void Disconnect() { if(timer) { dw_timer_disconnect(timer); timer = 0; } delete this; }
1464 protected:
1465 // Our signal handler functions to be overriden...
1466 // If they are not overridden and an event is generated, remove the unused handler
1467 virtual int OnTimer() {
1468 if(timer)
1469 dw_timer_disconnect(timer);
1470 delete this;
1471 return FALSE;
1472 }
1425 }; 1473 };
1426 1474
1427 class App 1475 class App
1428 { 1476 {
1429 protected: 1477 protected:
1442 static App *Init(const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, DW_NULL); dw_init(TRUE, 0, DW_NULL); } return _app; } 1490 static App *Init(const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, DW_NULL); dw_init(TRUE, 0, DW_NULL); } return _app; }
1443 static App *Init(const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, 0, DW_NULL); } return _app; } 1491 static App *Init(const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, 0, DW_NULL); } return _app; }
1444 static App *Init(int argc, char *argv[]) { if(!_app) { _app = new App(); dw_init(TRUE, argc, argv); } return _app; } 1492 static App *Init(int argc, char *argv[]) { if(!_app) { _app = new App(); dw_init(TRUE, argc, argv); } return _app; }
1445 static App *Init(int argc, char *argv[], const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, DW_NULL); dw_init(TRUE, argc, argv); } return _app; } 1493 static App *Init(int argc, char *argv[], const char *appid) { if(!_app) { _app = new App(); dw_app_id_set(appid, DW_NULL); dw_init(TRUE, argc, argv); } return _app; }
1446 static App *Init(int argc, char *argv[], const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } return _app; } 1494 static App *Init(int argc, char *argv[], const char *appid, const char *appname) { if(!_app) { _app = new App(); dw_app_id_set(appid, appname); dw_init(TRUE, argc, argv); } return _app; }
1495 // Destrouctor
1496 ~App() { dw_exit(0); }
1447 1497
1448 // User functions 1498 // User functions
1449 void Main() { dw_main(); } 1499 void Main() { dw_main(); }
1450 void MainIteration() { dw_main_iteration(); } 1500 void MainIteration() { dw_main_iteration(); }
1451 void MainQuit() { dw_main_quit(); } 1501 void MainQuit() { dw_main_quit(); }
1452 void Exit(int exitcode) { dw_exit(exitcode); } 1502 void Exit(int exitcode) { dw_exit(exitcode); }
1503 void Shutdown() { dw_shutdown(); }
1453 int MessageBox(const char *title, int flags, const char *format, ...) { 1504 int MessageBox(const char *title, int flags, const char *format, ...) {
1454 int retval; 1505 int retval;
1455 va_list args; 1506 va_list args;
1456 1507
1457 va_start(args, format); 1508 va_start(args, format);
1465 1516
1466 va_start(args, format); 1517 va_start(args, format);
1467 dw_vdebug(format, args); 1518 dw_vdebug(format, args);
1468 va_end(args); 1519 va_end(args);
1469 } 1520 }
1521 void Beep(int freq, int dur) { dw_beep(freq, dur); }
1522 void GetEnvironment(DWEnv *env) { dw_environment_query(env); }
1523 char *GetClipboard() { return dw_clipboard_get_text(); }
1524 void SetClipboard(const char *text) { if(text) dw_clipboard_set_text(text, strlen(text)); }
1525 void SetDefaultFont(const char *fontname) { dw_font_set_default(fontname); }
1526 unsigned long ColorChoose(unsigned long initial) { return dw_color_choose(initial); }
1527 char *FileBrowse(const char *title, const char *defpath, const char *ext, int flags) { return dw_file_browse(title, defpath, ext, flags); }
1528 char *FontChoose(const char *currfont) { return dw_font_choose(currfont); }
1529 void Free(void *buff) { dw_free(buff); }
1470 }; 1530 };
1471 1531
1472 // Static singleton reference declared outside of the class 1532 // Static singleton reference declared outside of the class
1473 App* App::_app = DW_NULL; 1533 App* App::_app = DW_NULL;
1474 1534