Changeset View
Changeset View
Standalone View
Standalone View
src/Singleton.h
Show All 10 Lines | 1 | /* This file is part of KGraphViewer. | |||
---|---|---|---|---|---|
11 | General Public License for more details. | 11 | General Public License for more details. | ||
12 | 12 | | |||
13 | You should have received a copy of the GNU General Public License | 13 | You should have received a copy of the GNU General Public License | ||
14 | along with this program; if not, write to the Free Software | 14 | along with this program; if not, write to the Free Software | ||
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
16 | 02110-1301, USA | 16 | 02110-1301, USA | ||
17 | */ | 17 | */ | ||
18 | 18 | | |||
19 | | ||||
20 | #ifndef KGRAPHVIEWER_SINGLETON_H | 19 | #ifndef KGRAPHVIEWER_SINGLETON_H | ||
21 | #define KGRAPHVIEWER_SINGLETON_H | 20 | #define KGRAPHVIEWER_SINGLETON_H | ||
22 | 21 | | |||
23 | /** | 22 | /** | ||
24 | * An implementation of the singleton pattern | 23 | * An implementation of the singleton pattern | ||
25 | * | 24 | * | ||
26 | * @short Singleton pattern implementation | 25 | * @short Singleton pattern implementation | ||
27 | * @author Gaël de Chalendar <kleag@free.fr> | 26 | * @author Gaël de Chalendar <kleag@free.fr> | ||
28 | */ | 27 | */ | ||
29 | template<typename Object> | 28 | template<typename Object> class Singleton | ||
30 | class Singleton | | |||
31 | { | 29 | { | ||
32 | public: | 30 | public: | ||
33 | | ||||
34 | /** | 31 | /** | ||
35 | * @brief const singleton accessor | 32 | * @brief const singleton accessor | ||
36 | */ | 33 | */ | ||
37 | static const Object& single(); | 34 | static const Object &single(); | ||
38 | 35 | | |||
39 | /** | 36 | /** | ||
40 | * @brief singleton accessor | 37 | * @brief singleton accessor | ||
41 | */ | 38 | */ | ||
42 | static Object& changeable(); | 39 | static Object &changeable(); | ||
43 | 40 | | |||
44 | | ||||
45 | private: | 41 | private: | ||
46 | static Object* s_instance; | 42 | static Object *s_instance; | ||
47 | | ||||
48 | }; | 43 | }; | ||
49 | 44 | | |||
50 | template<typename Object> | 45 | template<typename Object> Object *Singleton<Object>::s_instance(nullptr); | ||
51 | Object* Singleton<Object>::s_instance(nullptr); | | |||
52 | 46 | | |||
53 | template<typename Object> | 47 | template<typename Object> const Object &Singleton<Object>::single() | ||
54 | const Object& Singleton<Object>::single() | | |||
55 | { | | |||
56 | if (s_instance == nullptr) | | |||
57 | { | 48 | { | ||
49 | if (s_instance == nullptr) { | ||||
58 | s_instance=new Object(); | 50 | s_instance = new Object(); | ||
59 | } | 51 | } | ||
60 | return *s_instance; | 52 | return *s_instance; | ||
61 | } | 53 | } | ||
62 | 54 | | |||
63 | template<typename Object> | 55 | template<typename Object> Object &Singleton<Object>::changeable() | ||
64 | Object& Singleton<Object>::changeable() | | |||
65 | { | | |||
66 | if (s_instance == nullptr) | | |||
67 | { | 56 | { | ||
57 | if (s_instance == nullptr) { | ||||
68 | s_instance=new Object(); | 58 | s_instance = new Object(); | ||
69 | } | 59 | } | ||
70 | return *s_instance; | 60 | return *s_instance; | ||
71 | } | 61 | } | ||
72 | 62 | | |||
73 | #endif | 63 | #endif |