/*---------------------------------------------------------------------------* Project: NintendoWare File: main.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. 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. $Revision: 13145 $ *---------------------------------------------------------------------------*/ //------------------------------------------------------------------ // デモ: 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 ); // pWeakBase = pAAA; // Error! 排他方式の違うものへの代入はコンパイルエラー。 pBaseA = pAAA; pWeakAAA = pAAA; pWeakBase = pBBB; if (pAAA == pBaseA) { NW_LOG("pAAA == pBaseA\n"); } if (pBaseA == pWeakAAA.Lock()) { NW_LOG("pBaseA == pWeakAAA\n"); } if (pBBB == pWeakBase.Lock()) { NW_LOG("pBBB == pWeakBBB\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); }