comparison orange/serialization/Serializable.d @ 0:f7b078e85f7f

First commit
author Jacob Carlborg <doob@me.com>
date Wed, 26 May 2010 17:19:13 +0200
parents
children 511d1ef4e299
comparison
equal deleted inserted replaced
-1:000000000000 0:f7b078e85f7f
1 /**
2 * Copyright: Copyright (c) 2010 Jacob Carlborg.
3 * Authors: Jacob Carlborg
4 * Version: Initial created: Jan 26, 2010
5 * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6 */
7 module orange.serialization.Serializable;
8
9 import orange.serialization.archives.Archive;
10 import orange.serialization.Events;
11 import orange.util.CTFE;
12
13 template Serializable ()
14 {
15 void toData (T) (T archive, T.DataType key)
16 {
17 alias typeof(this) ThisType;
18
19 foreach (i, dummy ; typeof(T.tupleof))
20 {
21 alias typeof(ThisType.tupleof[i]) FieldType;
22
23 const field = nameOfFieldAt!(ThisType, i);
24 auto value = getValueOfField!(ThisType, FieldType, field)(this);
25
26 archive.archive(value, field);
27 }
28 }
29
30 void fromData (T) (T archive, T.DataType key)
31 {
32 alias typeof(this) ThisType;
33
34 foreach (i, dummy ; typeof(ThisType.tupleof))
35 {
36 alias typeof(ThisType.tupleof[i]) FieldType;
37
38 const field = nameOfFieldAt!(ThisType, i);
39 auto value = archive.unarchive!(FieldType)(field);
40
41 setValueOfField!(FieldType, ThisType, field)(this, value);
42 }
43 }
44 }
45
46 template NonSerialized (alias field)
47 {
48 NonSerializedField!(field) __nonSerialized;
49 }
50
51 struct NonSerializedField (alias f)
52 {
53 const field = f.stringof;
54 }
55
56 package const nonSerializedField = "__nonSerialized";
57 package const serializedField = "__serialized";
58 package const internalFields = [nonSerializedField[], onDeserializedField, onDeserializingField, onSerializedField, onSerializingField];