1 /*---------------------------------------------------------------------------*
2 Project: NintendoWare
3 File: main.cpp
4
5 Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Revision: 13145 $
14 *---------------------------------------------------------------------------*/
15
16 //------------------------------------------------------------------
17 // デモ: RuntimeTypeInfo
18 //
19 // 概要
20 // クラスに実行時型情報を埋め込み安全なダウンキャストをおこなうサンプルです。
21 // Baseクラスのポインタが指しているAAAクラス、BBBクラス、CCC_AAAクラスの
22 // オブジェクトへのインスタンスを安全に派生クラスにキャストします。
23 //
24 // シリアル経由でのデバッグ出力のみのサンプルです。
25 //
26 //------------------------------------------------------------------
27
28 #include <nn.h>
29
30 #include <nw/ut/ut_RuntimeTypeInfo.h>
31
32 #include <nw/sdk.h>
33
34 // クラスBase
35 class Base
36 {
37 public:
38 NW_UT_RUNTIME_TYPEINFO;
39
Print()40 virtual void Print() { OS_Printf("I'm Base\n"); }
41 };
42
43 // クラスAAA
44 class AAA : public Base
45 {
46 public:
47 NW_UT_RUNTIME_TYPEINFO;
48
Print()49 virtual void Print() { OS_Printf("I'm AAA\n"); }
FuncAAA()50 void FuncAAA() { OS_Printf("do FuncAAA\n"); }
51 };
52
53 // クラスBBB
54 class BBB : public Base
55 {
56 public:
57 NW_UT_RUNTIME_TYPEINFO;
58
Print()59 virtual void Print() { OS_Printf("I'm BBB\n"); }
FuncBBB()60 void FuncBBB() { OS_Printf("do FuncBBB\n"); }
61 };
62
63 // クラスCCC
64 class CCC_AAA : public AAA
65 {
66 public:
67 NW_UT_RUNTIME_TYPEINFO;
68
Print()69 virtual void Print() { OS_Printf("I'm CCC\n"); }
FuncCCC()70 void FuncCCC() { OS_Printf("do FuncCCC\n"); }
71
72 };
73
74
75 // 実行時型情報の実体を定義します。
76 NW_UT_RUNTIME_TYPEINFO_ROOT_DEFINITION( Base );
77 NW_UT_RUNTIME_TYPEINFO_DEFINITION( AAA, Base );
78 NW_UT_RUNTIME_TYPEINFO_DEFINITION( BBB, Base );
79 NW_UT_RUNTIME_TYPEINFO_DEFINITION( CCC_AAA, AAA );
80
81
82
83
84 /*---------------------------------------------------------------------------*
85 Name: ObjTest
86
87 Description: オブジェクトのダウンキャストのテスト関数。
88 クラスAAA, BBB, CCC_AAAへのダウンキャストを試みて
89 キャストに成功した場合には派生クラスのメソッドを実行します。
90
91 Arguments: obj Baseの派生オブジェクトへのポインタ
92
93 Returns: None.
94 *---------------------------------------------------------------------------*/
95 static void
ObjTest(Base * obj)96 ObjTest( Base* obj )
97 {
98 AAA* pAAA;
99 BBB* pBBB;
100 CCC_AAA* pCCC;
101
102 OS_Printf("-----------------------------\n");
103 obj->Print();
104
105 // クラスAAAにキャスト
106 pAAA = nw::ut::DynamicCast<AAA*>( obj );
107 if ( pAAA )
108 {
109 pAAA->FuncAAA();
110 }
111 else
112 {
113 OS_Printf("Can't cast to AAA\n");
114 }
115
116 // クラスBBBにキャスト
117 pBBB = nw::ut::DynamicCast<BBB*>( obj );
118 if ( pBBB )
119 {
120 pBBB->FuncBBB();
121 }
122 else
123 {
124 OS_Printf("Can't cast to BBB\n");
125 }
126
127 // クラスCCC_AAAにキャスト
128 pCCC = nw::ut::DynamicCast<CCC_AAA*>( obj );
129 if ( pCCC )
130 {
131 pCCC->FuncCCC();
132 }
133 else
134 {
135 OS_Printf("Can't cast to CCC\n");
136 }
137 }
138
139
140
141 /*---------------------------------------------------------------------------*
142 Name: main
143
144 Description: サンプルのメイン関数です。
145
146 Arguments: なし。
147
148 Returns: なし。
149 *---------------------------------------------------------------------------*/
150 void
nnMain(void)151 nnMain( void )
152 {
153 OS_Init();
154
155 Base obj1;
156 AAA obj2;
157 BBB obj3;
158 CCC_AAA obj4;
159
160 ObjTest( &obj1 );
161 ObjTest( &obj2 );
162 ObjTest( &obj3 );
163 ObjTest( &obj4 );
164
165 OS_Exit(0);
166 }
167
168
169
170