1 /**
2  * Copyright: Copyright (c) 2010-2011 Jacob Carlborg.
3  * Authors: Jacob Carlborg
4  * Version: Initial created: Feb 4, 2010
5  * License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
6  */
7 module orange.serialization.RegisterWrapper;
8 
9 import orange.serialization.Serializer;
10 
11 /**
12  * This class is the base of all register wrappers. A register wrapper wraps a function
13  * that is registered with the serializer. The serializer calls this function to perform
14  * custom (de)serialization when needed.
15  */
16 class RegisterBase { }
17 
18 /**
19  * This class wraps registered functions for serialization.
20  *
21  * Params:
22  *     T = the type of the class or struct which is serialized
23  */
24 class SerializeRegisterWrapper (T) : RegisterBase
25 {
26     private void delegate (T, Serializer, Serializer.Data) dg;
27     private bool isDelegate;
28 
29     /**
30      * Creates a new instance of this class with the given delegate that performs the
31      * custom serialization.
32      *
33      *
34      * Params:
35      *     dg = the delegate to call when performing custom serialization
36      */
37     this (void delegate (T, Serializer, Serializer.Data) dg)
38     {
39         isDelegate = true;
40         this.dg = dg;
41     }
42 
43     /**
44      * Creates a new instance of this class with the given function that performs the
45      * custom serialization.
46      *
47      *
48      * Params:
49      *     func = the delegate to call when performing custom serialization
50      */
51     this (void function (T, Serializer, Serializer.Data) func)
52     {
53         dg.funcptr = func;
54     }
55 
56     /**
57      * Calls the function to perform the custom serialization.
58      *
59      * Params:
60      *     value = the instance that is to be serialized
61      *     serializer = the serializer that performs the serialization
62      *     key = the key of the given value
63      */
64     void opCall (T value, Serializer serializer, Serializer.Data key)
65     {
66         if (dg && isDelegate)
67             dg(value, serializer, key);
68 
69         else if (dg)
70             dg.funcptr(value, serializer, key);
71     }
72 }
73 
74 /// Ditto
75 class SerializeRegisterWrapperRef (T) : RegisterBase
76 {
77     private void delegate (ref T, Serializer, Serializer.Data) dg;
78     private bool isDelegate;
79 
80     /**
81      * Creates a new instance of this class with the given delegate that performs the
82      * custom serialization.
83      *
84      *
85      * Params:
86      *     dg = the delegate to call when performing custom serialization
87      */
88     this (void delegate (ref T, Serializer, Serializer.Data) dg)
89     {
90         isDelegate = true;
91         this.dg = dg;
92     }
93 
94     /**
95      * Creates a new instance of this class with the given function that performs the
96      * custom serialization.
97      *
98      *
99      * Params:
100      *     func = the delegate to call when performing custom serialization
101      */
102     this (void function (ref T, Serializer, Serializer.Data) func)
103     {
104         dg.funcptr = func;
105     }
106 
107     /**
108      * Calls the function to perform the custom serialization.
109      *
110      * Params:
111      *     value = the instance that is to be serialized
112      *     serializer = the serializer that performs the serialization
113      *     key = the key of the given value
114      */
115     void opCall (ref T value, Serializer serializer, Serializer.Data key)
116     {
117         if (dg && isDelegate)
118             dg(value, serializer, key);
119 
120         else if (dg)
121             dg.funcptr(value, serializer, key);
122     }
123 }
124 
125 /**
126  * This class wraps registered functions for deserialization.
127  *
128  * Params:
129  *     T = the type of the class or struct which is deserialized
130  */
131 class DeserializeRegisterWrapper (T) : RegisterBase
132 {
133     private void delegate (ref T, Serializer, Serializer.Data) dg;
134     private bool isDelegate;
135 
136     /**
137      * Creates a new instance of this class with the given delegate that performs the
138      * custom deserialization.
139      *
140      *
141      * Params:
142      *     dg = the delegate to call when performing custom serialization
143      */
144     this (void delegate (ref T, Serializer, Serializer.Data) dg)
145     {
146         isDelegate = true;
147         this.dg = dg;
148     }
149 
150     /**
151      * Creates a new instance of this class with the given function that performs the
152      * custom serialization.
153      *
154      *
155      * Params:
156      *     func = the delegate to call when performing custom serialization
157      */
158     this (void function (ref T, Serializer, Serializer.Data) func)
159     {
160         dg.funcptr = func;
161     }
162 
163     /**
164      * Calls the function to perform the custom deserialization.
165      *
166      * Params:
167      *     value = the instance that is to be deserialized
168      *     serializer = the serializer that performs the deserialization
169      *     key = the key of the given value
170      */
171     void opCall (ref T value, Serializer serializer, Serializer.Data key)
172     {
173         if (dg && isDelegate)
174             dg(value, serializer, key);
175 
176         if (dg)
177             dg.funcptr(value, serializer, key);
178     }
179 }