annotate orange/serialization/Serializer.d @ 28:bffcbc8c392b experimental

Associative arrays are now treated as references.
author Jacob Carlborg <doob@me.com>
date Fri, 19 Nov 2010 11:55:04 +0100
parents fc315d786f24
children c422ff6477dd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
1 /**
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
2 * Copyright: Copyright (c) 2010 Jacob Carlborg.
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
3 * Authors: Jacob Carlborg
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
4 * Version: Initial created: Jan 26, 2010
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
6 */
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
7 module orange.serialization.Serializer;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
8
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
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
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
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;
99c52d46822a Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents: 8
diff changeset
15 alias ConvError ConversionException;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
16 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
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
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
19 import orange.serialization._;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
20 import orange.serialization.archives._;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
21 import orange.util._;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
22
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
23 private
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
24 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
25 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
26
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
27 enum Mode
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
28 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
29 serializing,
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
30 deserializing
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
31 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
32
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
33 alias Mode.serializing serializing;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
34 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
35
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
36 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
37 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
38 if (c >= 'a' && c <= 'z')
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
39 return c - 32;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
40
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
41 return c;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
42 }
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
43 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
44
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
45 class Serializer
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
46 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
47 alias void delegate (ArchiveException exception, string[] data) ErrorCallback;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
48 alias UntypedData Data;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
49 alias size_t Id;
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
50
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
51 private
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
52 {
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
53 ErrorCallback errorCallback_;
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
54 Archive archive;
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
55
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
56 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
57 Id idCounter;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
58
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
59 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
60 RegisterBase[string] deserializers;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
61
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
62 Id[void*] serializedReferences;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
63 void*[Id] deserializedReferences;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
64
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
65 Array[Id] serializedArrays;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
66 void[][Id] deserializedSlices;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
67
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
68 bool hasBegunSerializing;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
69 bool hasBegunDeserializing;
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
70
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
71 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
72 void delegate (ArchiveException exception, string[] data) doNothingOnErrorCallback;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
73 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
74
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
75 this (Archive archive)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
76 {
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
77 this.archive = archive;
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
78
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
79 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
80 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
81
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
82 setThrowOnErrorCallback();
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
83 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
84
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
85 ErrorCallback errorCallback ()
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
86 {
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
87 return errorCallback_;
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
88 }
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
89
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
90 ErrorCallback errorCallback (ErrorCallback errorCallback)
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
91 {
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
92 return errorCallback_ = errorCallback;
18
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
93 }
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
94
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
95 void setThrowOnErrorCallback ()
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 errorCallback = throwOnErrorCallback;
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
98 }
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
99
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
100 void setDoNothingOnErrorCallback ()
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
101 {
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
102 errorCallback = doNothingOnErrorCallback;
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
103 }
3d42ea434d46 Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents: 17
diff changeset
104
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
105 void reset ()
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
106 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
107 resetCounters();
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
108
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
109 serializers = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
110 deserializers = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
111
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
112 serializedReferences = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
113 deserializedReferences = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
114
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
115 serializedArrays = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
116 deserializedSlices = null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
117
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
118 hasBegunSerializing = false;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
119 hasBegunDeserializing = false;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
120
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
121 archive.reset;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
122 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
123
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
124 Data serialize (T) (T value, string key = null)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
125 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
126 if (!hasBegunSerializing)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
127 hasBegunSerializing = true;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
128
20
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
129 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
130 postProcess;
20
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
131
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
132 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
133 }
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
134
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
135 private void serializeInternal (T) (T value, string key = null)
20
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
136 {
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
137 if (!key)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
138 key = nextKey;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
139
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
140 archive.beginArchiving();
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
141
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
142 static if ( is(T == typedef) )
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
143 serializeTypedef(value, key);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
144
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
145 else static if (isObject!(T))
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
146 serializeObject(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
147
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
148 else static if (isStruct!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
149 serializeStruct(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
150
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
151 else static if (isString!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
152 serializeString(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
153
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
154 else static if (isArray!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
155 serializeArray(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
156
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
157 else static if (isAssociativeArray!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
158 serializeAssociativeArray(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
159
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
160 else static if (isPrimitive!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
161 serializePrimitive(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
162
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
163 else static if (isPointer!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
164 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
165 static if (isFunctionPointer!(T))
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
166 goto error;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
167
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
168 else
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
169 serializePointer(value, key);
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
170 }
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
171
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
172 else static if (isEnum!(T))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
173 serializeEnum(value, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
174
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
175 else
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
176 {
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
177 error:
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
178 throw new SerializationException(format!(`The type "`, T, `" cannot be serialized.`), __FILE__, __LINE__);
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
179 }
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
180 }
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
181
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
182 private void serializeObject (T) (T value, string key)
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
183 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
184 if (!value)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
185 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
186
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
187 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
188
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
189 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
190 return archive.archiveReference(key, reference);
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
191
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
192 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
193
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
194 Id id = nextId;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
195 addSerializedReference(value, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
196
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
197 triggerEvents(serializing, value, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
198 archive.archiveObject(runtimeType, T.stringof, key, id, {
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
199 if (runtimeType in serializers)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
200 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
201 auto wrapper = getSerializerWrapper!(T)(runtimeType);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
202 wrapper(value, this, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
203 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
204
17
c4e7e64ffb67 Changed toData/fromData to take an instance of the serializer instead of the archive.
Jacob Carlborg <doob@me.com>
parents: 16
diff changeset
205 else static if (isSerializable!(T, Serializer))
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
206 value.toData(this, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
207
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
208 else
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
209 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
210 if (isBaseClass(value))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
211 throw new SerializationException(`The object of the static type "` ~ T.stringof ~ `" have a different runtime type (` ~ runtimeType ~ `) and therefore needs to register a serializer for its type "` ~ runtimeType ~ `".`, __FILE__, __LINE__);
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
212
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
213 objectStructSerializeHelper(value);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
214 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
215 });
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
216 });
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
217 }
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
218
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
219 private void serializeStruct (T) (T value, string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
220 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
221 string type = T.stringof;
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
222
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
223 triggerEvents(serializing, value, {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
224 archive.archiveStruct(type, key, nextId, {
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
225 if (type in serializers)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
226 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
227 auto wrapper = getSerializerWrapper!(T)(type);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
228 wrapper(value, this, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
229 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
230
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
231 else
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
232 {
17
c4e7e64ffb67 Changed toData/fromData to take an instance of the serializer instead of the archive.
Jacob Carlborg <doob@me.com>
parents: 16
diff changeset
233 static if (isSerializable!(T, Serializer))
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
234 value.toData(this, key);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
235
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
236 else
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
237 objectStructSerializeHelper(value);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
238 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
239 });
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
240 });
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
241 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
242
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
243 private void serializeString (T) (T value, string key)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
244 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
245 auto id = nextId;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
246 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
247
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
248 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
249 addSerializedArray(array, id);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
250 }
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
251
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
252 private void serializeArray (T) (T value, string key)
20
9a575087b961 Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 18
diff changeset
253 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
254 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
255 auto id = nextId;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
256
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
257 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
258 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
259 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
260 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
261
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
262 addSerializedArray(array, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
263 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
264
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
265 private void serializeAssociativeArray (T) (T value, string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
266 {
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
267 auto reference = getSerializedReference(value);
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
268
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
269 if (reference != Id.max)
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
270 return archive.archiveReference(key, reference);
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
271
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
272 Id id = nextId;
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
273 addSerializedReference(value, id);
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
274
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
275 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
276 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
277
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
278 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
279 size_t i;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
280
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
281 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
282 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
283 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
284 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
285 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
286
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
287 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
288 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
289 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
290
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
291 i++;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
292 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
293 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
294 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
295
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
296 private void serializePointer (T) (T value, string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
297 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
298 if (!value)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
299 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
300
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
301 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
302
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
303 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
304 return archive.archiveReference(key, reference);
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
305
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
306 Id id = nextId;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
307
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
308 addSerializedReference(value, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
309
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
310 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
311 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
312 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
313 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
314 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
315 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
316
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
317 else static if (isSerializable!(T, Serializer))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
318 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
319
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
320 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
321 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
322 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
323 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
324
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
325 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
326 serializeInternal(*value, nextKey);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
327 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
328 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
329 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
330
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
331 private void serializeEnum (T) (T value, string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
332 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
333 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
334 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
335 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
336
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
337 archive.archiveEnum(val, type, key, nextId);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
338 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
339
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
340 private void serializePrimitive (T) (T value, string key)
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.archive(value, key, nextId);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
343 }
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 private void serializeTypedef (T) (T value, string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
346 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
347 archive.archiveTypedef(T.stringof, key, nextId, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
348 serializeInternal!(BaseTypeOfTypedef!(T))(value, nextKey);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
349 });
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
350 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
351
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
352 T deserialize (T) (Data data, string key = null)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
353 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
354 if (hasBegunSerializing && !hasBegunDeserializing)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
355 resetCounters();
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
356
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
357 if (!hasBegunDeserializing)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
358 hasBegunDeserializing = true;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
359
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
360 if (!key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
361 key = nextKey;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
362
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
363 archive.beginUnarchiving(data);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
364 return deserializeInternal!(T)(key);
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
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
367 private T deserializeInternal (T) (string 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 static if (isTypedef!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
370 return deserializeTypedef!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
371
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
372 else static if (isObject!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
373 return deserializeObject!(T)(key);
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 else static if (isStruct!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
376 return deserializeStruct!(T)(key);
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 static if (isString!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
379 return deserializeString!(T)(key);
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 else static if (isArray!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
382 return deserializeArray!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
383
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
384 else static if (isAssociativeArray!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
385 return deserializeAssociativeArray!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
386
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
387 else static if (isPrimitive!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
388 return deserializePrimitive!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
389
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
390 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
391 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
392 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
393 goto error;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
394
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
395 return deserializePointer!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
396 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
397
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
398 else static if (isEnum!(T))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
399 return deserializeEnum!(T)(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
400
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
401 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
402 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
403 error:
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
404 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
405 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
406 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
407
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
408 private T deserializeObject (T) (string key)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
409 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
410 auto id = deserializeReference(key);
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 (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
413 return *reference;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
414
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
415 T value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
416 Object val = value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
417
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
418 archive.unarchiveObject(key, id, val, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
419 triggerEvents(deserializing, value, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
420 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
421 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
422
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
423 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
424 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
425 auto wrapper = getDeserializerWrapper!(T)(runtimeType);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
426 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
427 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
428
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
429 else static if (isSerializable!(T, Serializer))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
430 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
431
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
432 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
433 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
434 if (isBaseClass(value))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
435 throw new SerializationException(`The object of the static type "` ~ T.stringof ~ `" have a different runtime type (` ~ runtimeType ~ `) and therefore needs to register a deserializer for its type "` ~ runtimeType ~ `".`, __FILE__, __LINE__);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
436
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
437 objectStructDeserializeHelper(value);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
438 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
439 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
440 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
441
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
442 addDeserializedReference(value, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
443
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
444 return value;
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
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
447 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
448 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
449 T value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
450
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
451 archive.unarchiveStruct(key, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
452 triggerEvents(deserializing, value, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
453 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
454
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
455 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
456 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
457 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
458 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
459 }
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
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 (isSerializable!(T, Serializer))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
464 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
465
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
466 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
467 objectStructDeserializeHelper(value);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
468 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
469 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
470 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
471
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
472 return value;
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
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
475 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
476 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
477 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
478
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
479 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
480 return *tmp;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
481
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
482 T value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
483
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
484 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
485 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
486 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
487 value = archive.unarchiveString(slice.id).toSlice(slice);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
488
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
489 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
490 value = archive.unarchiveWstring(slice.id).toSlice(slice);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
491
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
492 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
493 value = archive.unarchiveDstring(slice.id).toSlice(slice);
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
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
496 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
497 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
498 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
499 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
500
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
501 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
502 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
503
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
504 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
505 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
506 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
507
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
508 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
509
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
510 return value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
511 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
512
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
513 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
514 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
515 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
516
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
517 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
518 return *tmp;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
519
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
520 T value;
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 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
523 value.length = length;
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 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
526 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
527 };
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 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
530 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
531 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
532 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
533
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
534 return value.toSlice(slice);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
535 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
536
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
537 else
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 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
540
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
541 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
542 return cast(T) *a;
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 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
545
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
546 return value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
547 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
548 }
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 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
551 {
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
552 auto id = deserializeReference(key);
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
553
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
554 if (auto reference = getDeserializedReference!(T)(id))
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
555 return *reference;
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
556
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
557 T value;
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 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
560 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
561
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
562 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
563 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
564 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
565 Key aaKey;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
566 Value aaValue;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
567 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
568
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
569 archive.unarchiveAssociativeArrayKey(k, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
570 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
571 });
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 archive.unarchiveAssociativeArrayValue(k, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
574 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
575 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
576
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
577 value[aaKey] = aaValue;
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
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
581 addDeserializedReference(value, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
582
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
583 return value;
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
584 }
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
585
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
586 private T deserializePointer (T) (string key)
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 auto id = deserializeReference(key);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
589
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
590 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
591 return *reference;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
592
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
593 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
594
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
595 id = archive.unarchivePointer(key, {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
596 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
597 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
598 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
599 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
600 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
601
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
602 else static if (isSerializable!(T, Serializer))
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
603 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
604
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
605 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
606 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
607 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
608 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
609
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
610 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
611 *value = deserializeInternal!(BaseTypeOfPointer!(T))(nextKey);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
612 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
613 });
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 addDeserializedReference(value, id);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
616
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
617 return value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
618 }
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 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
621 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
622 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
623
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
624 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
625 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
626 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
627
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
628 private T deserializePrimitive (T) (string key)
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 const functionName = toUpper(T.stringof[0]) ~ T.stringof[1 .. $];
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
631 mixin("return archive.unarchive" ~ functionName ~ "(key);");
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 deserializeTypedef (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 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
636 T value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
637
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
638 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
639 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
640 });
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
641
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
642 return value;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
643 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
644
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
645 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
646 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
647 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
648 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
649
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
650 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
651 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
652 return archive.unarchiveSlice(key);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
653 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
654
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
655 private void objectStructSerializeHelper (T) (ref T value)
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
656 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
657 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.`));
8
613a0bb20207 Now works with dmd 1.062
Jacob Carlborg <doob@me.com>
parents: 4
diff changeset
658 const nonSerializedFields = collectAnnotations!(nonSerializedField, T);
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
659
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
660 foreach (i, dummy ; typeof(T.tupleof))
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
661 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
662 const field = nameOfFieldAt!(T, i);
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
663
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
664 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
665 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
666 alias typeof(T.tupleof[i]) Type;
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
667 Type v = value.tupleof[i];
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
668 serializeInternal(v, toData(field));
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
669 }
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
670 }
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
671
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
672 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
673 serializeBaseTypes(value);
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
674 }
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
675
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
676 private void objectStructDeserializeHelper (T) (ref T value)
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
677 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
678 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.`));
8
613a0bb20207 Now works with dmd 1.062
Jacob Carlborg <doob@me.com>
parents: 4
diff changeset
679 const nonSerializedFields = collectAnnotations!(nonSerializedField, T);
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
680
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
681 foreach (i, dummy ; typeof(T.tupleof))
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
682 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
683 const field = nameOfFieldAt!(T, i);
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
684
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
685 static if (!internalFields.ctfeContains(field) && !nonSerializedFields.ctfeContains(field))
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
686 {
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
687 alias TypeOfField!(T, field) Type;
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
688 auto fieldValue = deserializeInternal!(Type)(toData(field));
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
689 value.tupleof[i] = fieldValue;
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
690 }
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
691 }
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
692
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
693 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
694 deserializeBaseTypes(value);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
695 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
696
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
697 private void serializeBaseTypes (T : Object) (T value)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
698 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
699 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
700
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
701 static if (!is(Base == Object))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
702 {
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
703 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
704 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
705 objectStructSerializeHelper(base);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
706 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
707 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
708
2
ea37a9470e3e Fixed multiple NonSerialized
Jacob Carlborg <doob@me.com>
parents: 0
diff changeset
709 private void deserializeBaseTypes (T : Object) (T value)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
710 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
711 alias BaseTypeTupleOf!(T)[0] Base;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
712
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
713 static if (!is(Base == Object))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
714 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
715 archive.unarchiveBaseClass!(Base)(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
716 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
717 objectStructDeserializeHelper(base);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
718 }
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
719 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
720
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
721 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
722 {
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
723 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
724
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
725 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
726 }
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 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
729 {
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
730 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
731
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
732 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
733 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
734
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
735 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
736 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
737 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
738
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
739 deserializedSlices[id] = value;
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 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
743 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
744 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
745 return *tmp;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
746
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
747 return Id.max;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
748 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
749
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
750 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
751 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
752 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
753 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
754
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
755 return null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
756 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
757
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
758 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
759 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
760 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
761 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
762 // 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
763 return null;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
764 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
765
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
766 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
767 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
768 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
769 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
770 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
771
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
772 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
773 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
774 return array[slice.offset .. slice.offset + slice.length];
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
775 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
776
15
9f6064f9505a Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents: 14
diff changeset
777 private SerializeRegisterWrapper!(T, Serializer) getSerializerWrapper (T) (string type)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
778 {
15
9f6064f9505a Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents: 14
diff changeset
779 auto wrapper = cast(SerializeRegisterWrapper!(T, Serializer)) serializers[type];
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
780
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
781 if (wrapper)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
782 return wrapper;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
783
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
784 assert(false, "throw exception here");
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
785 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
786
15
9f6064f9505a Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents: 14
diff changeset
787 private DeserializeRegisterWrapper!(T, Serializer) getDeserializerWrapper (T) (string type)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
788 {
15
9f6064f9505a Changed from archive to serializer in the register wrappers.
Jacob Carlborg <doob@me.com>
parents: 14
diff changeset
789 auto wrapper = cast(DeserializeRegisterWrapper!(T, Serializer)) deserializers[type];
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
790
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
791 if (wrapper)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
792 return wrapper;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
793
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
794 assert(false, "throw exception here");
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
795 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
796
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
797 private SerializeRegisterWrapper!(T, Serializer) toSerializeRegisterWrapper (T) (void delegate (T, Serializer, string) dg)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
798 {
16
091ff1b263db Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents: 15
diff changeset
799 return new SerializeRegisterWrapper!(T, Serializer)(dg);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
800 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
801
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
802 private SerializeRegisterWrapper!(T, Serializer) toSerializeRegisterWrapper (T) (void function (T, Serializer, string) func)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
803 {
16
091ff1b263db Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents: 15
diff changeset
804 return new SerializeRegisterWrapper!(T, Serializer)(func);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
805 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
806
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
807 private DeserializeRegisterWrapper!(T, Serializer) toDeserializeRegisterWrapper (T) (void delegate (ref T, Serializer, string) dg)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
808 {
16
091ff1b263db Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents: 15
diff changeset
809 return new DeserializeRegisterWrapper!(T, Serializer)(dg);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
810 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
811
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
812 private DeserializeRegisterWrapper!(T, Serializer) toDeserializeRegisterWrapper (T) (void function (ref T, Serializer, string) func)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
813 {
16
091ff1b263db Missed some in the previous commit.
Jacob Carlborg <doob@me.com>
parents: 15
diff changeset
814 return new DeserializeRegisterWrapper!(T, Serializer)(func);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
815 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
816
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
817 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
818 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
819 serializedArrays[id] = array;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
820 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
821
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
822 private void postProcessArrays ()
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
823 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
824 bool foundSlice = true;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
825
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
826 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
827 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
828 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
829 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
830 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
831 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
832 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
833 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
834 foundSlice = true;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
835 break;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
836 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
837
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
838 else
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
839 foundSlice = false;
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
840 }
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
841
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
842 if (!foundSlice)
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
843 archive.postProcessArray(sliceKey);
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
844 }
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 postProcess ()
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
848 {
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
849 postProcessArrays();
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
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
852 private template arrayToString (T)
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
853 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
854 const arrayToString = ElementTypeOfArray!(T).stringof;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
855 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
856
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
857 private bool isBaseClass (T) (T value)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
858 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
859 auto name = value.classinfo.name;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
860 auto index = name.lastIndexOf('.');
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
861
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
862 return T.stringof != name[index + 1 .. $];
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
863 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
864
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
865 private Id nextId ()
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
866 {
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
867 return idCounter++;
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
868 }
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
869
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
870 private string nextKey ()
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
871 {
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
872 return toData(keyCounter++);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
873 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
874
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
875 private void resetCounters ()
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
876 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
877 keyCounter = 0;
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
878 idCounter = 0;
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
879 }
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
880
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
881 private string toData (T) (T value)
25
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
882 {
b51e953f79eb Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents: 20
diff changeset
883 return to!(string)(value);
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
884 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
885
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
886 private void triggerEvent (string name, T) (T value)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
887 {
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
888 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
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
889
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
890 foreach (i, dummy ; typeof(T.tupleof))
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
891 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
892 const field = nameOfFieldAt!(T, i);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
893
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
894 static if (field == name)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
895 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
896 alias TypeOfField!(T, field) Type;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
897 auto event = getValueOfField!(T, Type, field)(value);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
898 event(value);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
899 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
900 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
901 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
902
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
903 private void triggerEvents (T) (Mode mode, T value, void delegate () dg)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
904 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
905 if (mode == serializing)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
906 triggerEvent!(onSerializingField)(value);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
907
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
908 else
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
909 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
910
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
911 dg();
26
78e5fef4bbf2 Third step in refactoring the API. Stating to add unit tests.
Jacob Carlborg <doob@me.com>
parents: 25
diff changeset
912
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
913 if (mode == serializing)
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
914 triggerEvent!(onSerializedField)(value);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
915
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
916 else
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
917 triggerEvent!(onDeserializedField)(value);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
918 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
919
8
613a0bb20207 Now works with dmd 1.062
Jacob Carlborg <doob@me.com>
parents: 4
diff changeset
920 private static string[] collectAnnotations (string name, T) ()
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
921 {
3
3c155e4c3d56 Fixed structs
Jacob Carlborg <doob@me.com>
parents: 2
diff changeset
922 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
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
923
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
924 string[] annotations;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
925
8
613a0bb20207 Now works with dmd 1.062
Jacob Carlborg <doob@me.com>
parents: 4
diff changeset
926 foreach (i, type ; typeof(T.tupleof))
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
927 {
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
928 const field = nameOfFieldAt!(T, i);
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
929
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
930 static if (field == name)
8
613a0bb20207 Now works with dmd 1.062
Jacob Carlborg <doob@me.com>
parents: 4
diff changeset
931 annotations ~= type.field;
0
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
932 }
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
933
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
934 return annotations;
f7b078e85f7f First commit
Jacob Carlborg <doob@me.com>
parents:
diff changeset
935 }
28
bffcbc8c392b Associative arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents: 27
diff changeset
936 }