From 1307bbdfcc40506785d0dd182bf57393b82e47bc Mon Sep 17 00:00:00 2001 From: Pietro Gagliardi Date: Sun, 17 May 2015 18:33:18 -0400 Subject: [PATCH] More dynamic typing work. --- redo/box.c | 1 + redo/tools/idl2h.go | 13 ++++++++++++- redo/types.c | 10 ++++++---- redo/ui.idl | 4 ++-- redo/windows/OLDgroup.c | 1 + redo/windows/OLDtab.c | 1 + redo/windows/button.c | 1 + redo/windows/checkbox.c | 1 + redo/windows/entry.c | 1 + redo/windows/group.c | 1 + redo/windows/label.c | 1 + redo/windows/tab.c | 1 + redo/windows/window.c | 1 + 13 files changed, 30 insertions(+), 7 deletions(-) diff --git a/redo/box.c b/redo/box.c index 437edc9f..5fa9a29b 100644 --- a/redo/box.c +++ b/redo/box.c @@ -270,6 +270,7 @@ uiBox *uiNewHorizontalBox(void) b->controls = newPtrArray(); + uiControl(b)->Type = uiTypeBox(); b->baseDestroy = uiControl(b)->Destroy; uiControl(b)->Destroy = boxDestroy; b->baseSetParent = uiControl(b)->SetParent; diff --git a/redo/tools/idl2h.go b/redo/tools/idl2h.go index f7cc533a..ef3d0237 100644 --- a/redo/tools/idl2h.go +++ b/redo/tools/idl2h.go @@ -94,6 +94,8 @@ func geniface(i *pgidl.Interface, prefix string) { fmt.Printf("struct %s%s {\n", prefix, i.Name) if i.From != "" { fmt.Printf("\t%s%s base;\n", prefix, i.From) + } else { + fmt.Printf("\tuintmax_t Type;\n") } for _, f := range i.Fields { fmt.Printf("\t%s;\n", typedecl(f.Type, f.Name)) @@ -113,8 +115,17 @@ func geniface(i *pgidl.Interface, prefix string) { fmt.Printf("%s\n", cmethodmacro(m, prefix + i.Name)) } fmt.Printf("};\n") - fmt.Printf("#define %s%s(this) ((%s%s *) (this))\n", + fmt.Printf("%s uintmax_t %sType%s(void);\n", + *extern, + prefix, i.Name) + fmt.Printf("#define %s%s(this) ((%s%s *) %sIsA((this), %sType%s(), 1))\n", prefix, i.Name, + prefix, i.Name, + prefix, + prefix, i.Name) + fmt.Printf("#define %sIs%s(this) (%sIsA((this), %sType%s(), 0) != NULL)\n", + prefix, i.Name, + prefix, prefix, i.Name) } diff --git a/redo/types.c b/redo/types.c index 4a058160..054c0901 100644 --- a/redo/types.c +++ b/redo/types.c @@ -25,7 +25,7 @@ uintmax_t uiRegisterType(const char *name, uintmax_t parent) return types->len - 1; } -void *uiIsA(void *p, uintmax_t id) +void *uiIsA(void *p, uintmax_t id, int fail) { uiTyped *t; struct typeinfo *ti; @@ -45,9 +45,11 @@ void *uiIsA(void *p, uintmax_t id) break; compareTo = ti->parent; } - ti = ptrArrayIndex(types, struct typeInfo *, t->Type); - complain("object %p not a %s in uiIsA()", t, ti->name); - return NULL; // make compiler happy + if (fail) { + ti = ptrArrayIndex(types, struct typeInfo *, t->Type); + complain("object %p not a %s in uiIsA()", t, ti->name); + } + return NULL; } // TODO free type info diff --git a/redo/ui.idl b/redo/ui.idl index e27cb0ec..fcf0ce15 100644 --- a/redo/ui.idl +++ b/redo/ui.idl @@ -29,8 +29,8 @@ func OnShouldQuit(f *func(data *void) int, data *void); func FreeText(text *char); func RegisterType(name *const char, parent uintmax_t); -func IsA(p *void, type uintmax_t); -struct uiTyped { +func IsA(p *void, type uintmax_t, fail int); +struct Typed { field Type uintmax_t; }; diff --git a/redo/windows/OLDgroup.c b/redo/windows/OLDgroup.c index 344d6910..bc330448 100644 --- a/redo/windows/OLDgroup.c +++ b/redo/windows/OLDgroup.c @@ -110,6 +110,7 @@ uiGroup *uiNewGroup(const char *text) g->hwnd = (HWND) uiControlHandle(uiControl(g)); + uiControl(g)->Type = uiTypeGroup(); uiControl(g)->PreferredSize = groupPreferredSize; g->baseResize = uiControl(g)->Resize; uiControl(g)->Resize = groupResize; diff --git a/redo/windows/OLDtab.c b/redo/windows/OLDtab.c index 66e932a6..6a671c92 100644 --- a/redo/windows/OLDtab.c +++ b/redo/windows/OLDtab.c @@ -349,6 +349,7 @@ uiTab *uiNewTab(void) if (SetWindowSubclass(t->hwnd, tabSubProc, 0, (DWORD_PTR) t) == FALSE) logLastError("error subclassing Tab to give it its own resize handler in uiNewTab()"); + uiControl(t)->Type = uiTypeTab(); uiControl(t)->PreferredSize = tabPreferredSize; t->baseResize = uiControl(t)->Resize; uiControl(t)->Resize = tabResize; diff --git a/redo/windows/button.c b/redo/windows/button.c index b07cd657..bea672df 100644 --- a/redo/windows/button.c +++ b/redo/windows/button.c @@ -105,6 +105,7 @@ uiButton *uiNewButton(const char *text) b->onClicked = defaultOnClicked; + uiControl(b)->Type = uiTypeButton(); uiControl(b)->PreferredSize = buttonPreferredSize; uiButton(b)->Text = buttonText; diff --git a/redo/windows/checkbox.c b/redo/windows/checkbox.c index 07148f6e..0490515a 100644 --- a/redo/windows/checkbox.c +++ b/redo/windows/checkbox.c @@ -120,6 +120,7 @@ uiCheckbox *uiNewCheckbox(const char *text) c->onToggled = defaultOnToggled; + uiControl(c)->Type = uiTypeCheckbox(); uiControl(c)->PreferredSize = checkboxPreferredSize; uiCheckbox(c)->Text = checkboxText; diff --git a/redo/windows/entry.c b/redo/windows/entry.c index ec8b66f7..9bc91fdc 100644 --- a/redo/windows/entry.c +++ b/redo/windows/entry.c @@ -115,6 +115,7 @@ uiEntry *uiNewEntry(void) e->onChanged = defaultOnChanged; + uiControl(e)->Type = uiTypeEntry(); uiControl(e)->PreferredSize = entryPreferredSize; uiEntry(e)->Text = entryText; diff --git a/redo/windows/group.c b/redo/windows/group.c index 91b2fefb..d1f7f8ab 100644 --- a/redo/windows/group.c +++ b/redo/windows/group.c @@ -56,6 +56,7 @@ uiGroup *uiNewGroup(const char *text) g->hwnd = (HWND) uiControlHandle(uiControl(g)); + uiControl(g)->Type = uiTypeGroup(); uiControl(g)->PreferredSize = groupPreferredSize; uiGroup(g)->SetChild = groupSetChild; diff --git a/redo/windows/label.c b/redo/windows/label.c index b277871d..6ecc0e51 100644 --- a/redo/windows/label.c +++ b/redo/windows/label.c @@ -71,6 +71,7 @@ uiLabel *uiNewLabel(const char *text) l->hwnd = (HWND) uiControlHandle(uiControl(l)); + uiControl(l)->Type = uiTypeLabel(); uiControl(l)->PreferredSize = labelPreferredSize; uiLabel(l)->Text = labelText; diff --git a/redo/windows/tab.c b/redo/windows/tab.c index 797eeb61..b1da1181 100644 --- a/redo/windows/tab.c +++ b/redo/windows/tab.c @@ -74,6 +74,7 @@ uiTab *uiNewTab(void) t->hwnd = (HWND) uiControlHandle(uiControl(t)); + uiControl(t)->Type = uiTypeTab(); uiControl(t)->PreferredSize = tabPreferredSize; uiTab(t)->AppendPage = tabAppendPage; diff --git a/redo/windows/window.c b/redo/windows/window.c index 9a7cbe7f..362268b4 100644 --- a/redo/windows/window.c +++ b/redo/windows/window.c @@ -355,6 +355,7 @@ uiWindow *uiNewWindow(const char *title, int width, int height, int hasMenubar) w->onClosing = defaultOnClosing; + uiControl(w)->Type = uiTypeWindow(); uiControl(w)->Destroy = windowDestroy; uiControl(w)->Handle = windowHandle; uiControl(w)->Parent = windowParent;