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