/*---------------------------------------------------------------------------* Project: NintendoWare File: main.cpp Copyright (C)2009-2011 Nintendo/HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo and/or its licensed developers and are protected by national and international copyright laws. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. The content herein is highly confidential and should be handled accordingly. $Revision: $ *---------------------------------------------------------------------------*/ //------------------------------------------------------------------ // デモ: SharedPtr // // 概要 // nw::ut::SharedPtr クラスのデモです。 // テンプレート引数に LockObject を指定するスレッドセーフ版と、 // デフォルトの非スレッドセーフ版の SharedPtr を作成します。 //------------------------------------------------------------------ #include #include #include #include // クラスBase class Base { public: virtual ~Base() {} }; // クラスAAA class AAA : public Base { public: virtual ~AAA() { NW_LOG("destruct AAA\n"); } }; // クラスBBB class BBB : public Base { public: virtual ~BBB() { NW_LOG("destruct BBB\n"); } }; /*---------------------------------------------------------------------------* Name: main Description: サンプルのメイン関数です。 Arguments: なし。 Returns: なし。 *---------------------------------------------------------------------------*/ void nnMain( void ) { nw::demo::DemoAllocator allocator; nn::os::Initialize(); nw::demo::InitializeDemoMemory(); nw::demo::InitializeDemoAllocator(&allocator, nw::demo::DEMO_MEMORY_SIZE, nn::os::ALLOCATE_OPTION_LINEAR); nw::ut::WeakPtr pWeakAAA; nw::ut::WeakPtr pWeakBase; nw::ut::SharedPtr pBaseA; { AAA* objA = new(allocator.Alloc(sizeof(AAA))) AAA(); BBB* objB = new(allocator.Alloc(sizeof(BBB))) BBB(); nw::ut::SharedPtr pAAA = nw::ut::SharedPtr( objA, &allocator ); nw::ut::SharedPtr pBBB = nw::ut::SharedPtr( objB, &allocator ); nw::ut::WeakPtr pWeakBBB; // pWeakBase = pAAA; // Error! 排他方式の違うものへの代入はコンパイルエラー。 pBaseA = pAAA; pWeakAAA = pAAA; pWeakBBB = pBBB; pWeakBase = pWeakBBB; if (pAAA == pBaseA) { NW_LOG("pAAA == pBaseA\n"); } if (pBaseA == pWeakAAA.Lock()) { NW_LOG("pBaseA == pWeakAAA\n"); } if (pBBB == pWeakBBB.Lock()) { NW_LOG("pBBB == pWeakBBB\n"); } if (pBBB == pWeakBase.Lock()) { NW_LOG("pBBB == pWeakBase\n"); } if (pWeakBBB.Lock() == pWeakBase.Lock()) { NW_LOG("pWeakBBB == pWeakBase\n"); } } if ( pWeakAAA.Lock() ) { NW_LOG("AAA is not released\n"); } else { NW_LOG("AAA is already released\n"); } if ( pWeakBase.Lock() ) { NW_LOG("BBB is not released\n"); } else { NW_LOG("BBB is already released\n"); } OS_Exit(0); }