Created
April 13, 2017 12:56
-
-
Save dogbert17/c1c44e170319d993ad50c71802173f57 to your computer and use it in GitHub Desktop.
08-callbacks.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#ifdef _WIN32 | |
#define DLLEXPORT __declspec(dllexport) | |
#else | |
#define DLLEXPORT extern | |
#endif | |
typedef struct { | |
char *str; | |
long ival; | |
} Struct; | |
DLLEXPORT void TakeACallback(void (*cb)(void)) { | |
cb(); | |
} | |
DLLEXPORT void TakeIntCallback(void (*cb)(int)) { | |
cb(17); | |
} | |
DLLEXPORT void TakeStringCallback(void (*cb)(char *)) { | |
cb("lorem ipsum"); | |
} | |
DLLEXPORT void TakeStructCallback(void (*cb)(Struct *)) { | |
Struct *s = (Struct *) malloc(sizeof(Struct)); | |
s->str = "foobar"; | |
s->ival = -42; | |
cb(s); | |
} | |
DLLEXPORT int CheckReturnsFloat(double (*cb)()) { | |
double num = cb(); | |
if(1.23 - num > 0.001) return 1; | |
return 6; | |
} | |
DLLEXPORT int CheckReturnsStr(char *(*cb)()) { | |
char *str = cb(); | |
if(strcmp(str, "Herps and derps")) return 1; | |
return 7; | |
} | |
DLLEXPORT int CheckReturnsStruct(Struct *(*cb)()) { | |
Struct *s = cb(); | |
if(s->ival != 314) return 1; | |
if(strcmp(s->str, "Tweedledum, tweedledee")) return 2; | |
return 8; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment