mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 18:02:05 +00:00
* introduces virtual pragma, modifies proc def, prevents proc decl * marks virtual procs as infix * forward declare vfuncs inside the typedef * adds naked callConv to virtual * virtual proc error if not defined in the same top level scope as the type * first param is now this. extracts genvirtualheaderproc * WIP syntax * supports obj. Removes the need for the prefix * parameter count starts as this. Cleanup * clean up * sem tests * adds integration tests * uses constraint to store the virtual content * introduces genVirtualProcParams --------- Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
69 lines
1.7 KiB
Nim
69 lines
1.7 KiB
Nim
discard """
|
|
targets: "cpp"
|
|
cmd: "nim cpp $file"
|
|
output: '''
|
|
hello foo
|
|
hello boo
|
|
hello boo
|
|
Const Message: hello world
|
|
NimPrinter: hello world
|
|
NimPrinterConstRef: 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;
|
|
}
|
|
};
|
|
""".}
|
|
|
|
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
|
|
|
|
NimPrinter().printConst(message)
|
|
var val : int32 = 10
|
|
NimPrinter().printConstRef(message, val)
|
|
|
|
|