1 module ut.object;
2 import numem.object;
3 import numem.lifetime;
4 import numem.core.hooks;
5 
6 class MyRCClass : NuRefCounted {
7 @nogc:
8 private:
9     int secret;
10 
11 public:
12     ~this() { }
13 
14     this(int secret) {
15         super();
16         this.secret = secret;
17     }
18 
19     int getSecret() {
20         return secret;
21     }
22 }
23 
24 class TrackedClass : NuRefCounted {
25 @nogc:
26 private:
27     __gshared uint rcClassCount;
28 
29 public:
30     ~this() { rcClassCount--; }
31     this() { rcClassCount++; }
32 }
33 
34 @("refcounted create-destroy.")
35 unittest {
36     MyRCClass rcclass = nogc_new!MyRCClass(42);
37     assert(rcclass.getSecret() == 42);
38     assert(rcclass.release() is null);
39 }
40 
41 @("refcounted pool")
42 unittest {
43     autoreleasepool(() {
44         foreach(i; 0..100) {
45             nogc_new!TrackedClass().autoreleased();
46         }
47 
48         assert(TrackedClass.rcClassCount == 100);
49     });
50     assert(TrackedClass.rcClassCount == 0);
51 }
52 
53 @("numem nogc overloads")
54 @nogc
55 unittest {
56     MyRCClass rcclass = nogc_new!MyRCClass(42);
57     
58     assert(rcclass.toString() == typeid(MyRCClass).name);
59     assert(rcclass.toHash() == rcclass.toHash()); // Just to ensure they are properly nogc.
60     assert(rcclass.opCmp(rcclass) == 0);
61     assert(rcclass.opEquals(rcclass)); // Just to ensure they are properly nogc.
62     rcclass.release();
63 }
64 
65 @nu_destroywith!((ref obj){ })
66 class CDestroyWith : NuObject {
67 private:
68 @nogc:
69     __gshared uint fCount = 0;
70     
71 public:
72     ~this() { fCount++; }
73 }
74 
75 @("nu_destroywith")
76 unittest {
77     auto dwith = nogc_new!CDestroyWith();
78 
79     assert(nogc_trydelete(dwith));
80     assert(CDestroyWith.fCount == 0);
81 }