SDC C-Project CF Review 프로그램
LYW
2021-07-08 e10b8c2a3f6ee6b639dbb49ff6635d0657531d1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//////////////////////////////////////////////////////////////////////////////////////
// Singleton.h: interface for the Singleton class.
//////////////////////////////////////////////////////////////////////////////////////
//    ex) sample
//
//    class Test : public Singleton < Test >
//    {
//    public:
//        Test* Sample( int num );
//        //...
//    };
//
//
//
//    #define g_Test    Test::GetSingleton()
//
//    void SomeFuncton( void )
//    {
//        Test * pTemp = Test::GetSingleton().Sample( 7 );
//        // or   Test * pTemp = g_Test.Sample( 7 );
//    }
//
//////////////////////////////////////////////////////////////////////////////////////
 
/*---------------------------------------------------------------------------
 
# ÆÄÀϸí : Singleton.h
# ¼³  ¸í : interface for the Singleton template class.
# ³»  ·Â : 
# ºñ  °í : 
 
---------------------------------------------------------------------------*/
 
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
 
///////////////////////////////////////////////////////////////////////////////
// include define statement
///////////////////////////////////////////////////////////////////////////////
 
#include "assert.h"
 
///////////////////////////////////////////////////////////////////////////////
// class define statement
///////////////////////////////////////////////////////////////////////////////
 
/*---------------------------------------------------------------------------
 
# Å¬·¡½º  ¸í : Singleton
# ºÎ¸ðŬ·¡½º : ¾øÀ½
# ÂüÁ¶Å¬·¡½º : ¾øÀ½
# °ü¸®Ã¥ÀÓÀÚ : Voidhoon
# ¼³      ¸í : 
# º¯      ¼ö : 
# ³»      ·Â : 
# ¹®  Á¦  Á¡ : 
 
---------------------------------------------------------------------------*/
 
template <typename T> class Singleton
{
    static T* ms_Singleton;
 
public:
    Singleton(void)
    {
        assert(!ms_Singleton);
        int offset = (int)(T*)1 - (int)(Singleton <T>*)(T*)1;
        ms_Singleton = (T*)((int)this + offset);
    }
 
    ~Singleton(void)
    {
        assert(ms_Singleton);
        ms_Singleton = 0;
    }
 
    static void SetSingleton(T* pT)
    {
        assert(pT);
        ms_Singleton = pT;
    }
 
    static T* GetSingleton(void)
    {
        assert(ms_Singleton);
        return ms_Singleton;
    }
 
    static T* GetSingletonPtr(void)
    {
        assert(ms_Singleton);
        return ms_Singleton;
    }
 
    static T& GetSingletonInstance(void)
    {
        assert(ms_Singleton);
        return(*ms_Singleton);
    }
};
 
template <typename T> T* Singleton <T>::ms_Singleton = 0;
 
#endif