Skip to content

Instantly share code, notes, and snippets.

@ykst
Created May 19, 2014 03:34
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 ykst/b9ae81bbe7c9c8b18825 to your computer and use it in GitHub Desktop.
Save ykst/b9ae81bbe7c9c8b18825 to your computer and use it in GitHub Desktop.
C言語でusing的な構文 ref: http://qiita.com/ykst/items/c045104ab9c0108ba166
using(something, 100) {
something_show(this);
}
#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);
}
}
#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