Creating very nice C API

This commit is contained in:
Kolan Sh 2012-05-02 18:25:52 +04:00
parent 14b295c886
commit 079b09287d
9 changed files with 210 additions and 346 deletions

View File

@ -1,106 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include "ColoredSquare.h"
void ColoredSquare_constructor (ColoredSquare *this,
double a,
int color)
{
printf ("ColoredSquare_constructor (%lu, %f, %d) called\n",
(unsigned long) this,
a,
color);
Square_constructor ((Square *) this, a);
this->color = color;
}
void ColoredSquare_copy (ColoredSquare *dest, const ColoredSquare *src)
{
printf ("ColoredSquare_copy (%lu, %lu) called\n",
(unsigned long) dest,
(unsigned long) src);
Square_copy ((Square *) dest, (Square *) src);
dest->color = src->color;
}
ColoredSquare* ColoredSquare_clone (const ColoredSquare *this)
{
ColoredSquare *csquare = ColoredSquare_new (0.0, 0);
printf ("ColoredSquare_clone (%lu) called\n",
(unsigned long) this);
ColoredSquare_copy (csquare, this);
printf ("ColoredSquare_clone (%lu) returns %lu\n",
(unsigned long) this,
(unsigned long) csquare);
return csquare;
}
const char* ColoredSquare_type (const ColoredSquare *this)
{
static const char *csquare_type_str = "ColoredSquare";
printf ("ColoredSquare_type (%lu) called\n",
(unsigned long) this);
printf ("ColoredSquare_type (%lu) returns \"%s\"\n",
(unsigned long) this,
csquare_type_str);
return csquare_type_str;
}
void ColoredSquare_draw (const ColoredSquare *this)
{
printf ("ColoredSquare_draw (%lu) called\n",
(unsigned long) this);
printf ("Drawing ColoredSquare with %f side and %d color\n",
this->a,
this->color);
}
void ColoredSquare_set_color (ColoredSquare *this, int color)
{
printf ("ColoredSquare_draw (%lu, %d) called\n",
(unsigned long) this,
color);
this->color = color;
}
/* public */
ColoredSquare* ColoredSquare_new (double a, int color)
{
static ColoredSquare_interface vtable =
{
{
{
{
(void* (*) (const void *)) ColoredSquare_clone,
(void (*) (void *)) Square_destroy
},
(const char* (*) (const Figure *)) ColoredSquare_type,
(void (*) (const Figure *)) ColoredSquare_draw,
(double (*) (const Figure *)) Square_area
},
Square_resize,
Square_diag_length
},
ColoredSquare_set_color
};
ColoredSquare *square = malloc (sizeof (*square));
ColoredSquare_constructor (square, a, color);
printf ("ColoredSquare_new (%f, %d) returns %lu\n",
a,
color,
(unsigned long) square);
square->vtable = &vtable;
/*goto end;
err:
end:*/
return square;
}

View File

@ -1,44 +0,0 @@
#ifndef __COLORED_SQUARE_H__
#define __COLORED_SQUARE_H__
#include "Square.h"
typedef struct ColoredSquare_data
{
Square_data;
int color;
} ColoredSquare_data;
struct ColoredSquare;
typedef struct ColoredSquare_interface
{
Square_interface;
void (*set_color) (struct ColoredSquare *this, int color);
} ColoredSquare_interface;
void ColoredSquare_constructor (struct ColoredSquare *this,
double a,
int color);
void ColoredSquare_copy (struct ColoredSquare *dest, const struct ColoredSquare *src);
struct ColoredSquare* ColoredSquare_clone (const struct ColoredSquare *this);
const char* ColoredSquare_type (const struct ColoredSquare *this);
void ColoredSquare_draw (const struct ColoredSquare *this);
void ColoredSquare_set_color (struct ColoredSquare *this, int color);
/* public */
typedef struct ColoredSquare
{
ColoredSquare_interface *vtable;
ColoredSquare_data;
} ColoredSquare;
ColoredSquare* ColoredSquare_new (double a, int color);
#endif // __COLORED_SQUARE_H__

17
Figure.c Normal file
View File

@ -0,0 +1,17 @@
#include "Figure.h"
/* considered to be protected */
void Figure_constructor (Figure *this)
{
// nothin here
}
void Figure_destructor (Figure *this)
{
// nothin here
}
void Figure_copy (struct Figure *dest, const struct Figure *src)
{
// nothin here
}

View File

