1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
discard """
targets: "cpp"
cmd: "nim cpp $file"
output: '''
hello foo
hello boo
hello boo
Const Message: hello world
NimPrinter: hello world
NimPrinterConstRef: hello world
NimPrinterConstRefByRef: hello world
'''
"""
{.emit:"""/*TYPESECTION*/
#include <iostream>
class CppPrinter {
public:
virtual void printConst(char* message) const {
std::cout << "Const Message: " << message << std::endl;
}
virtual void printConstRef(char* message, const int& flag) const {
std::cout << "Const Ref Message: " << message << std::endl;
}
virtual void printConstRef2(char* message, const int& flag) const {
std::cout << "Const Ref2 Message: " << message << std::endl;
}
};
""".}
proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
type
Foo = object of RootObj
FooPtr = ptr Foo
Boo = object of Foo
BooPtr = ptr Boo
CppPrinter {.importcpp, inheritable.} = object
NimPrinter {.exportc.} = object of CppPrinter
proc salute(self: FooPtr) {.virtual.} =
echo "hello foo"
proc salute(self: BooPtr) {.virtual.} =
echo "hello boo"
let foo = newCpp[Foo]()
let boo = newCpp[Boo]()
let booAsFoo = cast[FooPtr](newCpp[Boo]())
#polymorphism works
foo.salute()
boo.salute()
booAsFoo.salute()
let message = "hello world".cstring
proc printConst(self: CppPrinter, message: cstring) {.importcpp.}
CppPrinter().printConst(message)
#notice override is optional.
#Will make the cpp compiler to fail if not virtual function with the same signature if found in the base type
proc printConst(self: NimPrinter, message: cstring) {.virtual:"$1('2 #2) const override".} =
echo "NimPrinter: " & $message
proc printConstRef(self: NimPrinter, message: cstring, flag: int32) {.virtual:"$1('2 #2, const '3& #3 ) const override".} =
echo "NimPrinterConstRef: " & $message
proc printConstRef2(self: NimPrinter, message: cstring, flag {.byref.}: int32) {.virtual:"$1('2 #2, const '3 #3 ) const override".} =
echo "NimPrinterConstRefByRef: " & $message
NimPrinter().printConst(message)
var val : int32 = 10
NimPrinter().printConstRef(message, val)
NimPrinter().printConstRef2(message, val)
#bug 22269
type Doo = object
proc naiveMember(x: Doo): int {. virtual .} = 2
discard naiveMember(Doo())
#asmnostackframe works with virtual
{.emit:"""/*TYPESECTION*/
template<typename T>
struct Box {
T* first;
Box(int x){
first = new T(x);
};
};
struct Inner {
int val;
//Coo() = default;
Inner(int x){
val = x;
};
};
struct Base {
virtual Box<Inner> test() = 0;
};
""".}
type
Inner {.importcpp.} = object
Base {.importcpp, inheritable.} = object
Child = object of Base
Box[T] {.importcpp, inheritable.} = object
first: T
proc makeBox[T](x:int32): Box[T] {.importcpp:"Box<'0>(@)", constructor.}
proc test(self: Child): Box[Inner] {.virtual, asmnostackframe.} =
let res {.exportc.} = makeBox[Inner](100)
{.emit:"return res;".}
discard Child().test()
import virtualptr
#We dont want to pull Loo directly by using it as we are testing that the pointer pulls it.
proc makeMoo(): Moo {.importcpp:"{ new Loo() }".}
makeMoo().loo.salute()
|