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