Created
May 19, 2014 03:34
-
-
Save ykst/b9ae81bbe7c9c8b18825 to your computer and use it in GitHub Desktop.
C言語でusing的な構文 ref: http://qiita.com/ykst/items/c045104ab9c0108ba166
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
using(something, 100) { | |
something_show(this); | |
} |
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 <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#define using(class, ...) for (class##_handle this = class##_create(__VA_ARGS__), ___do_loop = this; (___do_loop != NULL) || ({ class##_delete(this); 0; }); ___do_loop = NULL) | |
struct something { | |
int param; | |
}; | |
typedef struct something *something_handle; | |
something_handle something_create(int param) | |
{ | |
something_handle new_handle = (something_handle)calloc(1, sizeof(*new_handle)); | |
new_handle->param = param; | |
return new_handle; | |
} | |
void something_show(something_handle h) | |
{ | |
printf("param = %d\n", h->param); | |
} | |
void something_delete(something_handle h) | |
{ | |
if (h) { | |
free(h); | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
using (something, 100) { | |
something_show(this); | |
} | |
} |
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
#define using(class, ...) for (class##_handle this = class##_create(__VA_ARGS__), ___do_loop = this; (___do_loop != NULL) || ({ class##_delete(this); 0; }); ___do_loop = NULL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment