提交 31f36c5b 编写于 作者: B Ben

Add examples for function dispatch

上级 f60033cf
/* 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);
}
/* Compile with:
CFLAGS="-g -Wall -std=gnu11 -O3" make print_methods
*/
#include <stdio.h>
#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
#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
/* See compilation notes in print_vtable_use.c*/
#include <stdio.h>
#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("<title>%s</title>\n<ul>", in->title);
for (int i=0; i < in->len; i++)
printf("<li>%s</li>\n", in->items[i]);
printf("</ul>\n");
}
/* See compilation notes in print_vtable_use.c*/
#include <glib.h>
#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);
/* 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("<title>♫ %s ♫</title>\n", in->title);
for (int i=0; i < in->len; i++)
printf("%s<br>\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);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册