tretvar.nim 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. discard """
  2. targets: "cpp"
  3. output: '''test1
  4. xest1
  5. '''
  6. """
  7. {.passC: "-std=c++14".}
  8. {.experimental: "dotOperators".}
  9. import macros
  10. type
  11. stdString {.importcpp: "std::string", header: "<string>".} = object
  12. stdUniquePtr[T] {.importcpp: "std::unique_ptr", header: "<memory>".} = object
  13. proc c_str(a: stdString): cstring {.importcpp: "(char *)(#.c_str())", header: "<string>".}
  14. proc len(a: stdString): csize_t {.importcpp: "(#.length())", header: "<string>".}
  15. proc setChar(a: var stdString, i: csize_t, c: char) {.importcpp: "(#[#] = #)", header: "<string>".}
  16. proc `*`*[T](this: stdUniquePtr[T]): var T {.noSideEffect, importcpp: "(* #)", header: "<memory>".}
  17. proc make_unique_str(a: cstring): stdUniquePtr[stdString] {.importcpp: "std::make_unique<std::string>(#)", header: "<string>".}
  18. macro `.()`*[T](this: stdUniquePtr[T], name: untyped, args: varargs[untyped]): untyped =
  19. result = nnkCall.newTree(
  20. nnkDotExpr.newTree(
  21. newNimNode(nnkPar).add(prefix(this, "*")),
  22. name
  23. )
  24. )
  25. copyChildrenTo(args, result)
  26. var val = make_unique_str("test1")
  27. echo val.c_str()
  28. val.setChar(0, 'x')
  29. echo val.c_str()