1 /**
2  * Copyright: Copyright (c) 2010-2011 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 mambo.serialization.Events;
8 
9 import mambo.util._;
10 
11 /**
12  * This event is triggered after the struct/class, this template has been mixed into,
13  * has been completely deserialized, including all the fields.
14  *
15  * Params:
16  *     method = the method to be invoked when the event is triggered
17  */
18 template OnDeserialized (alias method)
19 {
20 	static mambo.serialization.Events.Event!(method) __onDeserialized;
21 }
22 
23 /**
24  * This event is triggered after the struct/class (that this template has been mixed into)
25  * has been deserialized, but before any fields have been deserialized.
26  *
27  * Params:
28  *     method = the method to be invoked when the event is triggered
29  */
30 template OnDeserializing (alias method)
31 {
32 	static mambo.serialization.Events.Event!(method) __onDeserializing;
33 }
34 
35 /**
36  * This event is triggered after the struct/class (that this template has been mixed into)
37  * has been completely serialized, including all the fields.
38  *
39  * Params:
40  *     method = the method to be invoked when the event is triggered
41  */
42 template OnSerialized (alias method)
43 {
44 	static mambo.serialization.Events.Event!(method) __onSerialized;
45 }
46 
47 /**
48  * This event is triggered after the struct/class (that this template has been mixed into)
49  * has been serialized, but before any fields have been serialized.
50  *
51  * Params:
52  *     method = the method to be invoked when the event is triggered
53  */
54 template OnSerializing (alias method)
55 {
56 	static mambo.serialization.Events.Event!(method) __onSerializing;
57 }
58 
59 /**
60  * This struct represents an event.
61  *
62  * Params:
63  *     m = the method to be invoked when the event is triggered
64  */
65 struct Event (alias m)
66 {
67 	private enum method = &m;
68 
69 	/**
70 	 * Triggers the event on the given value.
71 	 *
72 	 * Params:
73 	 *     value = the object to trigger the event on
74 	 */
75 	void opCall (T) (T value)
76 	{
77 		void delegate () dg;
78 		dg.ptr = cast(void*) value;
79 		dg.funcptr = method;
80 		dg();
81 	}
82 }
83 
84 package:
85 
86 const onDeserializedField = "__onDeserialized";
87 const onDeserializingField = "__onDeserializing";
88 const onSerializedField = "__onSerialized";
89 const onSerializingField = "__onSerializing";