SharedPtr.opAssign

Shares ownership of the object managed by rhs.

If rhs manages no object, this manages no object too. If rhs is rvalue then move-assigns a SharedPtr from rhs

  1. void opAssign(typeof(null) nil)
  2. void opAssign(Rhs desired)
    struct SharedPtr
    scope
    void
    opAssign
    (
    MemoryOrder order = MemoryOrder.seq
    Rhs
    this This
    )
    (
    scope auto ref Rhs desired
    )
    if (
    isSmartPtr!Rhs &&
    !is(Rhs == shared)
    )

Examples

1 {
2 	SharedPtr!long px1 = SharedPtr!long.make(1);
3 	SharedPtr!long px2 = SharedPtr!long.make(2);
4 
5 	assert(px2.useCount == 1);
6 	px1 = px2;
7 	assert(*px1 == 2);
8 	assert(px2.useCount == 2);
9 }
10 
11 
12 {
13 	SharedPtr!long px = SharedPtr!long.make(1);
14 	SharedPtr!(const long) pcx = SharedPtr!long.make(2);
15 
16 	assert(px.useCount == 1);
17 	pcx = px;
18 	assert(*pcx == 1);
19 	assert(pcx.useCount == 2);
20 
21 }
22 
23 
24 {
25 	const SharedPtr!long cpx = SharedPtr!long.make(1);
26 	SharedPtr!(const long) pcx = SharedPtr!long.make(2);
27 
28 	assert(pcx.useCount == 1);
29 	pcx = cpx;
30 	assert(*pcx == 1);
31 	assert(pcx.useCount == 2);
32 
33 }
34 
35 {
36 	SharedPtr!(immutable long) pix = SharedPtr!(immutable long).make(123);
37 	SharedPtr!(const long) pcx = SharedPtr!long.make(2);
38 
39 	assert(pix.useCount == 1);
40 	pcx = pix;
41 	assert(*pcx == 123);
42 	assert(pcx.useCount == 2);
43 
44 }
45 
46 import btl.autoptr.rc_ptr;
47 {
48 	SharedPtr!long sp = SharedPtr!long.make(10);
49 	RcPtr!long rc = RcPtr!long.make(20);
50 
51 	assert(sp.useCount == 1);
52 	assert(rc.useCount == 1);
53 	sp = rc;
54 
55 	assert(sp.get == 20);
56 	assert(rc.useCount == 2);
57 }
58 
59 import btl.autoptr.intrusive_ptr;
60 {
61 	static struct Foo{
62 		private SharedControlBlock control;
63 		int i;
64 
65 		this(int i)pure nothrow @safe @nogc{
66 			this.i = i;
67 		}
68 	}
69 
70 	SharedPtr!Foo sp = SharedPtr!Foo.make(10);
71 	IntrusivePtr!Foo ip = IntrusivePtr!Foo.make(20);
72 
73 	assert(sp.useCount == 1);
74 	assert(ip.useCount == 1);
75 	sp = ip;
76 
77 	assert(sp.get.i== 20);
78 	assert(ip.useCount == 2);
79 }

Meta