cppintf: with param
This commit is contained in:
@@ -3,23 +3,23 @@ package main
|
|||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/_demo/cppintf/foo"
|
"github.com/goplus/llgo/_demo/cppintf/foo"
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
|
"github.com/goplus/llgo/c/math"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Bar struct {
|
type Bar struct {
|
||||||
foo.Callback
|
foo.Callback
|
||||||
a int
|
a int
|
||||||
b float64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBar(a int, b float64) *Bar {
|
func NewBar(a int) *Bar {
|
||||||
return &Bar{
|
return &Bar{
|
||||||
Callback: foo.Callback{
|
Callback: foo.Callback{
|
||||||
Vptr: &foo.CallbackVtbl{
|
Vptr: &foo.CallbackVtbl{
|
||||||
ValA: c.Func((*Bar).getA),
|
Val: c.Func((*Bar).getA),
|
||||||
ValB: c.Func((*Bar).getB),
|
Calc: c.Func((*Bar).sqrt),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
a: a, b: b,
|
a: a,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,11 +27,11 @@ func (p *Bar) getA() int {
|
|||||||
return p.a
|
return p.a
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Bar) getB() float64 {
|
func (p *Bar) sqrt(v float64) float64 {
|
||||||
return p.b
|
return math.Sqrt(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bar := NewBar(1, 2.0)
|
bar := NewBar(1)
|
||||||
foo.F(&bar.Callback)
|
foo.F(&bar.Callback)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,10 +2,10 @@
|
|||||||
#define interface struct
|
#define interface struct
|
||||||
|
|
||||||
interface ICallback {
|
interface ICallback {
|
||||||
virtual int valA() = 0;
|
virtual int val() = 0;
|
||||||
virtual double valB() = 0;
|
virtual double calc(double v) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern "C" void f(ICallback* cb) {
|
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));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ type Callback struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CallbackVtbl struct {
|
type CallbackVtbl struct {
|
||||||
ValA unsafe.Pointer
|
Val unsafe.Pointer
|
||||||
ValB unsafe.Pointer
|
Calc unsafe.Pointer
|
||||||
}
|
}
|
||||||
|
|
||||||
//go:linkname F C.f
|
//go:linkname F C.f
|
||||||
|
|||||||
Reference in New Issue
Block a user