I have a small pet project on this topic. It supports encapsulation, single inheritance, and polymorphism. If someone is still interested here is the link to the repo: https://github.com/alexmarincu/Cbject
And here is an example of the syntax:
Shape.h:
#ifndef SHAPE_H
#define SHAPE_H
#include "../Cbj/Cbj.h" // includes Cbject (base object) and macros
#include "Point.h"
#define Type Shape
#define Parent Cbject
AbstractClass(
Params(_, Point origin),
Props(_, Point origin),
VirtFuns(_,
(float, area, (0)),
(void, draw, (_, uint8 const a))));
Setters(_, (Point, origin));
Getters(_, (Point, origin));
SuperFun(void, draw, (_, uint8 const a));
#undef Parent
#undef Type
#endif // SHAPE_H
Shape.c:
#include "Shape.h"
#define Type Shape
#define Parent Cbject
AbstractClassSetup(
VirtFunCalls(_,
(float, area, (0), (0)),
(void, draw, (_, uint8 const a), (_, a))),
BindFuns(_,
(void, Shape, draw, (_, uint8 const a))));
Init { me->p.origin = params->origin; }
Terminate {}
DefaultSet(Point, origin);
DefaultGet(Point, origin);
SuperFun(void, draw, (_, uint8 const a))
{
// Default implementation of draw function
}
#undef Parent
#undef Type
Circle.h:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Shape.h"
#define Type Circle
#define Parent Shape
Class(
Params(_,
Point origin,
uint32 radius),
Props(_, int32 radius),
VirtFuns(0));
Set(uint32, radius);
Get(uint32, radius);
SuperFuns(_,
(float, area, (0)),
(void, draw, (_, uint8 const a)));
#undef Type
#undef Parent
#endif // CIRCLE_H
Circle.c:
#include "Circle.h"
#define Type Circle
#define Parent Shape
ClassSetup(
VirtFunCalls(_, (void, rotate, (0), (0))),
BindFuns(_,
(float, Shape, area, (0)),
(void, Shape, draw, (_, uint8 const a))));
PrivateConst(float, pi = 3.14);
Init
{
*s_params = (ShapeParams){
.origin.x = params->origin.x,
.origin.y = params->origin.y};
me->p.radius = params->radius;
}
Terminate {}
DefaultSet(uint32, radius);
DefaultGet(uint32, radius);
SuperFun(void, draw, (_, uint8 const a))
{
s_Shape_draw((Shape *) me, a); // Calling overridden function from parent
// Specific implementation of draw function
}
SuperFun(float, area, (0)) { return me->p.radius * me->p.radius * Circle_pi; } // Specific implementation of area function
#undef Parent
#undef Type
Usage:
...
{
Circle * circle = New_Circle(&((CircleParams){.origin.x = 0, .origin.y = 1, .radius = 1})); // create circle
uint32 radius = Circle_radius(circle)); // get radius
Circle_radiusSet(circle, 2); // set radius
float area = Shape_area(Circle_toShape(circle))); // polymorphic call
Delete_Circle(circle);
}
No documentation available yet, but the examples should be fairly easy to understand.