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 // デモ: SharedPtr
18 //
19 // 概要
20 //   nw::ut::SharedPtr クラスのデモです。
21 //   テンプレート引数に LockObject を指定するスレッドセーフ版と、
22 //   デフォルトの非スレッドセーフ版の SharedPtr を作成します。
23 //------------------------------------------------------------------
24 
25 #include <nn.h>
26 
27 #include <nw/ut/ut_SharedPtr.h>
28 #include <nw/demo/demo_SimpleApp.h>
29 
30 #include <nw/sdk.h>
31 
32 // クラスBase
33 class Base
34 {
35 public:
~Base()36     virtual ~Base() {}
37 };
38 
39 // クラスAAA
40 class AAA : public Base
41 {
42 public:
~AAA()43     virtual ~AAA()
44     {
45         NW_LOG("destruct AAA\n");
46     }
47 };
48 
49 // クラスBBB
50 class BBB : public Base
51 {
52 public:
~BBB()53     virtual ~BBB()
54     {
55         NW_LOG("destruct BBB\n");
56     }
57 };
58 
59 /*---------------------------------------------------------------------------*
60   Name:         main
61 
62   Description:  サンプルのメイン関数です。
63 
64   Arguments:    なし。
65 
66   Returns:      なし。
67  *---------------------------------------------------------------------------*/
68 void
nnMain(void)69 nnMain( void )
70 {
71     nw::demo::DemoAllocator allocator;
72 
73     nn::os::Initialize();
74     nw::demo::InitializeDemoMemory();
75     nw::demo::InitializeDemoAllocator(&allocator, nw::demo::DEMO_MEMORY_SIZE, nn::os::ALLOCATE_OPTION_LINEAR);
76 
77     nw::ut::WeakPtr<AAA> pWeakAAA;
78     nw::ut::WeakPtr<Base, nw::os::LockObject> pWeakBase;
79     nw::ut::SharedPtr<Base> pBaseA;
80 
81     {
82         AAA*     objA = new(allocator.Alloc(sizeof(AAA))) AAA();
83         BBB*     objB = new(allocator.Alloc(sizeof(BBB))) BBB();
84 
85         nw::ut::SharedPtr<AAA>  pAAA = nw::ut::SharedPtr<AAA>( objA, &allocator );
86         nw::ut::SharedPtr<BBB, nw::os::LockObject> pBBB = nw::ut::SharedPtr<BBB, nw::os::LockObject>( objB, &allocator );
87 
88         // pWeakBase = pAAA; // Error! 排他方式の違うものへの代入はコンパイルエラー。
89 
90         pBaseA = pAAA;
91         pWeakAAA = pAAA;
92         pWeakBase = pBBB;
93 
94         if (pAAA == pBaseA)
95         {
96             NW_LOG("pAAA == pBaseA\n");
97         }
98 
99         if (pBaseA == pWeakAAA.Lock())
100         {
101             NW_LOG("pBaseA == pWeakAAA\n");
102         }
103 
104         if (pBBB == pWeakBase.Lock())
105         {
106             NW_LOG("pBBB == pWeakBBB\n");
107         }
108     }
109 
110     if ( pWeakAAA.Lock() )
111     {
112         NW_LOG("AAA is not released\n");
113     }
114     else
115     {
116         NW_LOG("AAA is already released\n");
117     }
118 
119     if ( pWeakBase.Lock() )
120     {
121         NW_LOG("BBB is not released\n");
122     }
123     else
124     {
125         NW_LOG("BBB is already released\n");
126     }
127 
128     OS_Exit(0);
129 }
130 
131 
132 
133