Skip to content

Commit

Permalink
testing inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
lassade committed Jun 9, 2023
1 parent a8c0ad6 commit 45bd0e5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ inspeired by this [article](https://floooh.github.io/2020/08/23/sokol-bindgen.ht

`zig cc -x c++ -std=c++11 -Xclang -ast-dump=json {input_file}`

- ctors and dtors
- parse fn ptr function pointers should be decorated with `callconv(.C)`
- write opaques if they didn't get defined at the end of the file
- use `@compileError` for objects that couldn't be transpiled
- virtual functions and vtable
- auto add `vtable: *const anyopaque,` in needed types
- static methods inside classes
- peform layout validation

test with: other libraries from: https://github.com/godotengine/godot/tree/master/modules
Expand Down
2 changes: 1 addition & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn build(b: *std.Build) void {
// smol_lib.addIncludePath("./use_cases/common_cases/include");
// //smol_lib.linkLibC();
// smol_lib.linkLibCpp();
// smol_lib.addCSourceFile("./use_cases/common_cases/include/c002_cpp_structs.cpp", cflags);
// smol_lib.addCSourceFile("./use_cases/common_cases/include/c005_inheritance.cpp", cflags);
// exe.linkLibrary(smol_lib);

// This declares intent for the executable to be installed into the
Expand Down
6 changes: 6 additions & 0 deletions use_cases/common_cases/c005_inheritance.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,9 @@ pub const circle_t = extern struct {
extern fn _ZNK8circle_t4areaEv(self: *const circle_t) f32;
pub const area = _ZNK8circle_t4areaEv;
};

extern fn _Z6circlef(radius: f32) circle_t;
pub const circle = _Z6circlef;

extern fn _Z4areaRK7shape_t(shape: [*c]const shape_t) f32;
pub const area = _Z4areaRK7shape_t;
17 changes: 17 additions & 0 deletions use_cases/common_cases/include/c005_inheritance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "c005_inheritance.h"

circle_t::circle_t(float radius) {
this->radius = radius;
}

float circle_t::area() const {
return 3.14 * radius * radius;
}

circle_t circle(float radius) {
return circle_t(radius);
}

float area(const shape_t &shape) {
return shape.area();
}
12 changes: 8 additions & 4 deletions use_cases/common_cases/include/c005_inheritance.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ struct vec2_t {
float y;
};

class shape_t {
struct shape_t {
public:
vec2_t aabb;
virtual float area() const = 0;
};

class circle_t: public shape_t {
struct circle_t: public shape_t {
public:
virtual float area() const = 0;
circle_t(float radius);
virtual float area() const;

private:
float radius;
};
};

circle_t circle(float radius);
float area(const shape_t &shape);

0 comments on commit 45bd0e5

Please sign in to comment.