1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
//
// ItemTemplate.cs
//
// Author:
// Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
// Copyright (c) 2013-2017 Jean-Philippe Bruyère
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Crow
{
/// <summary> Test func on data, return yes if there's children </summary>
public delegate bool BooleanTestOnInstance(object instance);
/// <summary>
/// Derived from Instantiator with sub data fetching facilities for hierarchical data access.
/// </summary>
public class ItemTemplate : Instantiator {
public EventHandler Expand;
public BooleanTestOnInstance HasSubItems;
string strDataType;
string fetchMethodName;
#region CTOR
public ItemTemplate(string path, string _dataType = null, string _fetchDataMethod = null)
: base(path) {
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
}
public ItemTemplate (Stream ImlFragment, string _dataType, string _fetchDataMethod)
:base(ImlFragment)
{
strDataType = _dataType;
fetchMethodName = _fetchDataMethod;
}
#endregion
public void CreateExpandDelegate (TemplatedGroup host){
Type dataType = CompilerServices.tryGetType(strDataType);
if (dataType == null) {
Debug.WriteLine ("ItemTemplate error: DataType not found: {0}.", strDataType);
return;
}
Type tmpGrpType = typeof(TemplatedGroup);
Type evtType = typeof(EventHandler);
PropertyInfo piData = tmpGrpType.GetProperty ("Data");
MethodInfo evtInvoke = evtType.GetMethod ("Invoke");
ParameterInfo [] evtParams = evtInvoke.GetParameters ();
Type handlerArgsType = evtParams [1].ParameterType;
Type [] args = { CompilerServices.TObject, CompilerServices.TObject, handlerArgsType };
#region Expand dyn meth
//DM is bound to templatedGroup root (arg0)
//arg1 is the sender of the expand event
DynamicMethod dm = new DynamicMethod ("dyn_expand_" + fetchMethodName,
typeof (void), args, true);
System.Reflection.Emit.Label gotoEnd;
System.Reflection.Emit.Label ifDataIsNull;
ILGenerator il = dm.GetILGenerator (256);
il.DeclareLocal(typeof(GraphicObject));
gotoEnd = il.DefineLabel ();
ifDataIsNull = il.DefineLabel ();
il.Emit (OpCodes.Ldarg_1);//load sender of expand event
il.Emit(OpCodes.Ldstr, "List");
il.Emit (OpCodes.Callvirt, CompilerServices.miFindByName);
il.Emit (OpCodes.Stloc_0);
//check that 'Data' of list is not already set
il.Emit (OpCodes.Ldloc_0);
il.Emit (OpCodes.Callvirt, piData.GetGetMethod ());
il.Emit (OpCodes.Brfalse, ifDataIsNull);
il.Emit (OpCodes.Br, gotoEnd);
il.MarkLabel(ifDataIsNull);
//copy the ref of ItemTemplates list TODO: maybe find another way to share it among the nodes?
FieldInfo fiTemplates = tmpGrpType.GetField("ItemTemplates");
il.Emit (OpCodes.Ldloc_0);
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Ldfld, fiTemplates);
il.Emit (OpCodes.Stfld, fiTemplates);
//call 'fetchMethodName' from the dataSource to build the sub nodes list
il.Emit (OpCodes.Ldloc_0);//push 'List' (of sub nodes) into the stack
il.Emit (OpCodes.Ldarg_1);//get the dataSource of the sender
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataSource);
if (fetchMethodName != "self")//special keyword self allows the use of recurent list<<<
emitGetSubData(il, dataType);
//set 'return' from the fetch method as 'data' of the list
il.Emit (OpCodes.Callvirt, piData.GetSetMethod ());
il.MarkLabel(gotoEnd);
il.Emit (OpCodes.Ret);
Expand = (EventHandler)dm.CreateDelegate (evtType, host);
#endregion
#region Items counting dyn method
//dm is unbound, arg0 is instance of Item container to expand
dm = new DynamicMethod ("dyn_count_" + fetchMethodName,
typeof (bool), new Type[] {CompilerServices.TObject}, true);
il = dm.GetILGenerator (256);
//get the dataSource of the arg0
il.Emit (OpCodes.Ldarg_0);
il.Emit (OpCodes.Callvirt, CompilerServices.miGetDataSource);
if (fetchMethodName != "self")
emitGetSubData(il, dataType);
il.Emit (OpCodes.Callvirt, CompilerServices.miGetColCount);
il.Emit (OpCodes.Ldc_I4_0);
il.Emit (OpCodes.Cgt);
il.Emit (OpCodes.Ret);
HasSubItems = (BooleanTestOnInstance)dm.CreateDelegate (typeof(BooleanTestOnInstance));
#endregion
}
void emitGetSubData(ILGenerator il, Type dataType){
MethodInfo miGetDatas = dataType.GetMethod (fetchMethodName, new Type[] {});
if (miGetDatas == null)
miGetDatas = CompilerServices.SearchExtMethod (dataType, fetchMethodName);
if (miGetDatas == null) {//in last resort, search among properties
PropertyInfo piDatas = dataType.GetProperty (fetchMethodName);
if (piDatas == null) {
FieldInfo fiDatas = dataType.GetField (fetchMethodName);
if (fiDatas == null)//and among fields
throw new Exception ("Fetch data member not found in ItemTemplate: " + fetchMethodName);
il.Emit (OpCodes.Ldfld, fiDatas);
return;
}
miGetDatas = piDatas.GetGetMethod ();
if (miGetDatas == null)
throw new Exception ("Read only property for fetching data in ItemTemplate: " + fetchMethodName);
}
il.Emit (OpCodes.Callvirt, miGetDatas);
}
}
}
|