CTR Pia  4.11.3
Game Communication Engine
common_Singleton.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: common_Singleton.h
4 
5  Copyright 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 
14 
15 #pragma once
16 
17 #include <pia/common/common_definitions.h>
18 
19 #include <pia/common/common_Api.h>
20 
21 
22 // Add this macro near the start of the CPP file that corresponds to the singleton.
23 #define NN_PIA_COMMON_SINGLETON_DEFINE(CLASSNAME) CLASSNAME* CLASSNAME::s_pInstance = NULL
24 
25 
26 // For this macro, fill in the CPP file that corresponds to the singleton.
27 #define NN_PIA_COMMON_SINGLETON_CREATE_INSTANCE_IMPL(CLASSNAME) \
28  \
29 nn::Result CLASSNAME::CreateInstance(void) \
30  \
31 { \
32  if (!IsInitialized()) \
33  { \
34  PIA_RETURN_RESULT_WITH_LOG(ResultNotInitialized, "PiaCommon is not initialized."); \
35  } \
36  if (!IsInSetupMode()) \
37  { \
38  PIA_RETURN_RESULT_WITH_LOG(ResultInvalidState, "Singleton must be created between nn::pia::common::BeginSetup() and nn::pia::common::EndSetup()."); \
39  } \
40  if (s_pInstance) \
41  { \
42  PIA_RETURN_RESULT_WITH_LOG(ResultAlreadyExists, "Singleton is already created."); \
43  } \
44  \
45 /* <tt>RootObject::new</tt> is used if this class inherits <tt>RootObject</tt>.*/ \
46 /* Do not use global <tt>new</tt>. */ \
47  s_pInstance = new CLASSNAME(); \
48  PIA_ASSERT(PIA_IS_VALID_POINTER(s_pInstance)); \
49  \
50  return ResultSuccess(); \
51  \
52 }
53 
54 
55 // For this macro, fill in the CPP file that corresponds to the singleton.
56 #define NN_PIA_COMMON_SINGLETON_DESTROY_INSTANCE_IMPL(CLASSNAME) \
57  \
58 void CLASSNAME::DestroyInstance(void) \
59  \
60 { \
61  if (s_pInstance) \
62  { \
63 /* <tt>RootObject::delete</tt> is used if this class inherits <tt>RootObject</tt>. */ \
64 /* Do not use global <tt>delete</tt>. */ \
65  delete s_pInstance; \
66  s_pInstance = NULL; \
67  } \
68  \
69 }
70 
71 
72 namespace nn
73 {
74 namespace pia
75 {
76 namespace common
77 {
78 }
79 }
80 } // end of namespace nn::pia::common
Definition: assert.h:115