sharedPtr

Create SharedPtr from parameter ptr of type SharedPtr, RcPtr or IntrusivePtr.

@trusted
sharedPtr
(
Ptr
)
(
scope auto ref Ptr ptr
)
if (
!is(Ptr == shared) &&
)

Examples

1 import btl.autoptr.rc_ptr;
2 import btl.autoptr.intrusive_ptr;
3 
4 import core.lifetime : move;
5 //RcPtr -> SharedPtr:
6 {
7 	auto x = RcPtr!long.make(42);
8 	assert(*x == 42);
9 	assert(x.useCount == 1);
10 
11 	auto s = sharedPtr(x);
12 	assert(x.useCount == 2);
13 
14 	import btl.autoptr.shared_ptr : isSharedPtr;
15 	static assert(isSharedPtr!(typeof(s)));
16 
17 	auto s2 = sharedPtr(x.move);
18 	assert(s.useCount == 2);
19 
20 	auto y = sharedPtr(RcPtr!long.init);
21 	assert(y == null);
22 }
23 
24 //IntrusivePtr -> SharedPtr:
25 {
26 	static class Foo{
27 		ControlBlock!(int, int) c;
28 		int i;
29 
30 		this(int i)pure nothrow @safe @nogc{
31 			this.i = i;
32 		}
33 	}
34 
35 	auto x = IntrusivePtr!Foo.make(42);
36 	//assert(x.get.i == 42);
37 	assert(x.useCount == 1);
38 
39 	auto s = sharedPtr(x);
40 	assert(x.useCount == 2);
41 
42 	import btl.autoptr.shared_ptr : isSharedPtr;
43 	static assert(isSharedPtr!(typeof(s)));
44 
45 	auto s2 = sharedPtr(x.move);
46 	assert(s.useCount == 2);
47 
48 	auto y = sharedPtr(IntrusivePtr!Foo.init);
49 	assert(y == null);
50 }
51 
52 //SharedPtr -> SharedPtr:
53 {
54 	auto a = SharedPtr!long.make(1);
55 	assert(a.useCount == 1);
56 
57 	auto b = sharedPtr(a);
58 	assert(a.useCount == 2);
59 
60 	auto c = sharedPtr(SharedPtr!long.make(2));
61 	assert(c.useCount == 1);
62 }

Meta