Skip to content

Instantly share code, notes, and snippets.

@be9
Created September 26, 2012 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save be9/3786499 to your computer and use it in GitHub Desktop.
Save be9/3786499 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include "circle.h"
#include "new.h"
static void *Circle_ctor(void *_self, va_list *app) {
struct Circle *self = ((const struct Class *)Point)->ctor(_self, app);
self->rad = va_arg(*app, int);
return self;
}
#define x(p) (((const struct Point *)(p)) -> x)
#define y(p) (((const struct Point *)(p)) -> y)
static void Circle_draw(const void * _self)
{
const struct Circle * self = _self;
printf("circle at %d,%d rad %d\n", x(self), y(self), self->rad);
}
static const struct Class _Circle = {
sizeof(struct Circle), // size
Circle_ctor, // ctor
0, // dtor
Circle_draw // draw
};
const void *Circle = &_Circle;
#ifndef __circle_h
#define __circle_h
#include "point.h"
struct Circle {
const struct Point _;
int rad;
};
extern const void *Circle;
#endif
#include "point.h"
#include "circle.h"
#include "new.h"
int main(int argc, char **argv)
{
void *p;
while (*++argv) {
switch (**argv) {
case 'p':
p = new(Point, 1, 2);
break;
case 'c':
p = new(Circle, 1, 2, 3);
break;
default:
continue;
}
draw(p);
move(p, 10, 20);
draw(p);
delete(p);
}
return 0;
}
#include <stdlib.h>
#include <assert.h>
#include "new.h"
void *new(const void *_class, ...)
{
const struct Class *class = _class;
void *p = calloc(1, class->size);
assert(p);
*(const struct Class **)p = class;
if (class->ctor) {
va_list ap;
va_start(ap, _class);
p = class->ctor(p, &ap);
va_end(ap);
}
return p;
}
void delete(void *self)
{
const struct Class **cp = self;
if (self && *cp && (*cp)->dtor)
self = (*cp)->dtor(self);
free(self);
}
void draw(const void *self)
{
const struct Class * const *cp = self;
assert(self && *cp && (*cp)->draw);
(*cp)->draw(self);
}
#ifndef __new_h
#define __new_h
#include <stddef.h>
#include <stdarg.h>
struct Class {
size_t size;
void *(*ctor)(void *self, va_list *app);
void *(*dtor)(void *self);
void (*draw)(const void *self);
};
void *new(const void *class, ...);
void delete(void *item);
void draw(const void *self);
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
#include "new.h"
void move(void * _self, int dx, int dy)
{
struct Point * self = _self;
self->x += dx;
self->y += dy;
}
static void *Point_ctor(void *_self, va_list *app)
{
struct Point *self = _self;
self->x = va_arg(*app, int);
self->y = va_arg(*app, int);
return self;
}
static void Point_draw(const void *_self)
{
const struct Point *self = _self;
printf("\".\" at %d,%d\n", self->x, self->y);
}
static const struct Class _Point = {
sizeof(struct Point), // size
Point_ctor, // ctor
0, // dtor
Point_draw // draw
};
const void *Point = &_Point;
#ifndef __point_h
#define __point_h
struct Point {
const void *class;
int x, y; /* координаты */
};
void move(void *_self, int dx, int dy);
extern const void *Point;
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment