1 /*---------------------------------------------------------------------------* 2 Project: Horizon 3 File: util_Singleton.h 4 5 Copyright (C) Nintendo. 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 $Rev: 27772 $ 14 *---------------------------------------------------------------------------*/ 15 16 #ifndef NN_UTIL_UTIL_SINGLETON_H_ 17 #define NN_UTIL_UTIL_SINGLETON_H_ 18 19 #ifdef __cplusplus 20 21 #include <nn/assert.h> 22 #include <nn/util/util_Result.h> 23 #include <nn/util/util_NonCopyable.h> 24 25 namespace nn { namespace util { 26 27 //--------------------------------------------------------------------------- 28 //! :category Utility class template. 29 //! 30 //! @tparam Derived class type that inherits from this class. 31 //! 32 //! @brief Class template for creating classes that use the <tt>@ref Singleton</tt> pattern. 33 //! 34 //! class A : To ensure that only one instance of the "A" class can be created, define your class as <tt>public nn::util::Singleton< A ></tt>. 35 //! 36 //--------------------------------------------------------------------------- 37 template <class Derived> 38 class Singleton : private nn::util::NonCopyable<Singleton<Derived> > 39 { 40 public: GetInstance(void)41 static Derived& GetInstance(void) 42 { 43 NN_TASSERT_(ms_Singleton); 44 return(*ms_Singleton); 45 } 46 47 protected: Singleton()48 Singleton() 49 { 50 NN_TASSERTMSG_(ms_Singleton == 0, "This singleton constructor is called twice."); 51 ms_Singleton = static_cast<Derived*>(this); 52 } 53 ~Singleton()54 ~Singleton() 55 { 56 NN_TASSERT_(ms_Singleton); 57 ms_Singleton = 0; 58 } 59 60 private: 61 static Derived* ms_Singleton; 62 }; 63 64 template <class Derived> Derived* Singleton <Derived>::ms_Singleton = 0; 65 66 }} 67 68 #endif // __cplusplus 69 70 #endif /* NN_UTIL_SINGLETON_H_ */ 71