1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     util_Singleton.h
4 
5   Copyright (C)2009 Nintendo Co., Ltd.  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: 12449 $
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 template <class Derived>
28 class Singleton : private nn::util::NonCopyable<Singleton<Derived> >
29 {
30 public:
GetInstance(void)31     static Derived& GetInstance(void)
32     {
33         NN_TASSERT_(ms_Singleton);
34         return(*ms_Singleton);
35     }
36 
37 protected:
Singleton()38     Singleton()
39     {
40         NN_TASSERTMSG_(ms_Singleton == 0, "This singleton constructor is called twice.");
41         ms_Singleton = static_cast<Derived*>(this);
42     }
43 
~Singleton()44     ~Singleton()
45     {
46         NN_TASSERT_(ms_Singleton);
47         ms_Singleton = 0;
48     }
49 
50 private:
51         static Derived* ms_Singleton;
52 
53 };
54 
55 template <class Derived> Derived* Singleton <Derived>::ms_Singleton = 0;
56 
57 }}
58 
59 #endif // __cplusplus
60 
61 #endif /* NN_UTIL_SINGLETON_H_ */
62