1 /**
2 C++-style automatic memory management smart pointers for D using `stdx.allocator`.
3 
4 Unlike the C++ variants, the smart pointers themselves allocate the memory for the objects they contain.
5 That ensures the right allocator is used to dispose of the memory as well.
6 
7 Allocators are template arguments instead of using `theAllocator` so
8 that these smart pointers can be used in `@nogc` code. However, they
9 will default to `typeof(theAllocator)` for simplicity. The examples
10 above will be explicit.
11 
12 Another reason to have to pass in the type of allocator is to decide how it is to
13 be stored. Stateless allocators can be "stored" by value and imply zero-cost `Unique` pointers.
14 Singleton allocators such as Mallocator (that have an `instance` attribute/member function)
15 don't need to be passed in to the constructor. This is detected at compile-time as an example
16 of design by instrospection.
17 
18 `RefCounted` leverages D's type system by doing atomic reference counting *iff* the type of the contained
19 object is `shared`. Otherwise it's non-atomic.
20 */
21 module automem;
22 
23 public import automem.unique;
24 public import automem.unique_array;
25 public import automem.ref_counted;
26 
27 
28 /**
29  This unittest can be @safe if the allocator has @safe functions
30 */
31 @system @nogc unittest {
32 
33     import stdx.allocator.mallocator: Mallocator;
34     import std.algorithm: move;
35 
36     struct Point {
37         int x;
38         int y;
39     }
40 
41     {
42         // must pass arguments to initialise the contained object
43         auto u1 = Unique!(Point, Mallocator)(2, 3);
44         assert(*u1 == Point(2, 3));
45         assert(u1.y == 3);
46 
47         // auto u2 = u1; // won't compile, can only move
48         typeof(u1) u2 = u1.move;
49         assert(cast(bool)u1 == false); // u1 is now empty
50     }
51     // memory freed for the Point structure created in the block
52 
53     {
54         auto s1 = RefCounted!(Point, Mallocator)(4, 5);
55         assert(*s1 == Point(4, 5));
56         assert(s1.x == 4);
57         {
58             auto s2 = s1; // can be copied
59         } // ref count goes to 1 here
60 
61     } // ref count goes to 0 here, memory released
62 
63     {
64         // the constructor can also take (size, init) or (size, range) values
65         auto arr = UniqueArray!(Point, Mallocator)(3);
66 
67         const Point[3] expected1 = [Point(), Point(), Point()]; // because array literals aren't @nogc
68         assert(arr[] == expected1);
69 
70         const Point[1] expected2 = [Point()];
71         arr.length = 1;
72         assert(*arr == expected2); //deferencing is the same as slicing all of it
73 
74         arr ~= UniqueArray!(Point, Mallocator)(1, Point(6, 7));
75         const Point[2] expected3 = [Point(), Point(6, 7)];
76         assert(arr[] == expected3);
77 
78     } // memory for the array released here
79 }
80 
81 ///
82 @("theAllocator")
83 @system unittest {
84     with(theTestAllocator) {
85         auto ptr = Unique!int(42);
86         assert(*ptr == 42);
87     }
88     // TestAllocator will throw here if any memory leaks
89 }