@ -3,22 +3,47 @@
#include "Object.h"
struct Figure;
typedef struct Figure_interface
{
Object_interface;
const char* (*type) (const struct Figure *this);
void (*draw) (const struct Figure *this);
double (*area) (const struct Figure *this);
} Figure_interface;
typedef struct Figure
{
Figure_interface *vtable;
struct Figure_vtable
{
/* derived from Object */
const char* (*type) ();
struct Figure* (*clone) (const struct Figure *this);
void (*destroy) (struct Figure *this);
/* Figure virtual methods */
void (*draw) (const struct Figure *this);
double (*area) (const struct Figure *this);
} *vtable;
} Figure;
static inline const char* Figure_type (const Figure *this)
{
return this->vtable->type ();
}
static inline Figure* Figure_clone (const Figure *this)
{
return this->vtable->clone (this);
}
static inline void Figure_destroy (Figure *this)
{
this->vtable->destroy (this);
}
static inline void Figure_draw (const struct Figure *this)
{
this->vtable->draw (this);
}
static inline double (*area) (const struct Figure *this)
{
return this->vtable->area (this);
}
/* considered to be protected */
void Figure_constructor (Figure *this);
void Figure_destructor (Figure *this);
void Figure_copy (struct Figure *dest, const struct Figure *src);
#endif // __FIGURE_H__

View File

@ -1,11 +1,17 @@
#ifndef __OBJECT_H__
#define __OBJECT_H__
typedef struct Object_interface
typedef struct Object
{
void* (*clone) (const void *this);
void (*destroy) (void *this);
struct Object_vtable
{
/* Object methods */
const char* (*type) ();
struct Object* (*clone) (const Object *this);
void (*destroy) (struct Object *this);
} *vtable;
} Object_interface;
} Object;
#endif // __OBJECT_H__

70
Polygon.h Normal file
View File

@ -0,0 +1,70 @@
#ifndef __POLYGON_H__
#define __POLYGON_H__
#include "Figure.h"
typedef struct Polygon
{
struct Polygon_vtable
{
/* derived from Object */
const char* (*type) ();
struct Polygon* (*clone) (const struct Polygon *this);
void (*destroy) (struct Polygon *this);
/* derived from Figure */
void (*draw) (const struct Polygon *this);
double (*area) (const struct Polygon *this);
/* Polygon virtual methods */
double (*max_diag) (const struct Polygon *this);
} *vtable;
/* Polygon fields */
struct point
{
double x, y;
} *points;
} Polygon;
Polygon* Polygon_new (const struct *points);
static inline const char* Polygon_type (const Polygon *this)
{
return this->vtable->type ();
}
static inline Polygon* Polygon_clone (const Polygon *this)
{
return this->vtable->clone (this);
}
static inline void Polygon_destroy (Polygon *this)
{
this->vtable->destroy (this);
}
static inline void Polygon_draw (const Polygon *this)
{
this->vtable->draw (this);
}
static inline double Polygon_area (const Polygon *this)
{
return this->vtable->area (this);
}
static inline double Polygon_max_diag (const Polygon *this)
{
return this->max_diag (this);
}
/* considered to be protected */
void Polygon_constructor (Polygon *this);
void Polygon_destructor (Polygon *this);
void Polygon_copy (Polygon *dest, const Polygon *src);
#endif // __POLYGON_H__

75
Rhomb.h Normal file
View File

@ -0,0 +1,75 @@
#ifndef __RHOMB_H__
#define __RHOMB_H__
#include "Polygon.h"
typedef struct Rhomb
{
struct Rhomb_vtable
{
/* derived from Object */
const char* (*type) ();
struct Rhomb* (*clone) (const struct Rhomb *this);
void (*destroy) (struct Rhomb *this);
/* derived from Figure */
void (*draw) (const struct Rhomb *this);
double (*area) (const struct Rhomb *this);
/* derived from Polygon */
double (*max_diag) (const struct Rhomb *this);
/* Rhomb virtual methods */
int (*is_square) (const struct Rhomb *this);
} *vtable;
/* derived from Polygon */
struct point
{
double x, y;
} *points;
} Rhomb;
Rhomb* Rhomb_new (const struct *points);
static inline const char* Rhomb_type (const Rhomb *this)
{
return this->vtable->type ();
}
static inline Rhomb* Rhomb_clone (const Rhomb *this)
{
return this->vtable->clone (this);
}
static inline void Rhomb_destroy (Rhomb *this)
{
this->vtable->destroy (this);
}
static inline void Rhomb_draw (const Rhomb *this)
{
this->vtable->draw (this);
}
static int double Rhomb_area (const Rhomb *this)
{
return this->vtable_area (this);
}
static inline double Rhomb_max_diag (const Rhomb *this)
{
return this->max_diag (this);
}
static inline int Rhomb_is_square (const Rhomb *this);
/* considered to be protected */
void Rhomb_constructor (Rhomb *this);
void Rhomb_destructor (Rhomb *this);
void Rhomb_copy (Rhomb *dest, const Rhomb *src);
#endif // __RHOMB_H__

