-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #398 from xushiwei/q
cppmintf: implements multiple intefaces in c++
- Loading branch information
Showing
5 changed files
with
112 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.