從pdb讀取類聲明花了很久,從類聲明產(chǎn)生反射和dll接口花的時(shí)間更久啊,很多細(xì)節(jié)問題需要解決。文章的代碼已經(jīng)保存在了
Vczh Library++3.0(\Tools\Release\SideProjects\GacUI\GacUI.sln)。
反射和dll接口的工作進(jìn)行了一半。現(xiàn)在把類、函數(shù)、屬性和各種類型都聲稱了出來,但是還欠缺函數(shù)的實(shí)現(xiàn)。反射使用了lazy的做法,當(dāng)訪問到一個(gè)Type的成員的時(shí)候,才會(huì)對(duì)Type進(jìn)行初始化。不過初始化的內(nèi)容也很簡(jiǎn)單,只是把一些對(duì)方放倒一個(gè)列表里面,從而當(dāng)你想知道一個(gè)Type有哪些屬性和函數(shù)什么的可以很快查出來。dll的接口也差不多。現(xiàn)在分別展示反射和dll接口的代碼。首先是反射的:
下面是GuiBoundsComposition類型的反射。我們可以看到gacui_tpimp_GuiBoundsComposition繼承自了TypeDescriptor。當(dāng)我們調(diào)用GetTypeProvider->FindType(L"GuiBoundsComposition")->GetTypeDescriptor()的時(shí)候,就會(huì)返回下面的這個(gè)對(duì)象。
1 class gacui_tpimp_GuiBoundsComposition : public TypeDescriptor
2 {
3 protected:
4 void FillTypeContent()
5 {
6 AddBaseType((gacui_tpimp_type_cache_table.cache_GuiGraphicsSite));
7 AddConstructor(
8 (new MethodDescriptor(L"GuiBoundsComposition", IMemberDescriptor::Normal))
9 ->ReturnType((gacui_tpimp_type_cache_table.cache_GuiBoundsComposition)->GetPointerType())
10 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_GuiBoundsComposition_0))
11 );
12 AddMethod(
13 (new MethodDescriptor(L"GetAffectionFromParent", IMemberDescriptor::Virtual))
14 ->ReturnType((gacui_tpimp_type_cache_table.cache_GuiGraphicsComposition_member_ParentSizeAffection))
15 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_GetAffectionFromParent_1))
16 );
17 AddMethod(
18 (new MethodDescriptor(L"GetPreferredBounds", IMemberDescriptor::Virtual))
19 ->ReturnType((gacui_tpimp_type_cache_table.cache_Rect))
20 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_GetPreferredBounds_2))
21 );
22 AddMethod(
23 (new MethodDescriptor(L"GetBounds", IMemberDescriptor::Virtual))
24 ->ReturnType((gacui_tpimp_type_cache_table.cache_Rect))
25 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_GetBounds_3))
26 );
27 AddMethod(
28 (new MethodDescriptor(L"ClearAlignmentToParent", IMemberDescriptor::Normal))
29 ->ReturnType((gacui_tpimp_type_cache_table.primary_Void))
30 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_ClearAlignmentToParent_4))
31 );
32 AddMethod(
33 (new MethodDescriptor(L"IsAlignedToParent", IMemberDescriptor::Normal))
34 ->ReturnType((gacui_tpimp_type_cache_table.primary_Bool))
35 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_IsAlignedToParent_5))
36 );
37 AddMethod(
38 (new MethodDescriptor(L"operator=", IMemberDescriptor::Normal))
39 ->ReturnType((gacui_tpimp_type_cache_table.cache_GuiBoundsComposition)->GetReferenceType())
40 ->Parameter(L"value", (gacui_tpimp_type_cache_table.cache_GuiBoundsComposition)->GetConstReferenceType())
41 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_operator_assign_6))
42 );
43 AddMethod(
44 (new MethodDescriptor(L"SetBounds", IMemberDescriptor::Normal))
45 ->ReturnType((gacui_tpimp_type_cache_table.primary_Void))
46 ->Parameter(L"value", (gacui_tpimp_type_cache_table.cache_Rect))
47 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_SetBounds_7))
48 );
49 AddProperty(
50 (new PropertyDescriptor(L"AlignmentToParent", IMemberDescriptor::Normal))
51 ->PropertyType((gacui_tpimp_type_cache_table.cache_Margin))
52 ->Getter(
53 (new MethodDescriptor(L"GetAlignmentToParent", IMemberDescriptor::Normal))
54 ->ReturnType((gacui_tpimp_type_cache_table.cache_Margin))
55 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_GetAlignmentToParent_8))
56 )
57 ->Setter(
58 (new MethodDescriptor(L"SetAlignmentToParent", IMemberDescriptor::Normal))
59 ->ReturnType((gacui_tpimp_type_cache_table.primary_Void))
60 ->Parameter(L"value", (gacui_tpimp_type_cache_table.cache_Margin))
61 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_SetAlignmentToParent_9))
62 )
63 );
64 AddProperty(
65 (new PropertyDescriptor(L"BoundsChanged", IMemberDescriptor::Normal))
66 ->PropertyType((gacui_tpimp_type_cache_table.cache_GuiGraphicsEvent_of_GuiEventArgs))
67 ->Getter(
68 (new MethodDescriptor(L"get_BoundsChanged", IMemberDescriptor::Normal))
69 ->ReturnType((gacui_tpimp_type_cache_table.cache_GuiGraphicsEvent_of_GuiEventArgs))
70 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_get_BoundsChanged_10))
71 )
72 ->Setter(
73 (new MethodDescriptor(L"set_BoundsChanged", IMemberDescriptor::Normal))
74 ->ReturnType((gacui_tpimp_type_cache_table.primary_Void))
75 ->Parameter(L"value", (gacui_tpimp_type_cache_table.cache_GuiGraphicsEvent_of_GuiEventArgs))
76 ->Handler(MethodDescriptor::HandlerFuncType(&gacui_tpimp_GuiBoundsComposition::method_handler_set_BoundsChanged_11))
77 )
78 );
79 }
80
81 private:
82
83 static DescriptableValue method_handler_GuiBoundsComposition_0(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
84 {
85 throw 0;
86 }
87
88 static DescriptableValue method_handler_GetAffectionFromParent_1(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
89 {
90 throw 0;
91 }
92
93 static DescriptableValue method_handler_GetPreferredBounds_2(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
94 {
95 throw 0;
96 }
97
98 static DescriptableValue method_handler_GetBounds_3(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
99 {
100 throw 0;
101 }
102
103 static DescriptableValue method_handler_ClearAlignmentToParent_4(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
104 {
105 throw 0;
106 }
107
108 static DescriptableValue method_handler_IsAlignedToParent_5(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
109 {
110 throw 0;
111 }
112
113 static DescriptableValue method_handler_operator_assign_6(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
114 {
115 throw 0;
116 }
117
118 static DescriptableValue method_handler_SetBounds_7(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
119 {
120 throw 0;
121 }
122
123 static DescriptableValue method_handler_GetAlignmentToParent_8(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
124 {
125 throw 0;
126 }
127
128 static DescriptableValue method_handler_SetAlignmentToParent_9(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
129 {
130 throw 0;
131 }
132
133 static DescriptableValue method_handler_get_BoundsChanged_10(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
134 {
135 throw 0;
136 }
137
138 static DescriptableValue method_handler_set_BoundsChanged_11(const DescriptableValue& thisObject, const collections::IReadonlyList<DescriptableValue>& parameters)
139 {
140 throw 0;
141 }
142
143 public:
144 };
如果反射出來的函數(shù)不能被調(diào)用那自然是沒有意義的,所以AddMethod的時(shí)候會(huì)提供一個(gè)handler,而handler就是下面的靜態(tài)成員函數(shù)了。靜態(tài)成員函數(shù)的實(shí)現(xiàn)就是從thisObject拿到GuiBoundsComposition*之后,去調(diào)用對(duì)應(yīng)的函數(shù)。這個(gè)部分是可以自動(dòng)生成的,只是現(xiàn)在還沒寫完。
反射只是一部分,dll的接口也要生成,因?yàn)镚acUI的實(shí)現(xiàn)使用了很多模板和自定義異常,而模板和異常跨越dll邊界是很危險(xiǎn)的,所以需要用自動(dòng)生成的代碼來把這些東西隔離開(包括一場(chǎng)),從而實(shí)現(xiàn)安全調(diào)用。下面就是自動(dòng)生成的dll接口代碼,而同樣,函數(shù)也暫時(shí)沒有實(shí)現(xiàn)的內(nèi)容:
1 /***********************************************************************
2 GuiBoundsComposition
3 ***********************************************************************/
4
5 class GACUI_API GuiBoundsComposition : public GuiGraphicsSite
6 {
7 public:
8
9 public:
10
11 static rptr<GuiBoundsComposition> Create();
12
13 GuiGraphicsComposition :: ParentSizeAffection GetAffectionFromParent();
14 Rect GetPreferredBounds();
15 Rect GetBounds();
16 void ClearAlignmentToParent();
17 bool IsAlignedToParent();
18 rptr<GuiBoundsComposition> operator=(rptr<GuiBoundsComposition> value);
19 void SetBounds(Rect value);
20
21 Margin GetAlignmentToParent();
22 void SetAlignmentToParent(Margin value);
23 GuiGraphicsEvent_of_GuiEventArgs on_BoundsChanged();
24
25 };
26
27 /***********************************************************************
28 GuiBoundsComposition
29 ***********************************************************************/
30
31 rptr<GuiBoundsComposition> GuiBoundsComposition::Create()
32 {
33 throw 0;
34 }
35
36 GuiGraphicsComposition :: ParentSizeAffection GuiBoundsComposition::GetAffectionFromParent()
37 {
38 throw 0;
39 }
40
41 Rect GuiBoundsComposition::GetPreferredBounds()
42 {
43 throw 0;
44 }
45
46 Rect GuiBoundsComposition::GetBounds()
47 {
48 throw 0;
49 }
50
51 void GuiBoundsComposition::ClearAlignmentToParent()
52 {
53 throw 0;
54 }
55
56 bool GuiBoundsComposition::IsAlignedToParent()
57 {
58 throw 0;
59 }
60
61 rptr<GuiBoundsComposition> GuiBoundsComposition::operator=(rptr<GuiBoundsComposition> value)
62 {
63 throw 0;
64 }
65
66 void GuiBoundsComposition::SetBounds(Rect value)
67 {
68 throw 0;
69 }
70
71 Margin GuiBoundsComposition::GetAlignmentToParent()
72 {
73 throw 0;
74 }
75 void GuiBoundsComposition::SetAlignmentToParent(Margin value)
76 {
77 throw 0;
78 }
79
80 GuiGraphicsEvent_of_GuiEventArgs GuiBoundsComposition::on_BoundsChanged()
81 {
82 throw 0;
83 }
dll接口的部分生成比較簡(jiǎn)單,因?yàn)轭愃苅nt這樣的類型可以直接使用,而不像反射一樣還得給一個(gè)對(duì)象來告訴你這個(gè)是int。
之所要這兩部分,是因?yàn)镚acUI不僅允許用戶直接操作某個(gè)控件,也需要同時(shí)允許界面使用XML描述,或者以后對(duì)腳本的支持。C++訪問控件不需要反射,而XML和腳本則需要。XML描述界面是很重要的,因?yàn)檫@不僅可以用來支持皮膚、資源等高級(jí)抽象,還可以圍繞XML開發(fā)一個(gè)跟Blend Expression(當(dāng)然不可能有那么高級(jí)= =b)的GUI編輯器。
接下來會(huì)繼續(xù)給這些函數(shù)生成實(shí)現(xiàn)。這部分完成之后,GacUI就真正可以通過dll的方法來運(yùn)行了。一旦把功能都坐進(jìn)了dll,那么實(shí)現(xiàn)類似visual studio那樣的可以把界面插件分散在各個(gè)dll里面的編輯器框架,也消除了技術(shù)上的困難。
posted on 2012-02-21 10:33
陳梓瀚(vczh) 閱讀(3552)
評(píng)論(3) 編輯 收藏 引用 所屬分類:
GacUI