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.ref_counted;
25 public import automem.vector;
26 public import automem.array;
27 
28 
29 @safe unittest {
30 
31     import std.algorithm: move;
32 
33     static struct Point {
34         int x;
35         int y;
36     }
37 
38     // set theAllocator as desired beforehand, e.g.
39     // theAllocator = allocatorObject(Mallocator.instance)
40 
41     {
42         // must pass arguments to initialise the contained object
43         auto u1 = Unique!Point(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 = () @trusted { return 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(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         import std.algorithm: map;
65         import std.range: iota;
66 
67         // `vector` is also known as `array`
68         auto vec = vector(Point(1, 2), Point(3, 4), Point(5, 6));
69         assert(vec[] == [Point(1, 2), Point(3, 4), Point(5, 6)]);
70 
71         vec.length = 1;
72         assert(vec[] == [Point(1, 2)]);
73 
74         vec ~= Point(7, 8);
75         assert(vec[] == [Point(1, 2), Point(7, 8)]);
76 
77         vec ~= 2.iota.map!(i => Point(i + 10, i + 11));
78         assert(vec[] == [Point(1, 2), Point(7, 8), Point(10, 11), Point(11, 12)]);
79     } // memory for the array released here
80 }
81 
82 
83 // @nogc test - must explicitly use the allocator for compile-time guarantees
84 @safe @nogc unittest {
85     import stdx.allocator.mallocator: Mallocator;
86 
87     static struct Point {
88         int x;
89         int y;
90     }
91 
92     {
93         // must pass arguments to initialise the contained object
94         auto u1 = Unique!(Point, Mallocator)(2, 3);
95         assert(*u1 == Point(2, 3));
96         assert(u1.y == 3);
97     }
98     // memory freed for the Point structure created in the block
99 
100     // similarly for the other types
101 }