Option to write a dummmy callbacks.h
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/063cc4bf Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/063cc4bf Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/063cc4bf Branch: refs/heads/c-bindings-wip1 Commit: 063cc4bf3fc1fbce11502022157be61145fdfa28 Parents: 2d572a6 Author: Nick Wellnhofer <[email protected]> Authored: Mon Dec 24 00:55:35 2012 +0100 Committer: Nick Wellnhofer <[email protected]> Committed: Mon Dec 24 01:19:23 2012 +0100 ---------------------------------------------------------------------- clownfish/compiler/src/CFCBindClass.c | 23 +++++++++ clownfish/compiler/src/CFCBindClass.h | 5 ++ clownfish/compiler/src/CFCBindCore.c | 75 +++++++++++++++++++++++++++- clownfish/compiler/src/CFCBindCore.h | 6 ++ 4 files changed, 108 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/063cc4bf/clownfish/compiler/src/CFCBindClass.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindClass.c b/clownfish/compiler/src/CFCBindClass.c index bb0511c..3a6b826 100644 --- a/clownfish/compiler/src/CFCBindClass.c +++ b/clownfish/compiler/src/CFCBindClass.c @@ -512,6 +512,29 @@ CFCBindClass_callback_decs(CFCBindClass *self) { return cb_decs; } +// Declare dummy host callbacks. +char* +CFCBindClass_dummy_callback_decs(CFCBindClass *self) { + CFCClass *client = self->client; + CFCMethod **fresh_methods = CFCClass_fresh_methods(client); + char *cb_decs = CFCUtil_strdup(""); + + for (int meth_num = 0; fresh_methods[meth_num] != NULL; meth_num++) { + CFCMethod *method = fresh_methods[meth_num]; + + // Define callback to NULL. + if (CFCMethod_novel(method) && !CFCMethod_final(method)) { + const char *override_sym = CFCMethod_full_override_sym(method); + cb_decs = CFCUtil_cat(cb_decs, "#define ", override_sym, " NULL\n", + NULL); + } + } + + FREEMEM(fresh_methods); + + return cb_decs; +} + // Declare typedefs for every method, to ease casting. static char* S_method_typedefs(CFCBindClass *self) { http://git-wip-us.apache.org/repos/asf/lucy/blob/063cc4bf/clownfish/compiler/src/CFCBindClass.h ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindClass.h b/clownfish/compiler/src/CFCBindClass.h index 666e46b..cca2fa8 100644 --- a/clownfish/compiler/src/CFCBindClass.h +++ b/clownfish/compiler/src/CFCBindClass.h @@ -64,6 +64,11 @@ CFCBindClass_spec_def(CFCBindClass *self); char* CFCBindClass_callback_decs(CFCBindClass *self); +/* Return dummy host callbacks defined to NULL. + */ +char* +CFCBindClass_dummy_callback_decs(CFCBindClass *self); + /** Return the man page for the class. */ char* http://git-wip-us.apache.org/repos/asf/lucy/blob/063cc4bf/clownfish/compiler/src/CFCBindCore.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindCore.c b/clownfish/compiler/src/CFCBindCore.c index c41affa..5402ca4 100644 --- a/clownfish/compiler/src/CFCBindCore.c +++ b/clownfish/compiler/src/CFCBindCore.c @@ -17,6 +17,11 @@ #include <string.h> #include <stdio.h> +#ifndef true + #define true 1 + #define false 0 +#endif + #define CFC_NEED_BASE_STRUCT_DEF #include "CFCBase.h" #include "CFCBindCore.h" @@ -34,6 +39,7 @@ struct CFCBindCore { CFCHierarchy *hierarchy; char *header; char *footer; + int callbacks_enabled; }; /* Write the "parcel.h" header file, which contains common symbols needed by @@ -53,6 +59,11 @@ S_write_parcel_c(CFCBindCore *self); static void S_write_callbacks_h(CFCBindCore *self); +/* Write the "callbacks.h" header file with dummy callbacks. + */ +static void +S_write_dummy_callbacks_h(CFCBindCore *self); + const static CFCMeta CFCBINDCORE_META = { "Clownfish::CFC::Binding::Core", sizeof(CFCBindCore), @@ -75,6 +86,7 @@ CFCBindCore_init(CFCBindCore *self, CFCHierarchy *hierarchy, self->hierarchy = (CFCHierarchy*)CFCBase_incref((CFCBase*)hierarchy); self->header = CFCUtil_strdup(header); self->footer = CFCUtil_strdup(footer); + self->callbacks_enabled = true; return self; } @@ -86,6 +98,11 @@ CFCBindCore_destroy(CFCBindCore *self) { CFCBase_destroy((CFCBase*)self); } +void +CFCBindCore_enable_callbacks(CFCBindCore *self, int enabled) { + self->callbacks_enabled = enabled; +} + int CFCBindCore_write_all_modified(CFCBindCore *self, int modified) { CFCHierarchy *hierarchy = self->hierarchy; @@ -110,7 +127,8 @@ CFCBindCore_write_all_modified(CFCBindCore *self, int modified) { if (modified) { S_write_parcel_h(self); S_write_parcel_c(self); - S_write_callbacks_h(self); + if (self->callbacks_enabled) { S_write_callbacks_h(self); } + else { S_write_dummy_callbacks_h(self); } } return modified; @@ -517,6 +535,61 @@ S_write_callbacks_h(CFCBindCore *self) { FREEMEM(file_content); } +/* Write "callbacks.h" with NULL callbacks. + */ +static void +S_write_dummy_callbacks_h(CFCBindCore *self) { + CFCHierarchy *hierarchy = self->hierarchy; + CFCClass **ordered = CFCHierarchy_ordered_classes(hierarchy); + char *all_cb_decs = CFCUtil_strdup(""); + + for (int i = 0; ordered[i] != NULL; i++) { + CFCClass *klass = ordered[i]; + + if (!CFCClass_included(klass)) { + CFCBindClass *class_binding = CFCBindClass_new(klass); + char *cb_decs = CFCBindClass_dummy_callback_decs(class_binding); + all_cb_decs = CFCUtil_cat(all_cb_decs, cb_decs, NULL); + FREEMEM(cb_decs); + CFCBase_decref((CFCBase*)class_binding); + } + } + + FREEMEM(ordered); + + const char pattern[] = + "%s\n" + "#ifndef CFCCALLBACKS_H\n" + "#define CFCCALLBACKS_H 1\n" + "\n" + "#include <stdlib.h>\n" + "\n" + "%s" + "\n" + "#endif /* CFCCALLBACKS_H */\n" + "\n" + "%s\n" + "\n"; + size_t size = sizeof(pattern) + + strlen(self->header) + + strlen(all_cb_decs) + + strlen(self->footer) + + 50; + char *file_content = (char*)MALLOCATE(size); + sprintf(file_content, pattern, self->header, all_cb_decs, self->footer); + + // Unlink then write file. + const char *inc_dest = CFCHierarchy_get_include_dest(hierarchy); + char *filepath = CFCUtil_cat(CFCUtil_strdup(""), inc_dest, + CFCUTIL_PATH_SEP, "callbacks.h", NULL); + remove(filepath); + CFCUtil_write_file(filepath, file_content, strlen(file_content)); + FREEMEM(filepath); + + FREEMEM(all_cb_decs); + FREEMEM(file_content); +} + void CFCBindCore_write_man_pages(CFCBindCore *self) { CFCHierarchy *hierarchy = self->hierarchy; http://git-wip-us.apache.org/repos/asf/lucy/blob/063cc4bf/clownfish/compiler/src/CFCBindCore.h ---------------------------------------------------------------------- diff --git a/clownfish/compiler/src/CFCBindCore.h b/clownfish/compiler/src/CFCBindCore.h index 963c74e..44a416b 100644 --- a/clownfish/compiler/src/CFCBindCore.h +++ b/clownfish/compiler/src/CFCBindCore.h @@ -57,6 +57,12 @@ CFCBindCore_destroy(CFCBindCore *self); int CFCBindCore_write_all_modified(CFCBindCore *self, int modified); +/** Control creation of callback headers. With disabled callbacks, the + * callback functions are defined to NULL. + */ +void +CFCBindCore_enable_callbacks(CFCBindCore *self, int enabled); + /** Write all man pages. */ void
