cmake_demo/library/circle.c

59 lines
1.2 KiB
C

#include "circle.h"
double circle_area(void *self)
{
double area = 0;
struct _circle *pthis = container_of(self, struct _circle, me);
area = pthis->shape->vtb.area(pthis);
return area;
}
void circle_draw(void *self)
{
struct _circle *pthis = container_of(self, struct _circle, me);
pthis->shape->vtb.draw(pthis);
}
double _circle_area(void *self)
{
struct _circle *pthis = (struct _circle *)self;
double area = 3.14 * pthis->radius * pthis->radius;
printf("circle area = %0.2f\n", area);
return area;
}
void _circle_draw(void *self, void *parent)
{
printf("circle draw\n");
}
struct _circle *circle_new(double radius)
{
// super
static struct _virtual_table vtb = {
.area = (double (*)(void *self))_circle_area,
.draw = (void (*)(void *self))_circle_draw,
};
struct _shape *shape = shape_new();
if (shape == NULL)
{
return NULL;
}
shape->vtb = vtb;
// self
struct _circle *circle = calloc(1, sizeof(struct _circle));
if (circle == NULL)
{
return NULL;
}
circle->me = circle;
circle->radius = radius;
// set parent
circle->shape = shape;
return circle;
}