Skip to content

Commit

Permalink
Merge pull request #31 from z-------------/export-sharedptr-constructor
Browse files Browse the repository at this point in the history
Export newCppSharedPtr
  • Loading branch information
Clonkk authored Jan 12, 2023
2 parents 3ac25b4 + 8f22821 commit 914d41b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion cppstl.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.6.1"
version = "0.6.2"
author = "Clonkk"
description = "Bindings for the C++ Standard Template Library (STL)"
license = "MIT"
Expand All @@ -27,6 +27,8 @@ task test, "Run the tests":
exec "nim cpp -r --gc:arc tests/tcomplex.nim"
exec "nim cpp -r tests/tpair.nim"
exec "nim cpp -r --gc:arc tests/tpair.nim"
exec "nim cpp -r tests/tsmart_ptrs.nim"
exec "nim cpp -r --gc:arc tests/tsmart_ptrs.nim"

# the following should compile and *not* produce a codegen error
exec "nim cpp --gc:arc -r tests/tdestructor_codegen_bug.nim"
Expand Down
2 changes: 1 addition & 1 deletion cppstl/std_smartptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ when not defined(cpp):
type
CppSharedPtr*[T]{.importcpp: "std::shared_ptr", bycopy.} = object

proc newCppSharedPtr[T](p: ptr T): CppSharedPtr[T] {.constructor,
proc newCppSharedPtr*[T](p: ptr T): CppSharedPtr[T] {.constructor,
importcpp: "std::shared_ptr<'*0>(#)".}

func makeShared*(T: typedesc): CppSharedPtr[T] {.importcpp: "std::make_shared<'*0>()".}
Expand Down
8 changes: 8 additions & 0 deletions tests/tsmart_ptrs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ proc init*(T: type UniquePtrObj, name: string): UniquePtrObj =
result.name = name
result.id = guid

proc cppNew[T](x: typedesc[T]): ptr T {.importcpp: "(new '1)", nodecl.}

proc testShared() =
test "SharedPtr":
var sp1 = SharedPtrObj.init("ptr_1")
Expand All @@ -44,6 +46,12 @@ proc testShared() =
check: sp1.name == "ptr_3"
check sp1.name.len() == 5

var sp3 = block:
let p = cppNew(Obj)
p[] = sp1.deref
newCppSharedPtr(p)
check sp3.deref == sp1.deref

when defined(gcArc) or defined(gcOrc):
check: $(sp1) == "CppShared ptr Obj(id: 1, name: \"ptr_3\")"
else:
Expand Down

0 comments on commit 914d41b

Please sign in to comment.