From 42d305eabe23838d5f45c2a5d42dec7f12a386f8 Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 1 Sep 2024 17:38:44 +0800 Subject: [PATCH] =?UTF-8?q?container=5Fof=E6=98=AF=E8=B0=81=E5=8F=91?= =?UTF-8?q?=E6=98=8E=E7=9A=84=EF=BC=8C=E7=9C=9F=E6=98=AF=E4=B8=AA=E5=A4=A9?= =?UTF-8?q?=E6=89=8D=EF=BC=81=EF=BC=81=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/common.h | 13 +++++++++++++ library/copper.c | 2 ++ library/copper.h | 2 ++ library/rect.c | 9 +++++++-- library/rect.h | 3 ++- library/shape.c | 2 ++ library/shape.h | 2 ++ 7 files changed, 30 insertions(+), 3 deletions(-) diff --git a/library/common.h b/library/common.h index f64363a..152bf32 100644 --- a/library/common.h +++ b/library/common.h @@ -7,5 +7,18 @@ #include #include #include +#include + +// [warring] offsetof is redefined in stddef.h +// +// #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +// #define container_of(ptr, type, member) ({ \ +// const typeof( ((type *)0)->member ) *__mptr = (ptr); \ +// (type *)( (char *)__mptr - offsetof(type,member) );}) + +#define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - _offsetof(type,member) );}) #endif diff --git a/library/copper.c b/library/copper.c index 0b1a3f8..56fd99c 100644 --- a/library/copper.c +++ b/library/copper.c @@ -58,6 +58,8 @@ struct _copper_cash *copper_cash_new(double width, double height, double radius) { return NULL; } + cc->self = cc; + cc->price = 100; // set parent cc->shape = shape; diff --git a/library/copper.h b/library/copper.h index 4f5a20a..24c0a08 100644 --- a/library/copper.h +++ b/library/copper.h @@ -9,6 +9,8 @@ struct _copper_cash { + struct _copper_cash *self; + struct _shape *shape; struct _rect *rect; struct _circle *circle; diff --git a/library/rect.c b/library/rect.c index a70eebf..f2c6a76 100644 --- a/library/rect.c +++ b/library/rect.c @@ -11,8 +11,11 @@ double rect_area(void *self, void *parent) void rect_draw(void *self, void *parent) { - struct _rect *pthis = (struct _rect *)parent; - pthis->shape->vtb.draw(self, NULL); + // struct _rect *pthis = (struct _rect *)parent; + // pthis->shape->vtb.draw(self, NULL); + + struct _rect *pthis = container_of(parent, struct _rect, self); + pthis->shape->vtb.draw(pthis, NULL); } double _rect_area(void *self, void *parent) @@ -50,6 +53,8 @@ struct _rect *rect_new(double width, double height) { return NULL; } + rect->self = rect; + rect->width = width; rect->height = height; // set parent diff --git a/library/rect.h b/library/rect.h index 18c8bf9..3cd93c1 100644 --- a/library/rect.h +++ b/library/rect.h @@ -7,7 +7,8 @@ struct _rect { - int invalid; + // int invalid; + struct _rect *self; // super struct _shape *shape; diff --git a/library/shape.c b/library/shape.c index 7d8037a..1515c14 100644 --- a/library/shape.c +++ b/library/shape.c @@ -56,6 +56,8 @@ struct _shape *shape_new(void) { return NULL; } + shape->self = shape; + static struct _virtual_table vtb = { .area = (double (*)(void *self, void *parent))_shape_area, .draw = (void (*)(void *self, void *parent))_shape_draw diff --git a/library/shape.h b/library/shape.h index aef632e..2b4cb95 100644 --- a/library/shape.h +++ b/library/shape.h @@ -17,6 +17,8 @@ struct _virtual_table struct _shape { + struct _shape *self; + struct _virtual_table vtb; };