cmake_demo/library/shape.c

76 lines
1.4 KiB
C

#include "shape.h"
double virtual_area(void *self, void *parent)
{
ASSERT_CALL_VIRTUAL_METHOD();
}
void virtual_draw(void *self, void *parent)
{
ASSERT_CALL_VIRTUAL_METHOD();
}
struct _virtual_table *virtual_new(void)
{
struct _virtual_table *vtb = calloc(1, sizeof(struct _virtual_table));
if (vtb == NULL)
{
return NULL;
}
vtb->area = virtual_area;
vtb->draw = virtual_draw;
return vtb;
}
double shape_area(void *self, void *parent)
{
double area = 0;
struct _shape *pthis = (struct _shape *)parent;
area = pthis->vtb.area(self, NULL);
return area;
}
void shape_draw(void *self, void *parent)
{
struct _shape *pthis = (struct _shape *)parent;
pthis->vtb.draw(self, NULL);
}
PRIVATE double _shape_area(void *self, void *parent)
{
ASSERT_CALL_VIRTUAL_METHOD();
}
PRIVATE void _shape_draw(void *self, void *parent)
{
ASSERT_CALL_VIRTUAL_METHOD();
}
struct _shape *shape_new(void)
{
struct _shape *shape = calloc(1, sizeof(struct _shape));
if(shape == NULL)
{
return NULL;
}
static struct _virtual_table vtb = {
.area = (double (*)(void *self, void *parent))_shape_area,
.draw = (void (*)(void *self, void *parent))_shape_draw
};
shape->vtb = vtb;
return shape;
}
#if 0
void shape_free(struct _shape * self)
{
if (self != NULL)
{
free(self);
}
}
#endif