tmember.nim 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. discard """
  2. targets: "cpp"
  3. cmd: "nim cpp $file"
  4. output: '''
  5. 2
  6. false
  7. hello foo
  8. hello boo
  9. hello boo
  10. FunctorSupport!
  11. static
  12. static
  13. destructing
  14. destructing
  15. '''
  16. """
  17. proc print(s: cstring) {.importcpp:"printf(@)", header:"<stdio.h>".}
  18. type
  19. Doo {.exportc.} = object
  20. test: int
  21. proc memberProc(f: Doo) {.exportc, member.} =
  22. echo $f.test
  23. proc destructor(f: Doo) {.member: "~'1()", used.} =
  24. print "destructing\n"
  25. proc `==`(self, other: Doo): bool {.member:"operator==('2 const & #2) const -> '0"} =
  26. self.test == other.test
  27. let doo = Doo(test: 2)
  28. doo.memberProc()
  29. echo doo == Doo(test: 1)
  30. #virtual
  31. proc newCpp*[T](): ptr T {.importcpp:"new '*0()".}
  32. type
  33. Foo {.exportc.} = object of RootObj
  34. FooPtr = ptr Foo
  35. Boo = object of Foo
  36. BooPtr = ptr Boo
  37. proc salute(self: FooPtr) {.member: "virtual $1()".} =
  38. echo "hello foo"
  39. proc salute(self: BooPtr) {.member: "virtual $1()".} =
  40. echo "hello boo"
  41. let foo = newCpp[Foo]()
  42. let boo = newCpp[Boo]()
  43. let booAsFoo = cast[FooPtr](newCpp[Boo]())
  44. foo.salute()
  45. boo.salute()
  46. booAsFoo.salute()
  47. type
  48. NimFunctor = object
  49. discard
  50. proc invoke(f: NimFunctor, n:int) {.member:"operator ()('2 #2)" .} =
  51. echo "FunctorSupport!"
  52. {.experimental: "callOperator".}
  53. proc `()`(f: NimFunctor, n:int) {.importcpp:"#(@)" .}
  54. NimFunctor()(1)
  55. #static
  56. proc staticProc(self: FooPtr) {.member: "static $1()".} =
  57. echo "static"
  58. proc importedStaticProc() {.importcpp:"Foo::staticProc()".}
  59. foo.staticProc()
  60. importedStaticProc()