Correct types instead of void *

This commit is contained in:
Kolan Sh 2012-05-01 00:55:56 +04:00
parent 9821908cb0
commit 2f3417cf85
6 changed files with 60 additions and 54 deletions

View File

@ -3,7 +3,7 @@
#include "ColoredSquare.h" #include "ColoredSquare.h"
void ColoredSquare_constructor (void *this, void ColoredSquare_constructor (ColoredSquare *this,
double a, double a,
int color) int color)
{ {
@ -11,20 +11,20 @@ void ColoredSquare_constructor (void *this,
(unsigned long) this, (unsigned long) this,
a, a,
color); color);
Square_constructor (this, a); Square_constructor ((Square *) this, a);
((ColoredSquare *) this)->color = color; ((ColoredSquare *) this)->color = color;
} }
void ColoredSquare_copy (void *to, void *from) void ColoredSquare_copy (ColoredSquare *to, ColoredSquare *from)
{ {
printf ("ColoredSquare_copy (%lu, %lu) called\n", printf ("ColoredSquare_copy (%lu, %lu) called\n",
(unsigned long) to, (unsigned long) to,
(unsigned long) from); (unsigned long) from);
Square_copy (to, from); Square_copy ((Square *) to, (Square *) from);
((ColoredSquare *) to)->color = ((ColoredSquare *) from)->color; ((ColoredSquare *) to)->color = ((ColoredSquare *) from)->color;
} }
void* ColoredSquare_clone (void *this) ColoredSquare* ColoredSquare_clone (ColoredSquare *this)
{ {
ColoredSquare *csquare = ColoredSquare_new (0.0, 0); ColoredSquare *csquare = ColoredSquare_new (0.0, 0);
printf ("ColoredSquare_clone (%lu) called\n", printf ("ColoredSquare_clone (%lu) called\n",
@ -37,7 +37,7 @@ void* ColoredSquare_clone (void *this)
return csquare; return csquare;
} }
const char* ColoredSquare_type (void *this) const char* ColoredSquare_type (ColoredSquare *this)
{ {
static const char *csquare_type_str = "ColoredSquare"; static const char *csquare_type_str = "ColoredSquare";
printf ("ColoredSquare_type (%lu) called\n", printf ("ColoredSquare_type (%lu) called\n",
@ -48,7 +48,7 @@ const char* ColoredSquare_type (void *this)
return csquare_type_str; return csquare_type_str;
} }
void ColoredSquare_draw (void *this) void ColoredSquare_draw (ColoredSquare *this)
{ {
printf ("ColoredSquare_draw (%lu) called\n", printf ("ColoredSquare_draw (%lu) called\n",
(unsigned long) this); (unsigned long) this);
@ -57,7 +57,7 @@ void ColoredSquare_draw (void *this)
((ColoredSquare *) this)->color); ((ColoredSquare *) this)->color);
} }
void ColoredSquare_set_color (void *this, int color) void ColoredSquare_set_color (ColoredSquare *this, int color)
{ {
printf ("ColoredSquare_draw (%lu, %d) called\n", printf ("ColoredSquare_draw (%lu, %d) called\n",
(unsigned long) this, (unsigned long) this,
@ -67,7 +67,7 @@ void ColoredSquare_set_color (void *this, int color)
} }
/* public */ /* public */
void* ColoredSquare_new (double a, int color) ColoredSquare* ColoredSquare_new (double a, int color)
{ {
static ColoredSquare_interface vtable = static ColoredSquare_interface vtable =
{ {
@ -89,7 +89,7 @@ void* ColoredSquare_new (double a, int color)
color, color,
(unsigned long) square); (unsigned long) square);
square->vtable = (void *) &vtable; square->vtable = (ColoredSquare_interface *) &vtable;
/*goto end; /*goto end;
err: err:

View File

@ -11,22 +11,24 @@ typedef struct ColoredSquare_data
} ColoredSquare_data; } ColoredSquare_data;
struct ColoredSquare;
typedef struct ColoredSquare_interface typedef struct ColoredSquare_interface
{ {
Square_interface; Square_interface;
void (*set_color) (void *this, int color); void (*set_color) (struct ColoredSquare *this, int color);
} ColoredSquare_interface; } ColoredSquare_interface;
void ColoredSquare_constructor (void *this, void ColoredSquare_constructor (struct ColoredSquare *this,
double a, double a,
int color); int color);
void ColoredSquare_copy (void *to, void *from); void ColoredSquare_copy (struct ColoredSquare *to, struct ColoredSquare *from);
void* ColoredSquare_clone (void *this); struct ColoredSquare* ColoredSquare_clone (struct ColoredSquare *this);
const char* ColoredSquare_type (void *this); const char* ColoredSquare_type (struct ColoredSquare *this);
void ColoredSquare_draw (void *this); void ColoredSquare_draw (struct ColoredSquare *this);
void ColoredSquare_set_color (void *this, int color); void ColoredSquare_set_color (struct ColoredSquare *this, int color);
/* public */ /* public */
typedef struct ColoredSquare typedef struct ColoredSquare
@ -37,6 +39,6 @@ typedef struct ColoredSquare
} ColoredSquare; } ColoredSquare;
void* ColoredSquare_new (double a, int color); ColoredSquare* ColoredSquare_new (double a, int color);
#endif // __COLORED_SQUARE_H__ #endif // __COLORED_SQUARE_H__

View File

@ -5,13 +5,15 @@
#include "Object.h" #include "Object.h"
struct Figure;
typedef struct Figure_interface typedef struct Figure_interface
{ {
Object_interface; Object_interface;
const char* (*type) (void *this); const char* (*type) (struct Figure *this);
void (*draw) (void *this); void (*draw) (struct Figure *this);
double (*area) (void *this); double (*area) (struct Figure *this);
} Figure_interface; } Figure_interface;

View File

@ -2,7 +2,7 @@
#include "Square.h" #include "Square.h"
void Square_constructor (void *this, double a) void Square_constructor (Square *this, double a)
{ {
printf ("Square_constructor (%lu, %f) called\n", printf ("Square_constructor (%lu, %f) called\n",
(unsigned long) this, (unsigned long) this,
@ -10,13 +10,13 @@ void Square_constructor (void *this, double a)
((Square *) this)->a = a; ((Square *) this)->a = a;
} }
void Square_destructor (void *this) void Square_destructor (Square *this)
{ {
printf ("Square_destructor (%lu) called\n", printf ("Square_destructor (%lu) called\n",
(unsigned long) this); (unsigned long) this);
} }
void Square_copy (void *to, void *from) void Square_copy (Square *to, Square *from)
{ {
printf ("Square_copy (%lu, %lu) called\n", printf ("Square_copy (%lu, %lu) called\n",
(unsigned long) to, (unsigned long) to,
@ -24,7 +24,7 @@ void Square_copy (void *to, void *from)
((Square *) to)->a = ((Square *) from)->a; ((Square *) to)->a = ((Square *) from)->a;
} }
void* Square_clone (void *this) Square* Square_clone (Square *this)
{ {
Square *square = Square_new (0.0); Square *square = Square_new (0.0);
printf ("Square_clone (%lu) called\n", printf ("Square_clone (%lu) called\n",
@ -37,14 +37,14 @@ void* Square_clone (void *this)
return square; return square;
} }
void Square_destroy (void *this) void Square_destroy (Square *this)
{ {
printf ("Square_destroy (%lu) called\n", printf ("Square_destroy (%lu) called\n",
(unsigned long) this); (unsigned long) this);
free (this); free (this);
} }
const char* Square_type (void *this) const char* Square_type (Square *this)
{ {
static const char *square_type_str = "Square"; static const char *square_type_str = "Square";
printf ("Square_type (%lu) called\n", printf ("Square_type (%lu) called\n",
@ -56,7 +56,7 @@ const char* Square_type (void *this)
return square_type_str; return square_type_str;
} }
void Square_draw (void *this) void Square_draw (Square *this)
{ {
printf ("Square_draw (%lu) called\n", printf ("Square_draw (%lu) called\n",
(unsigned long) this); (unsigned long) this);
@ -64,7 +64,7 @@ void Square_draw (void *this)
((Square *) this)->a); ((Square *) this)->a);
} }
double Square_area (void *this) double Square_area (Square *this)
{ {
double area = ((Square *)this)->a * ((Square *)this)->a; double area = ((Square *)this)->a * ((Square *)this)->a;
printf ("Square_area (%lu) called\n", printf ("Square_area (%lu) called\n",
@ -76,7 +76,7 @@ double Square_area (void *this)
return area; return area;
} }
void Square_resize (void *this, double a) void Square_resize (Square *this, double a)
{ {
printf ("Square_resize (%lu, %f) called\n", printf ("Square_resize (%lu, %f) called\n",
(unsigned long) this, (unsigned long) this,
@ -84,7 +84,7 @@ void Square_resize (void *this, double a)
((Square *) this)->a = a; ((Square *) this)->a = a;
} }
double Square_diag_length (void *this) double Square_diag_length (Square *this)
{ {
double diag_length = ((Square *)this)->a * 1.41421356; double diag_length = ((Square *)this)->a * 1.41421356;
@ -98,7 +98,7 @@ double Square_diag_length (void *this)
} }
/* public */ /* public */
void* Square_new (double a) Square* Square_new (double a)
{ {
static Square_interface vtable = static Square_interface vtable =
{ {
@ -118,7 +118,7 @@ void* Square_new (double a)
a, a,
(unsigned long) square); (unsigned long) square);
square->vtable = (void *) &vtable; square->vtable = (Square_interface *) &vtable;
/*goto end; /*goto end;
err: err:

View File

@ -9,25 +9,27 @@ typedef struct Square_data
} Square_data; } Square_data;
struct Square;
typedef struct Square_interface typedef struct Square_interface
{ {
Figure_interface; Figure_interface;
void (*resize) (void *this, double a); void (*resize) (struct Square *this, double a);
double (*diag_length) (void *this); double (*diag_length) (struct Square *this);
} Square_interface; } Square_interface;
void Square_constructor (void *this, double a); void Square_constructor (struct Square *this, double a);
void Square_destructor (void *this); void Square_destructor (struct Square *this);
void Square_copy (void *to, void *from); void Square_copy (struct Square *to, struct Square *from);
void* Square_clone (void *this); struct Square* Square_clone (struct Square *this);
void Square_destroy (void *this); void Square_destroy (struct Square *this);
const char* Square_type (void *this); const char* Square_type (struct Square *this);
void Square_draw (void *this); void Square_draw (struct Square *this);
double Square_area (void *this); double Square_area (struct Square *this);
void Square_resize (void *this, double a); void Square_resize (struct Square *this, double a);
double Square_diag_length (void *this); double Square_diag_length (struct Square *this);
/* public */ /* public */
typedef struct Square typedef struct Square
@ -38,6 +40,6 @@ typedef struct Square
} Square; } Square;
void* Square_new (double a); struct Square* Square_new (double a);
#endif // __SQUARE_H__ #endif // __SQUARE_H__

16
main.c
View File

@ -5,18 +5,18 @@
int main (int argc, char *argv[]) int main (int argc, char *argv[])
{ {
int i = 0; unsigned long i = 0;
Figure *fig[3]; Figure *fig[3];
fig[0] = Square_new (1); fig[0] = (Figure *) Square_new (1);
fig[1] = ColoredSquare_new (2, 2); fig[1] = (Figure *) ColoredSquare_new (2, 2);
((ColoredSquare *) fig[1])->vtable->set_color (fig[1], 5); ((ColoredSquare *) fig[1])->vtable->set_color ((ColoredSquare *) fig[1], 5);
((Square *) fig[0])->vtable->resize (fig[0], 2); ((Square *) fig[0])->vtable->resize ((Square *) fig[0], 2);
((Square *) fig[1])->vtable->resize (fig[0], 3); ((Square *) fig[1])->vtable->resize ((Square *) fig[0], 3);
fig[2] = fig[1]->vtable->clone (fig[1]); fig[2] = fig[1]->vtable->clone (fig[1]);
((ColoredSquare *) fig[2])->vtable->set_color (fig[2], 3); ((ColoredSquare *) fig[2])->vtable->set_color ((ColoredSquare *) fig[2], 3);
for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++) for (i = 0; i < sizeof (fig) / sizeof (Figure *); i++)
{ {
@ -32,7 +32,7 @@ int main (int argc, char *argv[])
printf ("diag_length(%lu) = %f\n", printf ("diag_length(%lu) = %f\n",
(unsigned long) fig[i], (unsigned long) fig[i],
((Square *) fig[i])->vtable->diag_length (fig[i])); ((Square *) fig[i])->vtable->diag_length ((Square *) fig[i]));
} }
puts ("---"); puts ("---");