mirror of
https://github.com/lloeki/cblocks-clobj.git
synced 2025-12-06 10:44:40 +01:00
bound methods via blocks
This commit is contained in:
parent
0191e7acf1
commit
d0281df615
1 changed files with 23 additions and 9 deletions
32
main.c
32
main.c
|
|
@ -5,16 +5,13 @@
|
||||||
struct Post_s {
|
struct Post_s {
|
||||||
char *author;
|
char *author;
|
||||||
char *body;
|
char *body;
|
||||||
|
|
||||||
|
void (^init)();
|
||||||
|
void (^printf)();
|
||||||
|
void (^dealloc)();
|
||||||
};
|
};
|
||||||
typedef struct Post_s Post;
|
typedef struct Post_s Post;
|
||||||
|
|
||||||
Post *Post_alloc() {
|
|
||||||
Post *new = malloc(sizeof(Post));
|
|
||||||
new->author = malloc(255);
|
|
||||||
new->body = malloc(1024);
|
|
||||||
return new;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Post_init(Post *self) {
|
void Post_init(Post *self) {
|
||||||
*self->author = '\0';
|
*self->author = '\0';
|
||||||
*self->body = '\0';
|
*self->body = '\0';
|
||||||
|
|
@ -25,14 +22,31 @@ void Post_printf(Post *self) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Post_dealloc(Post *self) {
|
void Post_dealloc(Post *self) {
|
||||||
|
Block_release(self->dealloc);
|
||||||
|
Block_release(self->printf);
|
||||||
|
Block_release(self->init);
|
||||||
|
|
||||||
free(self->body);
|
free(self->body);
|
||||||
free(self->author);
|
free(self->author);
|
||||||
free(self);
|
free(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Post *Post_alloc() {
|
||||||
|
Post *new = malloc(sizeof(Post));
|
||||||
|
new->author = malloc(255);
|
||||||
|
new->body = malloc(1024);
|
||||||
|
|
||||||
|
new->init = Block_copy( ^ { Post_init(new); });
|
||||||
|
new->printf = Block_copy( ^ { Post_printf(new); });
|
||||||
|
new->dealloc = Block_copy( ^ { Post_dealloc(new); });
|
||||||
|
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
Post *post = Post_alloc();
|
Post *post = Post_alloc();
|
||||||
Post_printf(post);
|
post->init();
|
||||||
Post_dealloc(post);
|
post->printf();
|
||||||
|
post->dealloc();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue