ClassDeepReset

Deep reset function for dynamic class instances.

void
ClassDeepReset
(
T
)
(
ref T dst
)

Parameters

T

type of class to deep copy

dst T

destination instance

Examples

unit test for the DeepReset method. Makes a test structure and fills it with data before calling reset and making sure it is cleared.

We first build a basic struct that has both a single sub struct and a dynamic array of sub structs. Both of these are then filled along with the fursther sub sub struct.

The DeepReset method is then called. The struct is then confirmed to have had it's members reset to the correct values

TODO Adjust the unit test so it also deals with struct being re-initialised to make sure they are not full of old data (~=)

1 struct TestStruct
2 {
3     int a;
4     char[] b;
5     int[7] c;
6 
7     public struct SubStruct
8     {
9         int d;
10         char[] e;
11         char[][] f;
12         int[7] g;
13 
14         public struct SubSubStruct
15         {
16             int h;
17             char[] i;
18             char[][] j;
19             int[7] k;
20 
21             void InitStructure()
22             {
23                 this.h = -52;
24                 this.i.copy("even even more test text");
25                 this.j.length = 3;
26                 this.j[0].copy("abc");
27                 this.j[1].copy("def");
28                 this.j[2].copy("ghi");
29                 foreach ( ref item; this.k )
30                 {
31                     item = 120000;
32                 }
33             }
34         }
35 
36         void InitStructure()
37         {
38             this.d = 32;
39             this.e.copy("even more test text");
40 
41             this.f.length = 1;
42             this.f[0].copy("abc");
43             foreach ( ref item; this.g )
44             {
45                 item = 32400;
46             }
47         }
48 
49         SubSubStruct[] sub_sub_struct;
50     }
51 
52     SubStruct sub_struct;
53 
54     SubStruct[] sub_struct_array;
55 }
56 
57 TestStruct test_struct;
58 test_struct.a = 7;
59 test_struct.b.copy("some test");
60 foreach ( i, ref item; test_struct.c )
61 {
62     item = 64800;
63 }
64 
65 TestStruct.SubStruct sub_struct;
66 sub_struct.InitStructure;
67 test_struct.sub_struct = sub_struct;
68 test_struct.sub_struct_array ~= sub_struct;
69 test_struct.sub_struct_array ~= sub_struct;
70 
71 
72 TestStruct.SubStruct.SubSubStruct sub_sub_struct;
73 sub_sub_struct.InitStructure;
74 test_struct.sub_struct_array[0].sub_sub_struct ~= sub_sub_struct;
75 test_struct.sub_struct_array[1].sub_sub_struct ~= sub_sub_struct;
76 test_struct.sub_struct_array[1].sub_sub_struct ~= sub_sub_struct;
77 test_struct.sub_struct.sub_sub_struct ~= sub_sub_struct;
78 test_struct.sub_struct.sub_sub_struct ~= sub_sub_struct;
79 
80 DeepReset!(TestStruct)(test_struct);
81 
82 test!("==")(test_struct.a, 0);
83 test!("==")(test_struct.b, ""[]);
84 foreach ( item; test_struct.c )
85 {
86     test!("==")(item, 0);
87 }
88 
89 test!("==")(test_struct.sub_struct_array.length, 0);
90 
91 test!("==")(test_struct.sub_struct.d, 0);
92 test!("==")(test_struct.sub_struct.e, ""[]);
93 test!("==")(test_struct.sub_struct.f.length, 0);
94 foreach ( item; test_struct.sub_struct.g )
95 {
96     test!("==")(item, 0);
97 }
98 
99 test!("==")(test_struct.sub_struct.sub_sub_struct.length, 0);
100 
101 //Test nested classes.
102 class TestClass
103 {
104     int a;
105     char[] b;
106     int[2] c;
107 
108     public class SubClass
109     {
110         int d;
111         char[] e;
112     }
113 
114     SubClass s;
115 }
116 
117 TestClass test_class = new TestClass;
118 test_class.s =  test_class.new SubClass;
119 test_class.a = 7;
120 test_class.b = [];
121 test_class.b ~= 't';
122 test_class.c[1] = 1;
123 test_class.s.d = 5;
124 test_class.s.e = [];
125 test_class.s.e ~= 'q';
126 
127 DeepReset!(TestClass)(test_class);
128 test!("==")(test_class.a, 0);
129 test!("==")(test_class.b.length, 0);
130 test!("==")(test_class.c[1], 0);
131 test!("!is")(cast(void*)test_class.s, null);
132 test!("==")(test_class.s.d, 0);
133 test!("==")(test_class.s.e.length, 0);

Meta