-
Notifications
You must be signed in to change notification settings - Fork 6
/
NathanSingleton.h
47 lines (39 loc) · 1.73 KB
/
NathanSingleton.h
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
//
// Singleton.h
// Deep
//
// Created by Nathan Daly on 12/15/12.
// Copyright (c) 2012 Lions Entertainment. All rights reserved.
//
#ifndef Nathan_Singleton_h
#define Nathan_Singleton_h
// Put the following macro inside the class declaration
#define DECLARE_SINGELTON(CLASS_NAME) \
\
public: \
static CLASS_NAME* get(){ \
if (!singleton_ptr) \
singleton_ptr = new CLASS_NAME; \
return singleton_ptr; \
} \
\
private: \
static CLASS_NAME * singleton_ptr; \
friend class CLASS_NAME_destroyer; \
\
CLASS_NAME() {} \
~CLASS_NAME() {} \
CLASS_NAME(const CLASS_NAME&); \
CLASS_NAME& operator= (const CLASS_NAME&); \
struct destroyer { \
~destroyer(){ \
delete CLASS_NAME::singleton_ptr; \
} \
}; \
static destroyer the_destroyer;
// Put the following macro in the implementation file
#define DEFINE_SINGELTON(CLASS_NAME) \
\
CLASS_NAME* CLASS_NAME::singleton_ptr = 0; \
CLASS_NAME::destroyer CLASS_NAME::the_destroyer;
#endif