Mercurial > projects > orange
comparison orange/serialization/Serializer.d @ 18:3d42ea434d46
Added an error callback. Fixes #3 and #4.
author | Jacob Carlborg <doob@me.com> |
---|---|
date | Thu, 12 Aug 2010 23:24:51 +0200 |
parents | c4e7e64ffb67 |
children | 9a575087b961 |
comparison
equal
deleted
inserted
replaced
17:c4e7e64ffb67 | 18:3d42ea434d46 |
---|---|
35 | 35 |
36 class Serializer (ArchiveType : IArchive) | 36 class Serializer (ArchiveType : IArchive) |
37 { | 37 { |
38 static assert(isArchive!(ArchiveType), format!(`The type "`, ArchiveType, `" does not implement the necessary methods to be an archive.`)); | 38 static assert(isArchive!(ArchiveType), format!(`The type "`, ArchiveType, `" does not implement the necessary methods to be an archive.`)); |
39 | 39 |
40 alias ArchiveType.ErrorCallback ErrorCallback; | |
41 alias ArchiveType.DataType DataType; | |
42 | |
40 private | 43 private |
41 { | 44 { |
42 ArchiveType archive; | 45 ArchiveType archive; |
43 alias ArchiveType.DataType DataType; | |
44 | 46 |
45 RegisterBase[string] serializers; | 47 RegisterBase[string] serializers; |
46 RegisterBase[string] deserializers; | 48 RegisterBase[string] deserializers; |
47 | 49 |
48 size_t keyCounter; | 50 size_t keyCounter; |
49 | 51 |
50 bool hasBegunSerializing; | 52 bool hasBegunSerializing; |
51 bool hasBegunDeserializing; | 53 bool hasBegunDeserializing; |
54 | |
55 void delegate (ArchiveException exception, DataType[] data) throwOnErrorCallback; | |
56 void delegate (ArchiveException exception, DataType[] data) doNothingOnErrorCallback; | |
52 } | 57 } |
53 | 58 |
54 this () | 59 this () |
55 { | 60 { |
56 archive = new ArchiveType; | 61 archive = new ArchiveType; |
62 | |
63 throwOnErrorCallback = (ArchiveException exception, DataType[] data) { throw exception; }; | |
64 doNothingOnErrorCallback = (ArchiveException exception, DataType[] data) { /* do nothing */ }; | |
65 | |
66 setThrowOnErrorCallback(); | |
57 } | 67 } |
58 | 68 |
59 void registerSerializer (T) (string type, void delegate (T, Serializer, DataType) dg) | 69 void registerSerializer (T) (string type, void delegate (T, Serializer, DataType) dg) |
60 { | 70 { |
61 serializers[type] = toSerializeRegisterWrapper(dg); | 71 serializers[type] = toSerializeRegisterWrapper(dg); |
81 hasBegunSerializing = false; | 91 hasBegunSerializing = false; |
82 hasBegunDeserializing = false; | 92 hasBegunDeserializing = false; |
83 resetCounters(); | 93 resetCounters(); |
84 | 94 |
85 archive.reset; | 95 archive.reset; |
96 } | |
97 | |
98 ErrorCallback errorCallback () | |
99 { | |
100 return archive.errorCallback; | |
101 } | |
102 | |
103 ErrorCallback errorCallback (ErrorCallback errorCallback) | |
104 { | |
105 return archive.errorCallback = errorCallback; | |
106 } | |
107 | |
108 void setThrowOnErrorCallback () | |
109 { | |
110 errorCallback = throwOnErrorCallback; | |
111 } | |
112 | |
113 void setDoNothingOnErrorCallback () | |
114 { | |
115 errorCallback = doNothingOnErrorCallback; | |
86 } | 116 } |
87 | 117 |
88 DataType serialize (T) (T value, DataType key = null) | 118 DataType serialize (T) (T value, DataType key = null) |
89 { | 119 { |
90 if (!hasBegunSerializing) | 120 if (!hasBegunSerializing) |