Mercurial > projects > orange
annotate orange/serialization/archives/Archive.d @ 25:b51e953f79eb experimental
Second step in refactoring the API.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Wed, 06 Oct 2010 16:18:02 +0200 |
parents | 55f0a9d5df8d |
children | 78e5fef4bbf2 |
rev | line source |
---|---|
0 | 1 /** |
2 * Copyright: Copyright (c) 2010 Jacob Carlborg. | |
3 * Authors: Jacob Carlborg | |
4 * Version: Initial created: Feb 6, 2010 | |
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0) | |
6 */ | |
7 module orange.serialization.archives.Archive; | |
8 | |
9 version (Tango) | |
10 import tango.util.Convert; | |
11 | |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
12 else |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
13 { |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
14 import std.conv; |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
15 alias ConvError ConversionException; |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
16 } |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
17 |
0 | 18 import orange.serialization.archives.ArchiveException; |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
19 import orange.util.string; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
20 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
21 private enum ArchiveMode |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
22 { |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
23 archiving, |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
24 unarchiving |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
25 } |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
26 |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
27 struct Array |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
28 { |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
29 void* ptr; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
30 size_t length; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
31 size_t elementSize; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
32 |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
33 bool isSliceOf (Array b) |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
34 { |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
35 return ptr >= b.ptr && ptr + length * elementSize <= b.ptr + b.length * b.elementSize; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
36 } |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
37 } |
0 | 38 |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
39 struct Slice |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
40 { |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
41 size_t length; |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
42 size_t offset; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
43 size_t id; |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
44 } |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
45 |
0 | 46 interface IArchive |
47 { | |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
48 version (Tango) alias void[] IDataType; |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
49 else mixin ("alias immutable(void)[] IDataType;"); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
50 |
0 | 51 void beginArchiving (); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
52 void beginUnarchiving (IDataType data); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
53 |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
54 IDataType data (); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
55 void postProcess (); |
0 | 56 void reset (); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
57 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
58 void archiveArray (Array array, string type, string key, size_t id, void delegate () dg); |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
59 void archiveAssociativeArray (string keyType, string valueType, size_t length, string key, size_t id, void delegate () dg); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
60 |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
61 void archiveEnum (bool value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
62 void archiveEnum (byte value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
63 void archiveEnum (char value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
64 void archiveEnum (dchar value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
65 void archiveEnum (int value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
66 void archiveEnum (long value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
67 void archiveEnum (short value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
68 void archiveEnum (ubyte value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
69 void archiveEnum (uint value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
70 void archiveEnum (ulong value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
71 void archiveEnum (ushort value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
72 void archiveEnum (wchar value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
73 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
74 void archiveBaseClass (string type, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
75 void archiveNull (string type, string key); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
76 void archiveObject (string runtimeType, string type, string key, size_t id, void delegate () dg); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
77 void archivePointer (string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
78 void archiveReference (string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
79 //void archiveSlice (size_t length, size_t offset, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
80 void archiveStruct (string type, string key, size_t id, void delegate () dg); |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
81 void archiveTypedef (string type, string key, size_t id, void delegate () dg); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
82 |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
83 void archive (string value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
84 void archive (wstring value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
85 void archive (dstring value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
86 void archive (bool value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
87 void archive (byte value, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
88 //void archive (cdouble value, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
89 //void archive (cent value, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
90 //void archive (cfloat value, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
91 void archive (char value, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
92 //void archive (creal value, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
93 void archive (dchar value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
94 void archive (double value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
95 void archive (float value, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
96 //void archive (idouble value, string key, size_t id); |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
97 //void archive (ifloat value, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
98 void archive (int value, string key, size_t id); |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
99 //void archive (ireal value, string key, size_t id); |
24
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
100 void archive (long value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
101 void archive (real value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
102 void archive (short value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
103 void archive (ubyte value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
104 //void archive (ucent value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
105 void archive (uint value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
106 void archive (ulong value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
107 void archive (ushort value, string key, size_t id); |
55f0a9d5df8d
First step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
20
diff
changeset
|
108 void archive (wchar value, string key, size_t id); |
0 | 109 } |
110 | |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
111 abstract class Archive (U) : IArchive |
0 | 112 { |
9
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
113 version (Tango) alias U[] DataType; |
99c52d46822a
Serialization works now with D2, deserialization still doesn't work
Jacob Carlborg <doob@me.com>
parents:
0
diff
changeset
|
114 else mixin ("alias immutable(U)[] DataType;"); |
0 | 115 |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
9
diff
changeset
|
116 alias void delegate (ArchiveException exception, DataType[] data) ErrorCallback; |
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
9
diff
changeset
|
117 |
25
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
118 protected ErrorCallback errorCallback; |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
119 |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
120 protected this (ErrorCallback errorCallback) |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
121 { |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
122 this.errorCallback = errorCallback; |
b51e953f79eb
Second step in refactoring the API.
Jacob Carlborg <doob@me.com>
parents:
24
diff
changeset
|
123 } |
18
3d42ea434d46
Added an error callback. Fixes #3 and #4.
Jacob Carlborg <doob@me.com>
parents:
9
diff
changeset
|
124 |
0 | 125 protected DataType toDataType (T) (T value) |
126 { | |
127 try | |
128 return to!(DataType)(value); | |
129 | |
130 catch (ConversionException e) | |
131 throw new ArchiveException(e); | |
132 } | |
133 | |
134 protected T fromDataType (T) (DataType value) | |
135 { | |
136 try | |
137 return to!(T)(value); | |
138 | |
139 catch (ConversionException e) | |
140 throw new ArchiveException(e); | |
20
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
141 } |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
142 |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
143 protected bool isSliceOf (T, U = T) (T[] a, U[] b) |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
144 { |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
145 void* aPtr = a.ptr; |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
146 void* bPtr = b.ptr; |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
147 |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
148 return aPtr >= bPtr && aPtr + a.length * T.sizeof <= bPtr + b.length * U.sizeof; |
9a575087b961
Added support for slices. Strings and arrays are now treated as references.
Jacob Carlborg <doob@me.com>
parents:
18
diff
changeset
|
149 } |
0 | 150 } |