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 mambo.serialization.RegisterWrapper;
8 
9 import mambo.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 	 *     dg = 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 /**
75  * This class wraps registered functions for deserialization.
76  *
77  * Params:
78  *     T = the type of the class or struct which is deserialized
79  */
80 class DeserializeRegisterWrapper (T) : RegisterBase
81 {
82 	private void delegate (ref T, Serializer, Serializer.Data) dg;
83 	private bool isDelegate;
84 
85 	/**
86 	 * Creates a new instance of this class with the given delegate that performs the
87 	 * custom deserialization.
88 	 *
89 	 *
90 	 * Params:
91 	 *     dg = the delegate to call when performing custom serialization
92 	 */
93 	this (void delegate (ref T, Serializer, Serializer.Data) dg)
94 	{
95 		isDelegate = true;
96 		this.dg = dg;
97 	}
98 
99 	/**
100 	 * Creates a new instance of this class with the given function that performs the
101 	 * custom serialization.
102 	 *
103 	 *
104 	 * Params:
105 	 *     dg = the delegate to call when performing custom serialization
106 	 */
107 	this (void function (ref T, Serializer, Serializer.Data) func)
108 	{
109 		dg.funcptr = func;
110 	}
111 
112 	/**
113 	 * Calls the function to perform the custom deserialization.
114 	 *
115 	 * Params:
116 	 *     value = the instance that is to be deserialized
117 	 *     serializer = the serializer that performs the deserialization
118 	 *     key = the key of the given value
119 	 */
120 	void opCall (ref T value, Serializer serializer, Serializer.Data key)
121 	{
122 		if (dg && isDelegate)
123 			dg(value, serializer, key);
124 
125 		if (dg)
126 			dg.funcptr(value, serializer, key);
127 	}
128 }