Mercurial > dwindows
changeset 2867:ada74f4d3f39
C++: Implement conditional signal handlers based on overrides.
This uses templates in a sort of hacky way to determine if our virtual
functions have been overridden. Leaving some debug code in for the moment
to make sure it works on all platforms. OS/2 will require using GCC for the
C++ bindings, since VisualAge only supports C++99 and Watcom doesn't even
support that as far as I know.
author | bsmith@81767d24-ef19-dc11-ae90-00e081727c95 |
---|---|
date | Sat, 17 Dec 2022 21:39:00 +0000 |
parents | 6ea67d0809eb |
children | 5ee1aaa48fc7 |
files | Makefile.in dw.hpp dwtestoo.cpp |
diffstat | 3 files changed, 21 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/Makefile.in Sat Dec 17 11:51:22 2022 +0000 +++ b/Makefile.in Sat Dec 17 21:39:00 2022 +0000 @@ -6,6 +6,7 @@ MLFLAGS = -L. include $(srcdir)/Version CCFLAGS = @CFLAGS@ $(PLATCCFLAGS) @ARCH@ -D@DW_DEFINE@ -DBUILD_DLL -DDW_RESOURCES -DVER_REV=$(VER_REV) +CXXFLAGS= -std=c++11 LFLAGS = @LIBS@ @ARCH@ ARFLAGS = @ARFLAGS@ INSTALL = @INSTALL@ @@ -39,7 +40,7 @@ # $(CC) -c $(CCFLAGS) $(INCPATH) -o $@ $< #.cpp.o: -# $(CXX) -c $(CCFLAGS) $(INCPATH) -o $@ $< +# $(CXX) -c $(CXXFLAGS) $(CCFLAGS) $(INCPATH) -o $@ $< # Link flags shared objects @@ -199,7 +200,7 @@ -$(srcdir)/mac/finishup.sh $(srcdir) dwtest dwtestoo.o: $(srcdir)/dwtestoo.cpp $(srcdir)/dw.h $(srcdir)/dw.hpp - $(CXX) -c $(INCPATH) $(CCFLAGS) -o $@ $(srcdir)/dwtestoo.cpp + $(CXX) -c $(INCPATH) $(CXXFLAGS) $(CCFLAGS) -o $@ $(srcdir)/dwtestoo.cpp dwtestoo: dwtestoo.o $(CC) -o dwtestoo dwtestoo.o $(MLFLAGS) -l$(TARGET) $(LFLAGS) -lstdc++
--- a/dw.hpp Sat Dec 17 11:51:22 2022 +0000 +++ b/dw.hpp Sat Dec 17 21:39:00 2022 +0000 @@ -1,5 +1,6 @@ /* Dynamic Windows C++ Language Bindings * Copyright 2022 Brian Smith + * Requires a C++11 compatible compiler. */ #ifndef _HPP_DW @@ -35,11 +36,20 @@ }; // Top-level window class is packable +template <class Derived> class Window : public Box { private: - void Setup() { dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); - dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); } + void Setup() { + if(&Derived::OnDelete != &Window::OnDelete) { + dw_signal_connect(hwnd, DW_SIGNAL_DELETE, DW_SIGNAL_FUNC(_OnDelete), this); + dw_debug("DW_SIGNAL_DELETE\n"); + } + if(&Derived::OnConfigure != &Window::OnConfigure) { + dw_signal_connect(hwnd, DW_SIGNAL_CONFIGURE, DW_SIGNAL_FUNC(_OnConfigure), this); + dw_debug("DW_SIGNAL_CONFIGURE\n"); + } + } static int _OnDelete(HWND window, void *data) { return reinterpret_cast<Window *>(data)->OnDelete(); } static int _OnConfigure(HWND window, int width, int height, void *data) { return reinterpret_cast<Window *>(data)->OnConfigure(width, height); } public: @@ -54,9 +64,9 @@ void SetText(const char *text) { dw_window_set_text(hwnd, text); } void SetSize(unsigned long width, unsigned long height) { dw_window_set_size(hwnd, width, height); } void Show() { dw_window_show(hwnd); } -protected: - virtual int OnDelete() = 0; - virtual int OnConfigure(int width, int height) = 0; + // Our signal handler functions to be overriden + virtual int OnDelete() { return FALSE; } + virtual int OnConfigure(int width, int height) { return FALSE; }; }; class App
--- a/dwtestoo.cpp Sat Dec 17 11:51:22 2022 +0000 +++ b/dwtestoo.cpp Sat Dec 17 21:39:00 2022 +0000 @@ -1,15 +1,14 @@ #include <dw.hpp> -class MyWindow : public DW::Window +class MyWindow : public DW::Window<MyWindow> { public: MyWindow() { SetText("Basic application"); SetSize(200, 200); } -protected: - virtual int OnDelete() { DW::App *app = DW::App::Init(); app->MainQuit(); return FALSE; } - virtual int OnConfigure(int width, int height) { return FALSE; } + virtual int OnDelete() override { DW::App *app = DW::App::Init(); app->MainQuit(); return FALSE; } + virtual int OnConfigure(int width, int height) override { return FALSE; } }; int dwmain(int argc, char* argv[])