tvirtual.nim 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. discard """
  2. targets: "cpp"
  3. cmd: "nim cpp $file"
  4. output: '''
  5. hello foo
  6. hello boo
  7. hello boo
  8. Const Message: hello world
  9. NimPrinter: hello world
  10. NimPrinterConstRef: hello world
  11. NimPrinterConstRefByRef: hello world
  12. '''
  13. """
  14. {.emit:"""/*TYPESECTION*/
  15. #include <iostream>
  16. class CppPrinter {
  17. public:
  18. virtual void printConst(char* message) const {
  19. std::cout << "Const Message: " << message << std::endl;
  20. }
  21. virtual void printConstRef(char* message, const int& flag) const {
  22. std::cout << "Const Ref Message: " << message << std::endl;
  23. }
  24. virtual void printConstRef2(char* message, const int& flag) const {
  25. std::cout << "Const Ref2 Message: " << message << std::endl;
  26. }
  27. };
  28. """.}
  29. proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
  30. type
  31. Foo = object of RootObj
  32. FooPtr = ptr Foo
  33. Boo = object of Foo
  34. BooPtr = ptr Boo
  35. CppPrinter {.importcpp, inheritable.} = object
  36. NimPrinter {.exportc.} = object of CppPrinter
  37. proc salute(self: FooPtr) {.virtual.} =
  38. echo "hello foo"
  39. proc salute(self: BooPtr) {.virtual.} =
  40. echo "hello boo"
  41. let foo = newCpp[Foo]()
  42. let boo = newCpp[Boo]()
  43. let booAsFoo = cast[FooPtr](newCpp[Boo]())
  44. #polymorphism works
  45. foo.salute()
  46. boo.salute()
  47. booAsFoo.salute()
  48. let message = "hello world".cstring
  49. proc printConst(self: CppPrinter, message: cstring) {.importcpp.}
  50. CppPrinter().printConst(message)
  51. #notice override is optional.
  52. #Will make the cpp compiler to fail if not virtual function with the same signature if found in the base type
  53. proc printConst(self: NimPrinter, message: cstring) {.virtual:"$1('2 #2) const override".} =
  54. echo "NimPrinter: " & $message
  55. proc printConstRef(self: NimPrinter, message: cstring, flag: int32) {.virtual:"$1('2 #2, const '3& #3 ) const override".} =
  56. echo "NimPrinterConstRef: " & $message
  57. proc printConstRef2(self: NimPrinter, message: cstring, flag {.byref.}: int32) {.virtual:"$1('2 #2, const '3 #3 ) const override".} =
  58. echo "NimPrinterConstRefByRef: " & $message
  59. NimPrinter().printConst(message)
  60. var val : int32 = 10
  61. NimPrinter().printConstRef(message, val)
  62. NimPrinter().printConstRef2(message, val)
  63. #bug 22269
  64. type Doo = object
  65. proc naiveMember(x: Doo): int {. virtual .} = 2
  66. discard naiveMember(Doo())