Skip to content

Commit

Permalink
Merge pull request #398 from xushiwei/q
Browse files Browse the repository at this point in the history
cppmintf: implements multiple intefaces in c++
  • Loading branch information
xushiwei committed Jun 23, 2024
2 parents df39b66 + ce81872 commit 4f7d3ad
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
6 changes: 3 additions & 3 deletions _demo/cppintf/cppintf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (

type Bar struct {
foo.Callback
a int
a c.Int
}

func NewBar(a int) *Bar {
func NewBar(a c.Int) *Bar {
return &Bar{
Callback: foo.Callback{
Vptr: &foo.CallbackVtbl{
Expand All @@ -23,7 +23,7 @@ func NewBar(a int) *Bar {
}
}

func (p *Bar) getA() int {
func (p *Bar) getA() c.Int {
return p.a
}

Expand Down
50 changes: 50 additions & 0 deletions _demo/cppmintf/cpp_multi_intf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package main

import (
"unsafe"

"github.com/goplus/llgo/_demo/cppmintf/foo"
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/math"
)

type Bar struct {
foo.Callback
a c.Int
}

func NewBar(a c.Int) *Bar {
return &Bar{
Callback: foo.Callback{
ICalc: foo.ICalc{
Vptr: &foo.ICalcVtbl{
Calc: c.Func((*Bar).sqrt),
},
},
IVal: foo.IVal{
Vptr: &foo.IValVtbl{
Val: c.Func(bar_IVal_getA),
},
},
},
a: a,
}
}

func (p *Bar) getA() c.Int {
return p.a
}

func bar_IVal_getA(this c.Pointer) c.Int {
const delta = -int(unsafe.Offsetof(foo.Callback{}.IVal))
return (*Bar)(c.Advance(this, delta)).getA()
}

func (p *Bar) sqrt(v float64) float64 {
return math.Sqrt(v)
}

func main() {
bar := NewBar(1)
foo.F(&bar.Callback)
}
17 changes: 17 additions & 0 deletions _demo/cppmintf/foo/bar/bar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
#define interface struct

interface ICalc {
virtual double calc(double v) = 0;
};

interface IVal {
virtual int val() = 0;
};

class Callback : public ICalc, public IVal {
};

extern "C" void f(Callback* cb) {
printf("val: %d\ncalc(2): %lf\n", cb->val(), cb->calc(2));
}
42 changes: 42 additions & 0 deletions _demo/cppmintf/foo/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package foo

import (
"unsafe"
)

const (
LLGoFiles = "bar/bar.cpp"
LLGoPackage = "link"
)

// -----------------------------------------------------------------------------

type ICalc struct {
Vptr *ICalcVtbl
}

type ICalcVtbl struct {
Calc unsafe.Pointer
}

// -----------------------------------------------------------------------------

type IVal struct {
Vptr *IValVtbl
}

type IValVtbl struct {
Val unsafe.Pointer
}

// -----------------------------------------------------------------------------

type Callback struct {
ICalc
IVal
}

//go:linkname F C.f
func F(cb *Callback)

// -----------------------------------------------------------------------------
Binary file added _demo/cppmintf/foo/llgo_autogen.lla
Binary file not shown.

0 comments on commit 4f7d3ad

Please sign in to comment.