cppintf: with param

This commit is contained in:
xushiwei
2024-06-22 02:39:29 +08:00
parent bfa4e08a4e
commit 26b771f9f9
3 changed files with 13 additions and 13 deletions

View File

@@ -3,23 +3,23 @@ package main
import (
"github.com/goplus/llgo/_demo/cppintf/foo"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/math"
)
type Bar struct {
foo.Callback
a int
b float64
}
func NewBar(a int, b float64) *Bar {
func NewBar(a int) *Bar {
return &Bar{
Callback: foo.Callback{
Vptr: &foo.CallbackVtbl{
ValA: c.Func((*Bar).getA),
ValB: c.Func((*Bar).getB),
Val: c.Func((*Bar).getA),
Calc: c.Func((*Bar).sqrt),
},
},
a: a, b: b,
a: a,
}
}
@@ -27,11 +27,11 @@ func (p *Bar) getA() int {
return p.a
}
func (p *Bar) getB() float64 {
return p.b
func (p *Bar) sqrt(v float64) float64 {
return math.Sqrt(v)
}
func main() {
bar := NewBar(1, 2.0)
bar := NewBar(1)
foo.F(&bar.Callback)
}

View File

@@ -2,10 +2,10 @@
#define interface struct
interface ICallback {
virtual int valA() = 0;
virtual double valB() = 0;
virtual int val() = 0;
virtual double calc(double v) = 0;
};
extern "C" void f(ICallback* cb) {
printf("Hello %d, %lf!\n", cb->valA(), cb->valB());
printf("val: %d\ncalc(2): %lf\n", cb->val(), cb->calc(2));
}

View File

@@ -14,8 +14,8 @@ type Callback struct {
}
type CallbackVtbl struct {
ValA unsafe.Pointer
ValB unsafe.Pointer
Val unsafe.Pointer
Calc unsafe.Pointer
}
//go:linkname F C.f