134
Square.c
View File

@ -1,134 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include "Square.h"
void Square_constructor (Square *this, double a)
{
printf ("Square_constructor (%lu, %f) called\n",
(unsigned long) this,
a);
this->a = a;
}
void Square_destructor (Square *this)
{
printf ("Square_destructor (%lu) called\n",
(unsigned long) this);
}
void Square_copy (Square *dest, const Square *src)
{
printf ("Square_copy (%lu, %lu) called\n",
(unsigned long) dest,
(unsigned long) src);
dest->a = src->a;
}
Square* Square_clone (const Square *this)
{
Square *square = Square_new (0.0);
printf ("Square_clone (%lu) called\n",
(unsigned long) this);
Square_copy (square, this);
printf ("Square_clone (%lu) returns %lu\n",
(unsigned long) this,
(unsigned long) square);
return square;
}
void Square_destroy (Square *this)
{
printf ("Square_destroy (%lu) called\n",
(unsigned long) this);
free (this);
}
const char* Square_type (const Square *this)
{
static const char *square_type_str = "Square";
printf ("Square_type (%lu) called\n",
(unsigned long) this);
printf ("Square_type (%lu) returns \"%s\"\n",
(unsigned long) this,
square_type_str);
return square_type_str;
}
void Square_draw (const Square *this)
{
printf ("Square_draw (%lu) called\n",
(unsigned long) this);
printf ("Drawing Square with %f side\n",
this->a);
}
double Square_area (const Square *this)
{
double area = this->a * this->a;
printf ("Square_area (%lu) called\n",
(unsigned long) this);
printf ("Square_area (%lu) returns %f\n",
(unsigned long) this,
area);
return area;
}
void Square_resize (Square *this, double a)
{
printf ("Square_resize (%lu, %f) called\n",
(unsigned long) this,
a);
this->a = a;
}
double Square_diag_length (const Square *this)
{
double diag_length = ((Square *)this)->a * 1.41421356;
printf ("Square_diag_length (%lu) called\n",
(unsigned long) this);
printf ("Square_diag_length (%lu) returns %f\n",
(unsigned long) this,
diag_length);
return diag_length;
}
/* public */
Square* Square_new (double a)
{
static Square_interface vtable =
{
{
{
(void* (*) (const void *)) Square_clone,
(void (*) (void *)) Square_destroy
},
(const char* (*) (const Figure *)) Square_type,
(void (*) (const Figure *)) Square_draw,
(double (*) (const Figure *)) Square_area
},
Square_resize,
Square_diag_length
};
Square *square = malloc (sizeof (*square));
Square_constructor (square, a);
printf ("Square_new (%f) returns %lu\n",
a,
(unsigned long) square);
square->vtable = &vtable;
/*goto end;
err:
end:*/
return square;
}

View File

@ -1,45 +0,0 @@
#ifndef __SQUARE_H__
#define __SQUARE_H__
#include "Figure.h"
typedef struct Square_data
{
double a;
} Square_data;
struct Square;
typedef struct Square_interface
{
Figure_interface;
void (*resize) (struct Square *this, double a);
double (*diag_length) (const struct Square *this);
} Square_interface;
void Square_constructor (struct Square *this, double a);
void Square_destructor (struct Square *this);
void Square_copy (struct Square *dest, const struct Square *src);
struct Square* Square_clone (const struct Square *this);
void Square_destroy (struct Square *this);
const char* Square_type (const struct Square *this);
void Square_draw (const struct Square *this);
double Square_area (const struct Square *this);
void Square_resize (struct Square *this, double a);
double Square_diag_length (const struct Square *this);
/* public */
typedef struct Square
{
Square_interface *vtable;
Square_data;
} Square;
Square* Square_new (double a);
#endif // __SQUARE_H__