From 45bd0e5bb8164f37f945aa540002e1c39c85bdd4 Mon Sep 17 00:00:00 2001 From: Felipe Jorge Date: Fri, 9 Jun 2023 19:50:56 -0300 Subject: [PATCH] testing inheritance --- README.md | 4 +++- build.zig | 2 +- use_cases/common_cases/c005_inheritance.zig | 6 ++++++ .../common_cases/include/c005_inheritance.cpp | 17 +++++++++++++++++ .../common_cases/include/c005_inheritance.h | 12 ++++++++---- 5 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 use_cases/common_cases/include/c005_inheritance.cpp diff --git a/README.md b/README.md index b8c54d8..b42a784 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.zig b/build.zig index a50a6d1..09310ee 100644 --- a/build.zig +++ b/build.zig @@ -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 diff --git a/use_cases/common_cases/c005_inheritance.zig b/use_cases/common_cases/c005_inheritance.zig index ee747bf..46f1258 100644 --- a/use_cases/common_cases/c005_inheritance.zig +++ b/use_cases/common_cases/c005_inheritance.zig @@ -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; diff --git a/use_cases/common_cases/include/c005_inheritance.cpp b/use_cases/common_cases/include/c005_inheritance.cpp new file mode 100644 index 0000000..dae0931 --- /dev/null +++ b/use_cases/common_cases/include/c005_inheritance.cpp @@ -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(); +} \ No newline at end of file diff --git a/use_cases/common_cases/include/c005_inheritance.h b/use_cases/common_cases/include/c005_inheritance.h index d7a1b25..810367b 100644 --- a/use_cases/common_cases/include/c005_inheritance.h +++ b/use_cases/common_cases/include/c005_inheritance.h @@ -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; -}; \ No newline at end of file +}; + +circle_t circle(float radius); +float area(const shape_t &shape); \ No newline at end of file