Skip to content

Instantly share code, notes, and snippets.

@hugopeixoto
Created September 27, 2014 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hugopeixoto/f0c75a173b9401a8e058 to your computer and use it in GitHub Desktop.
Save hugopeixoto/f0c75a173b9401a8e058 to your computer and use it in GitHub Desktop.
#include <stdio.h>
struct fish_key_store {
void* data;
int (*getter) (void*, const char*, char*, size_t);
int (*setter) (void*, const char*, const char*);
};
struct fish_key_store fish_key_store_make (void* data, void* getter, void* setter) {
return (struct fish_key_store){
.data = data,
.getter = (int(*)(void*, const char*, char*, size_t))getter,
.setter = (int(*)(void*, const char*, const char*))setter,
};
}
void f (struct fish_key_store store) {
char buffer[1024];
store.getter(store.data, "key", buffer, 1024);
store.setter(store.data, "key", "value");
}
struct first_key_store { };
int first_key_store_get (struct first_key_store* a, const char* b, char* c, size_t d) { printf("getting first key %s\n", b); return -1; }
int first_key_store_set (struct first_key_store* a, const char* b, const char* c) { printf("setting first key %s: %s\n", b, c); return -1; }
struct second_key_store {};
int second_key_store_get (struct second_key_store* a, const char* b, char* c, size_t d) { printf("getting second key %s\n", b); return -1; }
int second_key_store_set (struct second_key_store* a, const char* b, const char* c) { printf("setting second key %s: %s\n", b, c); return -1; }
int main () {
struct first_key_store first;
f(fish_key_store_make(&first, first_key_store_get, first_key_store_set));
struct second_key_store second;
f(fish_key_store_make(&second, second_key_store_get, second_key_store_set));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment