From 37ea93714742f805e1ee6f0f61824ca27dbbf567 Mon Sep 17 00:00:00 2001 From: jf-home Date: Sun, 1 Sep 2024 18:02:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E6=B5=8B=E5=8F=91=E7=8E=B0=E8=BF=98?= =?UTF-8?q?=E6=98=AF=E4=B8=8D=E8=A1=8C=E4=BA=86=EF=BC=8Ccontainer=5Fof?= =?UTF-8?q?=E5=86=8D=E5=BC=BA=E5=A4=A7=EF=BC=8C=E4=B9=9F=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=8E=A8=E6=96=AD=E5=87=BA=E8=BF=99=E4=B8=AA=E5=91=80=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/circle.c | 19 ++++++++++--------- library/circle.h | 6 +++--- library/copper.c | 6 +++--- library/copper.h | 2 +- library/rect.c | 25 +++++++++++-------------- library/rect.h | 6 +++--- library/shape.c | 26 +++++++++++++------------- library/shape.h | 10 +++++----- src/main.c | 21 +++++++++++---------- 9 files changed, 60 insertions(+), 61 deletions(-) diff --git a/library/circle.c b/library/circle.c index c4e4a62..c47c323 100644 --- a/library/circle.c +++ b/library/circle.c @@ -1,21 +1,21 @@ #include "circle.h" -double circle_area(void *self, void *parent) +double circle_area(void *self) { double area = 0; - struct _circle *pthis = (struct _circle *)parent; - area = pthis->shape->vtb.area(self, NULL); + struct _circle *pthis = container_of(self, struct _circle, me); + area = pthis->shape->vtb.area(pthis); return area; } -void circle_draw(void *self, void *parent) +void circle_draw(void *self) { - struct _circle *pthis = (struct _circle *)parent; - pthis->shape->vtb.draw(self, NULL); + struct _circle *pthis = container_of(self, struct _circle, me); + pthis->shape->vtb.draw(pthis); } -double _circle_area(void *self, void *parent) +double _circle_area(void *self) { struct _circle *pthis = (struct _circle *)self; double area = 3.14 * pthis->radius * pthis->radius; @@ -32,8 +32,8 @@ struct _circle *circle_new(double radius) { // super static struct _virtual_table vtb = { - .area = (double (*)(void *self, void *parent))_circle_area, - .draw = (void (*)(void *self, void *parent))_circle_draw, + .area = (double (*)(void *self))_circle_area, + .draw = (void (*)(void *self))_circle_draw, }; struct _shape *shape = shape_new(); @@ -49,6 +49,7 @@ struct _circle *circle_new(double radius) { return NULL; } + circle->me = circle; circle->radius = radius; // set parent circle->shape = shape; diff --git a/library/circle.h b/library/circle.h index 263e059..c77690b 100644 --- a/library/circle.h +++ b/library/circle.h @@ -7,7 +7,7 @@ struct _circle { - int invalid; + struct _circle *me; // super struct _shape *shape; @@ -22,7 +22,7 @@ struct _circle struct _circle *circle_new(double radius); -double circle_area(void *self, void *parent); -void circle_draw(void *self, void *parent); +double circle_area(void *self); +void circle_draw(void *self); #endif diff --git a/library/copper.c b/library/copper.c index 56fd99c..97df8dc 100644 --- a/library/copper.c +++ b/library/copper.c @@ -27,8 +27,8 @@ struct _copper_cash *copper_cash_new(double width, double height, double radius) { // super static struct _virtual_table vtb = { - .area = (double (*)(void *self, void *parent))copper_cash_area, - .draw = (void (*)(void *self, void *parent))copper_cash_draw, + .area = (double (*)(void *self))copper_cash_area, + .draw = (void (*)(void *self))copper_cash_draw, }; struct _shape *shape = shape_new(); @@ -58,7 +58,7 @@ struct _copper_cash *copper_cash_new(double width, double height, double radius) { return NULL; } - cc->self = cc; + cc->me = cc; cc->price = 100; // set parent diff --git a/library/copper.h b/library/copper.h index 24c0a08..246daba 100644 --- a/library/copper.h +++ b/library/copper.h @@ -9,7 +9,7 @@ struct _copper_cash { - struct _copper_cash *self; + struct _copper_cash *me; struct _shape *shape; struct _rect *rect; diff --git a/library/rect.c b/library/rect.c index f2c6a76..b6790e7 100644 --- a/library/rect.c +++ b/library/rect.c @@ -1,24 +1,21 @@ #include "rect.h" -double rect_area(void *self, void *parent) +double rect_area(void *self) { double area = 0; - struct _rect *pthis = (struct _rect *)parent; - area = pthis->shape->vtb.area(self, NULL); + struct _rect *pthis = container_of(self, struct _rect, me); + area = pthis->shape->vtb.area(pthis); return area; } -void rect_draw(void *self, void *parent) +void rect_draw(void *self) { - // 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); + struct _rect *pthis = container_of(self, struct _rect, me); + pthis->shape->vtb.draw(pthis); } -double _rect_area(void *self, void *parent) +double _rect_area(void *self) { struct _rect *pthis = (struct _rect *)self; double area = pthis->width * pthis->height; @@ -27,7 +24,7 @@ double _rect_area(void *self, void *parent) return area; } -void _rect_draw(void *self, void *parent) +void _rect_draw(void *self) { printf("rect draw\n"); } @@ -36,8 +33,8 @@ struct _rect *rect_new(double width, double height) { // super static struct _virtual_table vtb = { - .area = (double (*)(void *self, void *parent))_rect_area, - .draw = (void (*)(void *self, void *parent))_rect_draw, + .area = (double (*)(void *self))_rect_area, + .draw = (void (*)(void *self))_rect_draw, }; struct _shape *shape = shape_new(); @@ -53,7 +50,7 @@ struct _rect *rect_new(double width, double height) { return NULL; } - rect->self = rect; + rect->me = rect; rect->width = width; rect->height = height; diff --git a/library/rect.h b/library/rect.h index 3cd93c1..fd2509b 100644 --- a/library/rect.h +++ b/library/rect.h @@ -8,7 +8,7 @@ struct _rect { // int invalid; - struct _rect *self; + struct _rect *me; // super struct _shape *shape; @@ -19,7 +19,7 @@ struct _rect struct _rect *rect_new(double width, double height); -double rect_area(void *self, void *parent); -void rect_draw(void *self, void *parent); +double rect_area(void *self); +void rect_draw(void *self); #endif diff --git a/library/shape.c b/library/shape.c index 1515c14..7cbc702 100644 --- a/library/shape.c +++ b/library/shape.c @@ -1,12 +1,12 @@ #include "shape.h" -double virtual_area(void *self, void *parent) +double virtual_area(void *self) { ASSERT_CALL_VIRTUAL_METHOD(); } -void virtual_draw(void *self, void *parent) +void virtual_draw(void *self) { ASSERT_CALL_VIRTUAL_METHOD(); } @@ -25,26 +25,26 @@ struct _virtual_table *virtual_new(void) -double shape_area(void *self, void *parent) +double shape_area(void *self) { double area = 0; - struct _shape *pthis = (struct _shape *)parent; - area = pthis->vtb.area(self, NULL); + struct _shape *pthis = container_of(self, struct _shape, me); + area = pthis->vtb.area(pthis); return area; } -void shape_draw(void *self, void *parent) +void shape_draw(void *self) { - struct _shape *pthis = (struct _shape *)parent; - pthis->vtb.draw(self, NULL); + struct _shape *pthis = container_of(self, struct _shape, me); + pthis->vtb.draw(pthis); } -PRIVATE double _shape_area(void *self, void *parent) +PRIVATE double _shape_area(void *self) { ASSERT_CALL_VIRTUAL_METHOD(); } -PRIVATE void _shape_draw(void *self, void *parent) +PRIVATE void _shape_draw(void *self) { ASSERT_CALL_VIRTUAL_METHOD(); } @@ -56,11 +56,11 @@ struct _shape *shape_new(void) { return NULL; } - shape->self = shape; + shape->me = shape; static struct _virtual_table vtb = { - .area = (double (*)(void *self, void *parent))_shape_area, - .draw = (void (*)(void *self, void *parent))_shape_draw + .area = (double (*)(void *self))_shape_area, + .draw = (void (*)(void *self))_shape_draw }; shape->vtb = vtb; return shape; diff --git a/library/shape.h b/library/shape.h index 2b4cb95..4ce4857 100644 --- a/library/shape.h +++ b/library/shape.h @@ -11,13 +11,13 @@ struct _virtual_table { - VIRTUAL double (*area)(void *self, void *parent); - VIRTUAL void (*draw)(void *self, void *parent); + VIRTUAL double (*area)(void *self); + VIRTUAL void (*draw)(void *self); }; struct _shape { - struct _shape *self; + struct _shape *me; struct _virtual_table vtb; }; @@ -25,8 +25,8 @@ struct _shape struct _shape *shape_new(void); -double shape_area(void *self, void *parent); -void shape_draw(void *self, void *parent); +double shape_area(void *self); +void shape_draw(void *self); #endif diff --git a/src/main.c b/src/main.c index 5970521..413f5b9 100644 --- a/src/main.c +++ b/src/main.c @@ -7,20 +7,21 @@ int main(int argc, char *argv[]) { struct _rect *rect = rect_new(4, 5); - rect_area(rect, rect); - rect_draw(rect, rect); + rect_area(rect); + rect_draw(rect); - struct _circle *circle = circle_new(3); - circle_area(circle, circle); - circle_draw(circle, circle); + struct _circle *circle = circle_new(2); + circle_area(circle); + circle_draw(circle); printf("----- rect & circle -----\n"); - shape_area(rect, rect->shape); - shape_draw(rect, rect->shape); + shape_area(rect->shape); + shape_draw(rect->shape); - shape_area(circle, circle->shape); - shape_draw(circle, circle->shape); + shape_area(circle->shape); + shape_draw(circle->shape); +#if 0 printf("----- [2] rect & circle -----\n"); struct _rect *rect2 = (struct _rect *)rect_new(2, 3); shape_area(rect2, rect2->shape); @@ -46,6 +47,6 @@ int main(int argc, char *argv[]) circle_area(cc, cc->circle); circle_draw(cc, cc->circle); - +#endif return 0; }