Mercurial > projects > orange
annotate orange/serialization/Serializer.d @ 49:beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Sat, 13 Aug 2011 15:57:19 +0200 |
parents | a7dea44fa9e3 |
children |
rev | line source |
---|---|
0 | 1 /** |
2 * Copyright: Copyright (c) 2010 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Jan 26, 2010 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module orange.serialization.Serializer; | |
8 | |
9 version (Tango) | |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
8
diff
changeset
|
10 import tango.util.Convert : to, ConversionException; |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
8
diff
changeset
|
11 |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
8
diff
changeset
|
12 else |
0 | 13 { |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
8
diff
changeset
|
14 import std.conv; |
34
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
15 alias ConvException ConversionException; |
0 | 16 } |
17 | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
18 import orange.core._; |
0 | 19 import orange.serialization._; |
34
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
20 import orange.serialization.archives.Archive; |
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
21 import orange.serialization.archives.ArchiveException; |
0 | 22 import orange.util._; |
23 | |
24 private | |
25 { | |
26 alias orange.util.CTFE.contains ctfeContains; | |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
27 |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
28 enum Mode |
0 | 29 { |
30 serializing, | |
31 deserializing | |
32 } | |
33 | |
34 alias Mode.serializing serializing; | |
35 alias Mode.deserializing deserializing; | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
36 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
37 private char toUpper (char c) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
38 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
39 if (c >= 'a' && c <= 'z') |
34
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
40 return cast(char) (c - 32); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
41 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
42 return c; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
43 } |
0 | 44 } |
45 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
46 class Serializer |
0 | 47 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
48 alias void delegate (ArchiveException exception, string[] data) ErrorCallback; |
34
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
49 alias Archive.UntypedData Data; |
068e853b9c07
Cleaned up. Updated to latest D2 compiler.
Jacob Carlborg <doob@me.com>
parents:
33
diff
changeset
|
50 alias Archive.Id Id; |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
51 |
0 | 52 private |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
53 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
54 struct ValueMeta |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
55 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
56 Id id; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
57 string key; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
58 } |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
59 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
60 ErrorCallback errorCallback_; |
39 | 61 Archive archive_; |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
62 |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
63 size_t keyCounter; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
64 Id idCounter; |
0 | 65 |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
66 void delegate (Object, Mode mode) [ClassInfo] registeredTypes; |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
67 |
0 | 68 RegisterBase[string] serializers; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
69 RegisterBase[string] deserializers; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
70 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
71 Id[void*] serializedReferences; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
72 void*[Id] deserializedReferences; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
73 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
74 Array[Id] serializedArrays; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
75 void[][Id] deserializedSlices; |
0 | 76 |
31
c68d29967c9f
Fixed some bugs in the handling of pointers.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
77 void*[Id] serializedPointers; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
78 void**[Id] deserializedPointers; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
79 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
80 ValueMeta[void*] serializedValues; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
81 void*[Id] deserializedValues; |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
82 |
0 | 83 bool hasBegunSerializing; |
84 bool hasBegunDeserializing; | |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
85 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
86 void delegate (ArchiveException exception, string[] data) throwOnErrorCallback; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
87 void delegate (ArchiveException exception, string[] data) doNothingOnErrorCallback; |
0 | 88 } |
89 | |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
90 this (Archive archive) |
0 | 91 { |
39 | 92 this.archive_ = archive; |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
93 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
94 throwOnErrorCallback = (ArchiveException exception, string[] data) { throw exception; }; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
95 doNothingOnErrorCallback = (ArchiveException exception, string[] data) { /* do nothing */ }; |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
96 |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
97 setThrowOnErrorCallback(); |
0 | 98 } |
99 | |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
100 void register (T : Object) () |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
101 { |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
102 registeredTypes[T.classinfo] = &downcastSerialize!(T); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
103 } |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
104 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
105 private void downcastSerialize (T : Object) (Object value, Mode mode) |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
106 { |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
107 auto casted = cast(T) value; |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
108 assert(casted); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
109 assert(casted.classinfo is T.classinfo); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
110 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
111 if (mode == serializing) |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
112 objectStructSerializeHelper(casted); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
113 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
114 else |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
115 objectStructDeserializeHelper(casted); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
116 } |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
117 |
39 | 118 Archive archive () |
119 { | |
120 return archive_; | |
121 } | |
122 | |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
123 ErrorCallback errorCallback () |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
124 { |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
125 return errorCallback_; |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
126 } |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
127 |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
128 ErrorCallback errorCallback (ErrorCallback errorCallback) |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
129 { |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
130 return errorCallback_ = errorCallback; |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
131 } |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
132 |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
133 void setThrowOnErrorCallback () |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
134 { |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
135 errorCallback = throwOnErrorCallback; |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
136 } |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
137 |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
138 void setDoNothingOnErrorCallback () |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
139 { |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
140 errorCallback = doNothingOnErrorCallback; |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
141 } |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
17
diff
changeset
|
142 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
143 void reset () |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
144 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
145 resetCounters(); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
146 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
147 serializers = null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
148 deserializers = null; |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
149 registeredTypes = null; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
150 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
151 serializedReferences = null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
152 deserializedReferences = null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
153 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
154 serializedArrays = null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
155 deserializedSlices = null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
156 |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
157 serializedValues = null; |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
158 deserializedValues = null; |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
159 |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
160 serializedPointers = null; |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
161 deserializedPointers = null; |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
162 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
163 hasBegunSerializing = false; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
164 hasBegunDeserializing = false; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
165 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
166 archive.reset; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
167 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
168 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
169 Data serialize (T) (T value, string key = null) |
0 | 170 { |
171 if (!hasBegunSerializing) | |
172 hasBegunSerializing = true; | |
173 | |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
174 serializeInternal(value, key); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
175 postProcess; |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
176 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
177 return archive.untypedData; |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
178 } |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
179 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
180 private void serializeInternal (T) (T value, string key = null, Id id = Id.max) |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
181 { |
0 | 182 if (!key) |
183 key = nextKey; | |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
184 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
185 if (id == Id.max) |
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
186 id = nextId; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
187 |
0 | 188 archive.beginArchiving(); |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
189 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
190 static if ( is(T == typedef) ) |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
191 serializeTypedef(value, key, id); |
0 | 192 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
193 else static if (isObject!(T)) |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
194 serializeObject(value, key, id); |
0 | 195 |
196 else static if (isStruct!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
197 serializeStruct(value, key, id); |
0 | 198 |
199 else static if (isString!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
200 serializeString(value, key, id); |
0 | 201 |
202 else static if (isArray!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
203 serializeArray(value, key, id); |
0 | 204 |
205 else static if (isAssociativeArray!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
206 serializeAssociativeArray(value, key, id); |
0 | 207 |
208 else static if (isPrimitive!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
209 serializePrimitive(value, key, id); |
0 | 210 |
211 else static if (isPointer!(T)) | |
212 { | |
213 static if (isFunctionPointer!(T)) | |
2 | 214 goto error; |
0 | 215 |
216 else | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
217 serializePointer(value, key, id); |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
218 } |
0 | 219 |
220 else static if (isEnum!(T)) | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
221 serializeEnum(value, key, id); |
0 | 222 |
223 else | |
2 | 224 { |
225 error: | |
226 throw new SerializationException(format!(`The type "`, T, `" cannot be serialized.`), __FILE__, __LINE__); | |
227 } | |
0 | 228 } |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
229 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
230 private void serializeObject (T) (T value, string key, Id id) |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
231 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
232 if (!value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
233 return archive.archiveNull(T.stringof, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
234 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
235 auto reference = getSerializedReference(value); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
236 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
237 if (reference != Id.max) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
238 return archive.archiveReference(key, reference); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
239 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
240 auto runtimeType = value.classinfo.name; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
241 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
242 addSerializedReference(value, id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
243 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
244 triggerEvents(serializing, value, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
245 archive.archiveObject(runtimeType, T.stringof, key, id, { |
0 | 246 if (runtimeType in serializers) |
247 { | |
248 auto wrapper = getSerializerWrapper!(T)(runtimeType); | |
249 wrapper(value, this, key); | |
250 } | |
251 | |
39 | 252 else static if (isSerializable!(T)) |
0 | 253 value.toData(this, key); |
254 | |
255 else | |
256 { | |
257 if (isBaseClass(value)) | |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
258 { |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
259 if (auto serializer = value.classinfo in registeredTypes) |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
260 (*serializer)(value, serializing); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
261 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
262 else |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
263 throw new SerializationException( |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
264 `The object of the static type "` ~ T.stringof ~ |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
265 `" have a different runtime type (` ~ runtimeType ~ |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
266 `) and therefore needs to either register its type or register a serializer for its type "` |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
267 ~ runtimeType ~ `".`, __FILE__, __LINE__); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
268 } |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
269 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
270 else |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
271 objectStructSerializeHelper(value); |
0 | 272 } |
273 }); | |
274 }); | |
275 } | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
276 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
277 private void serializeStruct (T) (T value, string key, Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
278 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
279 string type = T.stringof; |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
280 |
0 | 281 triggerEvents(serializing, value, { |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
282 archive.archiveStruct(type, key, id, { |
0 | 283 if (type in serializers) |
284 { | |
285 auto wrapper = getSerializerWrapper!(T)(type); | |
286 wrapper(value, this, key); | |
287 } | |
288 | |
289 else | |
290 { | |
40
02dbd18b7fe9
Moved all tests into its own modules.
Jacob Carlborg <doob@me.com>
parents:
39
diff
changeset
|
291 static if (isSerializable!(T)) |
0 | 292 value.toData(this, key); |
293 | |
294 else | |
2 | 295 objectStructSerializeHelper(value); |
0 | 296 } |
297 }); | |
298 }); | |
299 } | |
300 | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
301 private void serializeString (T) (T value, string key, Id id) |
0 | 302 { |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
303 auto array = Array(cast(void*) value.ptr, value.length, ElementTypeOfArray!(T).sizeof); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
304 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
305 archive.archive(value, key, id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
306 addSerializedArray(array, id); |
0 | 307 } |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
308 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
309 private void serializeArray (T) (T value, string key, Id id) |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
310 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
311 auto array = Array(value.ptr, value.length, ElementTypeOfArray!(T).sizeof); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
312 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
313 archive.archiveArray(array, arrayToString!(T), key, id, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
314 foreach (i, e ; value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
315 serializeInternal(e, toData(i)); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
316 }); |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
317 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
318 addSerializedArray(array, id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
319 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
320 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
321 private void serializeAssociativeArray (T) (T value, string key, Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
322 { |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
323 auto reference = getSerializedReference(value); |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
324 |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
325 if (reference != Id.max) |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
326 return archive.archiveReference(key, reference); |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
327 |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
328 addSerializedReference(value, id); |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
329 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
330 string keyType = KeyTypeOfAssociativeArray!(T).stringof; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
331 string valueType = ValueTypeOfAssociativeArray!(T).stringof; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
332 |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
333 archive.archiveAssociativeArray(keyType, valueType, value.length, key, id, { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
334 size_t i; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
335 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
336 foreach(k, v ; value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
337 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
338 archive.archiveAssociativeArrayKey(toData(i), { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
339 serializeInternal(k, toData(i)); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
340 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
341 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
342 archive.archiveAssociativeArrayValue(toData(i), { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
343 serializeInternal(v, toData(i)); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
344 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
345 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
346 i++; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
347 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
348 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
349 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
350 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
351 private void serializePointer (T) (T value, string key, Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
352 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
353 if (!value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
354 return archive.archiveNull(T.stringof, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
355 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
356 auto reference = getSerializedReference(value); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
357 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
358 if (reference != Id.max) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
359 return archive.archiveReference(key, reference); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
360 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
361 addSerializedReference(value, id); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
362 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
363 archive.archivePointer(key, id, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
364 if (key in serializers) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
365 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
366 auto wrapper = getSerializerWrapper!(T)(key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
367 wrapper(value, this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
368 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
369 |
39 | 370 else static if (isSerializable!(T)) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
371 value.toData(this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
372 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
373 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
374 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
375 static if (isVoid!(BaseTypeOfPointer!(T))) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
376 throw new SerializationException(`The value with the key "` ~ to!(string)(key) ~ `"` ~ format!(` of the type "`, T, `" cannot be serialized on its own, either implement orange.serialization.Serializable.isSerializable or register a serializer.`), __FILE__, __LINE__); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
377 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
378 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
379 serializeInternal(*value, nextKey); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
380 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
381 }); |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
382 |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
383 addSerializedPointer(value, id); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
384 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
385 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
386 private void serializeEnum (T) (T value, string key, Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
387 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
388 alias BaseTypeOfEnum!(T) EnumBaseType; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
389 auto val = cast(EnumBaseType) value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
390 string type = T.stringof; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
391 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
392 archive.archiveEnum(val, type, key, id); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
393 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
394 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
395 private void serializePrimitive (T) (T value, string key, Id id) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
396 { |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
397 archive.archive(value, key, id); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
398 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
399 |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
400 private void serializeTypedef (T) (T value, string key, Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
401 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
402 archive.archiveTypedef(T.stringof, key, nextId, { |
32
9df3b7a46a51
Updated the unit test with the latest changes.
Jacob Carlborg <doob@me.com>
parents:
31
diff
changeset
|
403 serializeInternal!(BaseTypeOfTypedef!(T))(value, nextKey); |
0 | 404 }); |
405 } | |
406 | |
39 | 407 T deserialize (T) (Data data, string key = "") |
408 { | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
409 if (hasBegunSerializing && !hasBegunDeserializing) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
410 resetCounters(); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
411 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
412 if (!hasBegunDeserializing) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
413 hasBegunDeserializing = true; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
414 |
39 | 415 if (key.empty()) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
416 key = nextKey; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
417 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
418 archive.beginUnarchiving(data); |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
419 auto value = deserializeInternal!(T)(key); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
420 deserializingPostProcess; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
421 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
422 return value; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
423 } |
39 | 424 |
425 T deserialize (T) (string key) | |
426 { | |
427 if (!hasBegunDeserializing) | |
40
02dbd18b7fe9
Moved all tests into its own modules.
Jacob Carlborg <doob@me.com>
parents:
39
diff
changeset
|
428 throw new SerializationException("Cannot deserialize without any data, this method should only be called after deserialization has begun.", __FILE__, __LINE__); |
39 | 429 |
430 return deserialize!(T)(archive.untypedData, key); | |
431 } | |
432 | |
433 T deserialize (T) () | |
434 { | |
435 return deserialize!(T)(""); | |
436 } | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
437 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
438 private T deserializeInternal (T, U) (U keyOrId) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
439 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
440 static if (isTypedef!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
441 return deserializeTypedef!(T)(keyOrId); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
442 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
443 else static if (isObject!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
444 return deserializeObject!(T)(keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
445 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
446 else static if (isStruct!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
447 return deserializeStruct!(T)(keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
448 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
449 else static if (isString!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
450 return deserializeString!(T)(keyOrId); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
451 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
452 else static if (isArray!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
453 return deserializeArray!(T)(keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
454 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
455 else static if (isAssociativeArray!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
456 return deserializeAssociativeArray!(T)(keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
457 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
458 else static if (isPrimitive!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
459 return deserializePrimitive!(T)(keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
460 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
461 else static if (isPointer!(T)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
462 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
463 static if (isFunctionPointer!(T)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
464 goto error; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
465 Id id; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
466 return deserializePointer!(T)(keyOrId, id); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
467 } |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
468 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
469 else static if (isEnum!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
470 return deserializeEnum!(T)(keyOrId); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
471 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
472 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
473 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
474 error: |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
475 throw new SerializationException(format!(`The type "`, T, `" cannot be deserialized.`), __FILE__, __LINE__); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
476 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
477 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
478 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
479 private T deserializeObject (T, U) (U keyOrId) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
480 { |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
481 auto id = deserializeReference(keyOrId); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
482 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
483 if (auto reference = getDeserializedReference!(T)(id)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
484 return *reference; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
485 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
486 T value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
487 Object val = value; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
488 nextId; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
489 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
490 archive.unarchiveObject(keyOrId, id, val, { |
42
8b9409423740
Added unit tests for (de)serializing events.
Jacob Carlborg <doob@me.com>
parents:
40
diff
changeset
|
491 triggerEvents(deserializing, cast(T) val, { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
492 value = cast(T) val; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
493 auto runtimeType = value.classinfo.name; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
494 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
495 if (runtimeType in deserializers) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
496 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
497 auto wrapper = getDeserializerWrapper!(T)(runtimeType); |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
498 wrapper(value, this, keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
499 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
500 |
39 | 501 else static if (isSerializable!(T)) |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
502 value.fromData(this, keyOrId); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
503 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
504 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
505 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
506 if (isBaseClass(value)) |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
507 { |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
508 if (auto deserializer = value.classinfo in registeredTypes) |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
509 (*deserializer)(value, deserializing); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
510 |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
511 else |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
512 throw new SerializationException( |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
513 `The object of the static type "` ~ T.stringof ~ |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
514 `" have a different runtime type (` ~ runtimeType ~ |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
515 `) and therefore needs to either register its type or register a deserializer for its type "` |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
516 ~ runtimeType ~ `".`, __FILE__, __LINE__); |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
517 } |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
518 |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
519 else |
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
520 objectStructDeserializeHelper(value); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
521 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
522 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
523 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
524 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
525 addDeserializedReference(value, id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
526 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
527 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
528 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
529 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
530 private T deserializeStruct (T) (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
531 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
532 T value; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
533 nextId; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
534 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
535 archive.unarchiveStruct(key, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
536 triggerEvents(deserializing, value, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
537 auto type = toData(T.stringof); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
538 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
539 if (type in deserializers) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
540 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
541 auto wrapper = getDeserializerWrapper!(T)(type); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
542 wrapper(value, this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
543 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
544 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
545 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
546 { |
40
02dbd18b7fe9
Moved all tests into its own modules.
Jacob Carlborg <doob@me.com>
parents:
39
diff
changeset
|
547 static if (isSerializable!(T)) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
548 value.fromData(this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
549 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
550 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
551 objectStructDeserializeHelper(value); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
552 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
553 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
554 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
555 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
556 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
557 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
558 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
559 private T deserializeString (T) (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
560 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
561 auto slice = deserializeSlice(key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
562 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
563 if (auto tmp = getDeserializedSlice!(T)(slice)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
564 return *tmp; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
565 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
566 T value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
567 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
568 if (slice.id != size_t.max) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
569 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
570 static if (is(T == string)) |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
571 value = toSlice(archive.unarchiveString(slice.id), slice); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
572 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
573 else static if (is(T == wstring)) |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
574 value = toSlice(archive.unarchiveWstring(slice.id), slice); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
575 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
576 else static if (is(T == dstring)) |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
577 value = toSlice(archive.unarchiveDstring(slice.id), slice); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
578 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
579 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
580 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
581 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
582 static if (is(T == string)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
583 value = archive.unarchiveString(key, slice.id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
584 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
585 else static if (is(T == wstring)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
586 value = archive.unarchiveWstring(key, slice.id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
587 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
588 else static if (is(T == dstring)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
589 value = archive.unarchiveDstring(key, slice.id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
590 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
591 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
592 addDeserializedSlice(value, slice.id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
593 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
594 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
595 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
596 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
597 private T deserializeArray (T) (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
598 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
599 auto slice = deserializeSlice(key); |
46
d6fbd0b3586e
Issue 8: Wrong results for array of arrays. (Temporary fix).
Jacob Carlborg <doob@me.com>
parents:
42
diff
changeset
|
600 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
601 if (auto tmp = getDeserializedSlice!(T)(slice)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
602 return *tmp; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
603 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
604 T value; |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
605 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
606 auto dg = (size_t length) { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
607 value.length = length; |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
608 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
609 foreach (i, ref e ; value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
610 e = deserializeInternal!(typeof(e))(toData(i)); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
611 }; |
46
d6fbd0b3586e
Issue 8: Wrong results for array of arrays. (Temporary fix).
Jacob Carlborg <doob@me.com>
parents:
42
diff
changeset
|
612 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
613 if (slice.id != size_t.max) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
614 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
615 archive.unarchiveArray(slice.id, dg); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
616 addDeserializedSlice(value, slice.id); |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
617 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
618 return toSlice(value, slice); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
619 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
620 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
621 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
622 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
623 slice.id = archive.unarchiveArray(key, dg); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
624 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
625 if (auto a = slice.id in deserializedSlices) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
626 return cast(T) *a; |
46
d6fbd0b3586e
Issue 8: Wrong results for array of arrays. (Temporary fix).
Jacob Carlborg <doob@me.com>
parents:
42
diff
changeset
|
627 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
628 addDeserializedSlice(value, slice.id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
629 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
630 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
631 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
632 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
633 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
634 private T deserializeAssociativeArray (T) (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
635 { |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
636 auto id = deserializeReference(key); |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
637 |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
638 if (auto reference = getDeserializedReference!(T)(id)) |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
639 return *reference; |
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
640 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
641 T value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
642 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
643 alias KeyTypeOfAssociativeArray!(T) Key; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
644 alias ValueTypeOfAssociativeArray!(T) Value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
645 |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
646 id = archive.unarchiveAssociativeArray(key, (size_t length) { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
647 for (size_t i = 0; i < length; i++) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
648 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
649 Key aaKey; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
650 Value aaValue; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
651 auto k = toData(i); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
652 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
653 archive.unarchiveAssociativeArrayKey(k, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
654 aaKey = deserializeInternal!(Key)(k); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
655 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
656 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
657 archive.unarchiveAssociativeArrayValue(k, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
658 aaValue = deserializeInternal!(Value)(k); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
659 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
660 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
661 value[aaKey] = aaValue; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
662 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
663 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
664 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
665 addDeserializedReference(value, id); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
666 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
667 return value; |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
668 } |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
669 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
670 private T deserializePointer (T) (string key, out Id id) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
671 { |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
672 id = deserializeReference(key); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
673 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
674 if (auto reference = getDeserializedReference!(T)(id)) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
675 return *reference; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
676 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
677 T value = new BaseTypeOfPointer!(T); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
678 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
679 auto pointerId = archive.unarchivePointer(key, { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
680 if (key in deserializers) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
681 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
682 auto wrapper = getDeserializerWrapper!(T)(key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
683 wrapper(value, this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
684 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
685 |
40
02dbd18b7fe9
Moved all tests into its own modules.
Jacob Carlborg <doob@me.com>
parents:
39
diff
changeset
|
686 else static if (isSerializable!(T)) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
687 value.fromData(this, key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
688 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
689 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
690 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
691 static if (isVoid!(BaseTypeOfPointer!(T))) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
692 throw new SerializationException(`The value with the key "` ~ to!(string)(key) ~ `"` ~ format!(` of the type "`, T, `" cannot be deserialized on its own, either implement orange.serialization.Serializable.isSerializable or register a deserializer.`), __FILE__, __LINE__); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
693 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
694 else |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
695 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
696 auto k = nextKey; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
697 id = deserializeReference(k); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
698 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
699 if (id != Id.max) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
700 return; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
701 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
702 *value = deserializeInternal!(BaseTypeOfPointer!(T))(k); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
703 } |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
704 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
705 }); |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
706 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
707 addDeserializedReference(value, pointerId); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
708 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
709 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
710 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
711 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
712 private T deserializeEnum (T) (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
713 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
714 alias BaseTypeOfEnum!(T) Enum; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
715 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
716 const functionName = toUpper(Enum.stringof[0]) ~ Enum.stringof[1 .. $]; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
717 mixin("return cast(T) archive.unarchiveEnum" ~ functionName ~ "(key);"); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
718 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
719 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
720 private T deserializePrimitive (T, U) (U keyOrId) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
721 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
722 const functionName = toUpper(T.stringof[0]) ~ T.stringof[1 .. $]; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
723 mixin("return archive.unarchive" ~ functionName ~ "(keyOrId);"); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
724 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
725 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
726 private T deserializeTypedef (T, U) (U keyOrId) |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
727 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
728 T value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
729 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
730 archive.unarchiveTypedef!(T)(key, { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
731 value = cast(T) deserializeInternal!(BaseTypeOfTypedef!(T))(nextKey); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
732 }); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
733 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
734 return value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
735 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
736 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
737 private Id deserializeReference (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
738 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
739 return archive.unarchiveReference(key); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
740 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
741 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
742 private Slice deserializeSlice (string key) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
743 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
744 return archive.unarchiveSlice(key); |
0 | 745 } |
746 | |
3 | 747 private void objectStructSerializeHelper (T) (ref T value) |
748 { | |
749 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); | |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
750 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
751 version (Tango) |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
752 const nonSerializedFields = collectAnnotations!(nonSerializedField, T); |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
753 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
754 else |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
755 mixin(`enum nonSerializedFields = collectAnnotations!(nonSerializedField, T);`); |
3 | 756 |
757 foreach (i, dummy ; typeof(T.tupleof)) | |
758 { | |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
759 version (Tango) |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
760 const field = nameOfFieldAt!(T, i); |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
761 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
762 else |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
763 mixin(`enum field = nameOfFieldAt!(T, i);`); |
3 | 764 |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
765 static if (!ctfeContains!(string)(internalFields, field) && !ctfeContains!(string)(nonSerializedFields, field)) |
3 | 766 { |
767 alias typeof(T.tupleof[i]) Type; | |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
768 Type v = value.tupleof[i]; |
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
769 auto id = nextId; |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
770 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
771 addSerializedValue(value.tupleof[i], id, toData(keyCounter)); |
30
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
772 serializeInternal(v, toData(field), id); |
9d1a8023bb89
Added IDs to every serialized value.
Jacob Carlborg <doob@me.com>
parents:
29
diff
changeset
|
773 } |
3 | 774 } |
2 | 775 |
4
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
776 static if (isObject!(T) && !is(T == Object)) |
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
777 serializeBaseTypes(value); |
3 | 778 } |
779 | |
780 private void objectStructDeserializeHelper (T) (ref T value) | |
781 { | |
782 static assert(isStruct!(T) || isObject!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); | |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
783 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
784 version (Tango) |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
785 const nonSerializedFields = collectAnnotations!(nonSerializedField, T); |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
786 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
787 else |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
788 mixin(`enum nonSerializedFields = collectAnnotations!(nonSerializedField, T);`); |
3 | 789 |
790 foreach (i, dummy ; typeof(T.tupleof)) | |
791 { | |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
792 version (Tango) |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
793 const field = nameOfFieldAt!(T, i); |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
794 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
795 else |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
796 mixin(`enum field = nameOfFieldAt!(T, i);`); |
3 | 797 |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
798 static if (!ctfeContains!(string)(internalFields, field) && !ctfeContains!(string)(nonSerializedFields, field)) |
3 | 799 { |
800 alias TypeOfField!(T, field) Type; | |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
801 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
802 static if (isPointer!(Type)) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
803 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
804 Id id; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
805 value.tupleof[i] = deserializePointer!(Type)(toData(field), id); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
806 addDeserializedPointer(value.tupleof[i], id); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
807 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
808 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
809 else |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
810 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
811 auto fieldValue = deserializeInternal!(Type)(toData(field)); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
812 value.tupleof[i] = fieldValue; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
813 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
814 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
815 addDeserializedValue(value.tupleof[i], nextId); |
3 | 816 } |
817 } | |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
818 |
4
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
819 static if (isObject!(T) && !is(T == Object)) |
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
820 deserializeBaseTypes(value); |
0 | 821 } |
822 | |
2 | 823 private void serializeBaseTypes (T : Object) (T value) |
0 | 824 { |
825 alias BaseTypeTupleOf!(T)[0] Base; | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
826 |
0 | 827 static if (!is(Base == Object)) |
828 { | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
829 archive.archiveBaseClass(Base.stringof, nextKey, nextId); |
4
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
830 Base base = value; |
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
831 objectStructSerializeHelper(base); |
0 | 832 } |
833 } | |
834 | |
2 | 835 private void deserializeBaseTypes (T : Object) (T value) |
0 | 836 { |
837 alias BaseTypeTupleOf!(T)[0] Base; | |
838 | |
839 static if (!is(Base == Object)) | |
840 { | |
47
9c9bbef6bf5e
Implemented unarchiveBaseClass. Enabled the unit tests for deserialize subclasses.
Jacob Carlborg <doob@me.com>
parents:
46
diff
changeset
|
841 archive.unarchiveBaseClass(nextKey); |
4
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
842 Base base = value; |
470ab5270d0c
Simplified the implementation of RegisterWrapper. Fixed: the base class of an object was not serialized
Jacob Carlborg <doob@me.com>
parents:
3
diff
changeset
|
843 objectStructDeserializeHelper(base); |
0 | 844 } |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
845 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
846 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
847 private void addSerializedReference (T) (T value, Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
848 { |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
849 static assert(isReference!(T) || isAssociativeArray!(T), format!(`The given type "`, T, `" is not a reference type, i.e. object, pointer or associative array.`)); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
850 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
851 serializedReferences[cast(void*) value] = id; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
852 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
853 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
854 private void addDeserializedReference (T) (T value, Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
855 { |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
856 static assert(isReference!(T) || isAssociativeArray!(T), format!(`The given type "`, T, `" is not a reference type, i.e. object, pointer or associative array.`)); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
857 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
858 deserializedReferences[id] = cast(void*) value; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
859 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
860 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
861 private void addDeserializedSlice (T) (T value, Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
862 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
863 static assert(isArray!(T) || isString!(T), format!(`The given type "`, T, `" is not a slice type, i.e. array or string.`)); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
864 |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
865 deserializedSlices[id] = cast(void[]) value; |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
866 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
867 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
868 private void addSerializedValue (T) (ref T value, Id id, string key) |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
869 { |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
870 serializedValues[&value] = ValueMeta(id, key); |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
871 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
872 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
873 private void addDeserializedValue (T) (ref T value, Id id) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
874 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
875 deserializedValues[id] = &value; |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
876 } |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
877 |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
878 private void addSerializedPointer (T) (T value, Id id) |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
879 { |
31
c68d29967c9f
Fixed some bugs in the handling of pointers.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
880 serializedPointers[id] = value; |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
881 } |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
882 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
883 private void addDeserializedPointer (T) (ref T value, Id id) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
884 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
885 deserializedPointers[id] = cast(void**) &value; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
886 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
887 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
888 private Id getSerializedReference (T) (T value) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
889 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
890 if (auto tmp = cast(void*) value in serializedReferences) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
891 return *tmp; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
892 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
893 return Id.max; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
894 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
895 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
896 private T* getDeserializedReference (T) (Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
897 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
898 if (auto reference = id in deserializedReferences) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
899 return cast(T*) reference; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
900 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
901 return null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
902 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
903 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
904 private T* getDeserializedSlice (T) (Slice slice) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
905 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
906 if (auto array = slice.id in deserializedSlices) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
907 return &(cast(T) *array)[slice.offset .. slice.offset + slice.length]; // dereference the array, cast it to the right type, |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
908 // slice it and then return a pointer to the result |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
909 return null; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
910 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
911 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
912 private T* getDeserializedArray (T) (Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
913 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
914 if (auto array = id in deserializedSlices) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
915 return cast(T*) array; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
916 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
917 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
918 private T* getDeserializedValue (T) (Id id) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
919 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
920 if (auto value = id in deserializedValues) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
921 return cast(T*) value; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
922 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
923 return null; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
924 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
925 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
926 private T[] toSlice (T) (T[] array, Slice slice) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
927 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
928 return array[slice.offset .. slice.offset + slice.length]; |
0 | 929 } |
930 | |
15
9f6064f9505a
Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents:
14
diff
changeset
|
931 private SerializeRegisterWrapper!(T, Serializer) getSerializerWrapper (T) (string type) |
0 | 932 { |
15
9f6064f9505a
Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents:
14
diff
changeset
|
933 auto wrapper = cast(SerializeRegisterWrapper!(T, Serializer)) serializers[type]; |
0 | 934 |
935 if (wrapper) | |
936 return wrapper; | |
937 | |
938 assert(false, "throw exception here"); | |
939 } | |
940 | |
15
9f6064f9505a
Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents:
14
diff
changeset
|
941 private DeserializeRegisterWrapper!(T, Serializer) getDeserializerWrapper (T) (string type) |
0 | 942 { |
15
9f6064f9505a
Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents:
14
diff
changeset
|
943 auto wrapper = cast(DeserializeRegisterWrapper!(T, Serializer)) deserializers[type]; |
0 | 944 |
945 if (wrapper) | |
946 return wrapper; | |
947 | |
948 assert(false, "throw exception here"); | |
949 } | |
950 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
951 private SerializeRegisterWrapper!(T, Serializer) toSerializeRegisterWrapper (T) (void delegate (T, Serializer, string) dg) |
0 | 952 { |
16
091ff1b263db
Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents:
15
diff
changeset
|
953 return new SerializeRegisterWrapper!(T, Serializer)(dg); |
0 | 954 } |
955 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
956 private SerializeRegisterWrapper!(T, Serializer) toSerializeRegisterWrapper (T) (void function (T, Serializer, string) func) |
0 | 957 { |
16
091ff1b263db
Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents:
15
diff
changeset
|
958 return new SerializeRegisterWrapper!(T, Serializer)(func); |
0 | 959 } |
960 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
961 private DeserializeRegisterWrapper!(T, Serializer) toDeserializeRegisterWrapper (T) (void delegate (ref T, Serializer, string) dg) |
0 | 962 { |
16
091ff1b263db
Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents:
15
diff
changeset
|
963 return new DeserializeRegisterWrapper!(T, Serializer)(dg); |
0 | 964 } |
965 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
966 private DeserializeRegisterWrapper!(T, Serializer) toDeserializeRegisterWrapper (T) (void function (ref T, Serializer, string) func) |
0 | 967 { |
16
091ff1b263db
Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents:
15
diff
changeset
|
968 return new DeserializeRegisterWrapper!(T, Serializer)(func); |
0 | 969 } |
970 | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
971 private void addSerializedArray (Array array, Id id) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
972 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
973 serializedArrays[id] = array; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
974 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
975 |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
976 private void postProcess () |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
977 { |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
978 postProcessArrays(); |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
979 postProcessPointers(); |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
980 } |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
981 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
982 private void postProcessArrays () |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
983 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
984 bool foundSlice = true; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
985 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
986 foreach (sliceKey, slice ; serializedArrays) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
987 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
988 foreach (arrayKey, array ; serializedArrays) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
989 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
990 if (slice.isSliceOf(array) && slice != array) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
991 { |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
992 auto s = Slice(slice.length, (slice.ptr - array.ptr) / slice.elementSize); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
993 archive.archiveSlice(s, sliceKey, arrayKey); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
994 foundSlice = true; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
995 break; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
996 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
997 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
998 else |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
999 foundSlice = false; |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1000 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1001 |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1002 if (!foundSlice) |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1003 archive.postProcessArray(sliceKey); |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1004 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1005 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1006 |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1007 private void postProcessPointers () |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1008 { |
31
c68d29967c9f
Fixed some bugs in the handling of pointers.
Jacob Carlborg <doob@me.com>
parents:
30
diff
changeset
|
1009 foreach (pointerId, value ; serializedPointers) |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1010 { |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1011 if (auto valueMeta = value in serializedValues) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1012 archive.archivePointer(valueMeta.id, valueMeta.key, pointerId); |
29
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1013 |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1014 else |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1015 archive.postProcessPointer(pointerId); |
c422ff6477dd
Better handling of serializing pointers.
Jacob Carlborg <doob@me.com>
parents:
28
diff
changeset
|
1016 } |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1017 } |
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1018 |
33
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1019 private void deserializingPostProcess () |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1020 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1021 deserializingPostProcessPointers; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1022 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1023 |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1024 private void deserializingPostProcessPointers () |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1025 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1026 foreach (pointeeId, pointee ; deserializedValues) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1027 { |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1028 if (auto pointer = pointeeId in deserializedPointers) |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1029 **pointer = pointee; |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1030 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1031 } |
4fea56a5849f
Now both internal and external pointers work.
Jacob Carlborg <doob@me.com>
parents:
32
diff
changeset
|
1032 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1033 private template arrayToString (T) |
0 | 1034 { |
35
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
1035 version (Tango) |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
1036 const arrayToString = ElementTypeOfArray!(T).stringof; |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
1037 |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
1038 else |
511d1ef4e299
Now all unit tests pass on latest DMD2 compiler.
Jacob Carlborg <doob@me.com>
parents:
34
diff
changeset
|
1039 mixin(`enum arrayToString = ElementTypeOfArray!(T).stringof;`); |
0 | 1040 } |
1041 | |
1042 private bool isBaseClass (T) (T value) | |
1043 { | |
49
beb4afce2f3e
Fixed (de)serializing through base class reference, including unit test.
Jacob Carlborg <doob@me.com>
parents:
48
diff
changeset
|
1044 return value.classinfo !is T.classinfo; |
0 | 1045 } |
1046 | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1047 private Id nextId () |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1048 { |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1049 return idCounter++; |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1050 } |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1051 |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1052 private string nextKey () |
0 | 1053 { |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1054 return toData(keyCounter++); |
0 | 1055 } |
1056 | |
1057 private void resetCounters () | |
1058 { | |
1059 keyCounter = 0; | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1060 idCounter = 0; |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1061 } |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1062 |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1063 private string toData (T) (T value) |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1064 { |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
1065 return to!(string)(value); |
0 | 1066 } |
1067 | |
1068 private void triggerEvent (string name, T) (T value) | |
1069 { | |
3 | 1070 static assert (isObject!(T) || isStruct!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); |
0 | 1071 |
1072 foreach (i, dummy ; typeof(T.tupleof)) | |
1073 { | |
1074 const field = nameOfFieldAt!(T, i); | |
1075 | |
1076 static if (field == name) | |
1077 { | |
1078 alias TypeOfField!(T, field) Type; | |
1079 auto event = getValueOfField!(T, Type, field)(value); | |
1080 event(value); | |
1081 } | |
1082 } | |
1083 } | |
1084 | |
1085 private void triggerEvents (T) (Mode mode, T value, void delegate () dg) | |
1086 { | |
1087 if (mode == serializing) | |
1088 triggerEvent!(onSerializingField)(value); | |
1089 | |
1090 else | |
1091 triggerEvent!(onDeserializingField)(value); | |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1092 |
0 | 1093 dg(); |
26
78e5fef4bbf2
Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents:
25
diff
changeset
|
1094 |
0 | 1095 if (mode == serializing) |
1096 triggerEvent!(onSerializedField)(value); | |
1097 | |
1098 else | |
1099 triggerEvent!(onDeserializedField)(value); | |
1100 } | |
1101 | |
8 | 1102 private static string[] collectAnnotations (string name, T) () |
0 | 1103 { |
3 | 1104 static assert (isObject!(T) || isStruct!(T), format!(`The given value of the type "`, T, `" is not a valid type, the only valid types for this method are objects and structs.`)); |
0 | 1105 |
1106 string[] annotations; | |
1107 | |
8 | 1108 foreach (i, type ; typeof(T.tupleof)) |
0 | 1109 { |
1110 const field = nameOfFieldAt!(T, i); | |
1111 | |
1112 static if (field == name) | |
8 | 1113 annotations ~= type.field; |
0 | 1114 } |
1115 | |
1116 return annotations; | |
1117 } | |
28
bffcbc8c392b
Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
27
diff
changeset
|
1118 } |