tmember.nim 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. destructing
  12. destructing
  13. '''
  14. """
  15. proc print(s: cstring) {.importcpp:"printf(@)", header:"<stdio.h>".}
  16. type
  17. Doo {.exportc.} = object
  18. test: int
  19. proc memberProc(f: Doo) {.exportc, member.} =
  20. echo $f.test
  21. proc destructor(f: Doo) {.member: "~'1()", used.} =
  22. print "destructing\n"
  23. proc `==`(self, other: Doo): bool {.member:"operator==('2 const & #2) const -> '0"} =
  24. self.test == other.test
  25. let doo = Doo(test: 2)
  26. doo.memberProc()
  27. echo doo == Doo(test: 1)
  28. #virtual
  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. proc salute(self: FooPtr) {.member: "virtual $1()".} =
  36. echo "hello foo"
  37. proc salute(self: BooPtr) {.member: "virtual $1()".} =
  38. echo "hello boo"
  39. let foo = newCpp[Foo]()
  40. let boo = newCpp[Boo]()
  41. let booAsFoo = cast[FooPtr](newCpp[Boo]())
  42. foo.salute()
  43. boo.salute()
  44. booAsFoo.salute()
  45. type
  46. NimFunctor = object
  47. discard
  48. proc invoke(f: NimFunctor, n:int) {.member:"operator ()('2 #2)" .} =
  49. echo "FunctorSupport!"
  50. {.experimental: "callOperator".}
  51. proc `()`(f: NimFunctor, n:int) {.importcpp:"#(@)" .}
  52. NimFunctor()(1)