diff --git a/print_dispatch.c b/print_dispatch.c new file mode 100644 index 0000000000000000000000000000000000000000..ff18d7ebc309a2b38ffad05f8c91e2134e3210cf --- /dev/null +++ b/print_dispatch.c @@ -0,0 +1,25 @@ +/* Compile with: +CFLAGS="-g -Wall -std=gnu11 -O3" make print_dispatch +*/ +#define skip_main +#include "print_methods.c" + +textlist_s recipe = {.title="Starfish and Coffee", + .len=2, .items=(char*[]){"Starfish", "Coffee"}}; + +void textlist_print(textlist_s *in){ + if (in->print){ + in->print(in); + return; + } + + printf("Title: %s\n\nItems:\n", in->title); + for (int i=0; i< in->len; i++) + printf("\t%s\n", in->items[i]); +} + +int main(){ + textlist_print(&save); + printf("\n-----\n\n"); + textlist_print(&recipe); +} diff --git a/print_methods.c b/print_methods.c new file mode 100644 index 0000000000000000000000000000000000000000..764820a2f5f8cca83273a91a0b11d16c307b5723 --- /dev/null +++ b/print_methods.c @@ -0,0 +1,34 @@ +/* Compile with: +CFLAGS="-g -Wall -std=gnu11 -O3" make print_methods +*/ +#include +#include "print_typedef.h" + +static void print_ad(textlist_s *in){ + printf("BUY THIS %s!!!! Features:\n", in->title); + for (int i=0; i< in->len; i++) + printf("∙ %s\n", in->items[i]); +} + +static void print_song(textlist_s *in){ + printf("♫ %s ♫\nLyrics:\n\n", in->title); + for (int i=0; i< in->len; i++) + printf("\t%s\n", in->items[i]); +} + +textlist_s save = {.title="God Save the Queen", + .len=3, .items=(char*[]){ + "There's no future", "No future", "No future for me."}, + .print=print_song}; + +textlist_s spend = {.title="Never mind the Bollocks LP", + .items=(char*[]){"By the Sex Pistols", "Anti-consumption themes"}, + .len=2, .print=print_ad}; + +#ifndef skip_main +int main(){ + save.print(&save); + printf("\n-----\n\n"); + spend.print(&spend); +} +#endif diff --git a/print_typedef.h b/print_typedef.h new file mode 100644 index 0000000000000000000000000000000000000000..aac456c2530a4637ce7410d91e84935723cf70e6 --- /dev/null +++ b/print_typedef.h @@ -0,0 +1,11 @@ +#ifndef textlist_s_h +#define textlist_s_h + +typedef struct textlist_s { + char *title; + char **items; + int len; + void (*print)(struct textlist_s*); +} textlist_s; + +#endif diff --git a/print_vtable.c b/print_vtable.c new file mode 100644 index 0000000000000000000000000000000000000000..e5e372c62c046b17a5cbf458bf352ed31efd5f4d --- /dev/null +++ b/print_vtable.c @@ -0,0 +1,22 @@ +/* See compilation notes in print_vtable_use.c*/ +#include +#include "print_vtable.h" + +GHashTable *print_fns; + +void check_print_fn(print_fn_type pf) { } + +void textlist_print_html(textlist_s *in){ + if (!print_fns) print_fns = g_hash_table_new(g_direct_hash, g_direct_equal); + + print_fn_type ph = g_hash_table_lookup(print_fns, in->print); + if (ph) { + ph(in); + return; + } + printf("%s\n\n"); +} + diff --git a/print_vtable.h b/print_vtable.h new file mode 100644 index 0000000000000000000000000000000000000000..f8b41b890cf9d9f3b907cd8e9c3d133cd2e34531 --- /dev/null +++ b/print_vtable.h @@ -0,0 +1,16 @@ +/* See compilation notes in print_vtable_use.c*/ +#include +#include "print_typedef.h" + +extern GHashTable *print_fns; + +typedef void (*print_fn_type)(textlist_s*); + +void check_print_fn(print_fn_type pf); + +#define print_hash_add(object, print_fn){ \ + check_print_fn(print_fn); \ + g_hash_table_insert(print_fns, (object)->print, print_fn); \ +} + +void textlist_print_html(textlist_s *in); diff --git a/print_vtable_use.c b/print_vtable_use.c new file mode 100644 index 0000000000000000000000000000000000000000..8c5e390b8111ddd51a9cb8b3a4479955e34365b0 --- /dev/null +++ b/print_vtable_use.c @@ -0,0 +1,27 @@ +/* Suggested makefile: +---------- +P=print_vtable_use +objects=print_vtable.o +CFLAGS=`pkg-config --cflags glib-2.0` -g -Wall -std=gnu11 -O3 +LDLIBS=`pkg-config --libs glib-2.0` + +$(P): $(objects) +---------- +*/ +#define skip_main +#include "print_methods.c" +#include "print_vtable.h" + +static void song_print_html(textlist_s *in){ + printf("♫ %s ♫\n", in->title); + for (int i=0; i < in->len; i++) + printf("%s
\n", in->items[i]); +} + +int main(){ + textlist_print_html(&save); + printf("\n-----\n\n"); + + print_hash_add(&save, song_print_html); + textlist_print_html(&save); +}