source: NonGTP/FCollada/FUtils/FUSingleton.h @ 964

Revision 964, 4.7 KB checked in by igarcia, 19 years ago (diff)
Line 
1/*
2        Copyright (C) 2005-2006 Feeling Software Inc.
3        MIT License: http://www.opensource.org/licenses/mit-license.php
4*/
5/*
6        Taken off the Protect project in 2005.
7*/
8
9/**
10        @file FUSingleton.h
11        This file contains macros to easily implement singletons.
12        A singleton is a class which has only one object of this class.
13        The advantage of a singleton over a static class is that the
14        application controls when and how the singleton is created and
15        destroyed. The disadvantage of a singleton is that you have one
16        extra memory lookup to do.
17*/
18
19#ifndef _FU_SINGLETON_H_
20#define _FU_SINGLETON_H_
21
22/** Declares a singleton.
23        Use this macros within the class declaration.
24        @param className The name of the class. */
25#define DECLARE_SINGLETON_CLASS(className) \
26private: \
27        static className* m_pSingleton; \
28public: \
29        static bool CreateSingleton(); \
30        static void DestroySingleton(); \
31        friend className* Get##className();
32
33/**     Declares a singleton.
34        Use this macros within the class declaration.
35        @param className The name of the class.
36        @param createArgs An argument for the constructor of the singleton. */
37#define DECLARE_SINGLETON_CLASS_WITH_ARGS(className, createArgs) \
38private: \
39        static className* m_pSingleton; \
40public: \
41        static bool CreateSingleton(createArgs); \
42        static void DestroySingleton(); \
43        friend className* Get##className();
44
45/**     Declares a singleton.
46        Use this macros within the class declaration.
47        @param className The name of the class.
48        @param createArgs1 A first argument for the constructor of the singleton.
49        @param createArgs2 A second argument for the constructor of the singleton. */
50#define DECLARE_SINGLETON_CLASS_WITH_ARGS2(className, createArgs1, createArgs2) \
51private: \
52        static className* m_pSingleton; \
53public: \
54        static bool CreateSingleton(createArgs1, createArgs2); \
55        static void DestroySingleton(); \
56        friend className* Get##className();
57
58/**     Implements the singleton.
59        Use this macros within the class implementation.
60        @param className The name of the class. */
61#define IMPLEMENT_SINGLETON(className) \
62        IMPLEMENT_CREATE_SINGLETON(className) \
63        IMPLEMENT_DESTROY_SINGLETON(className)
64
65/**     Implements the singleton.
66        Use this macros within the class implementation.
67        @param className The name of the class.
68        @param createArg1 The argument for the constructor of the singleton. */
69#define IMPLEMENT_SINGLETON_WITH_ARGS(className, createArg1) \
70        IMPLEMENT_CREATE_SINGLETON_WITH_ARGS(className, createArg1) \
71        IMPLEMENT_DESTROY_SINGLETON(className)
72
73/**     Implements the singleton.
74        Use this macros within the class implementation.
75        @param className The name of the class.
76        @param createArg1 A first argument for the constructor of the singleton.
77        @param createArg2 A second argument for the constructor of the singleton. */
78#define IMPLEMENT_SINGLETON_WITH_ARGS2(className, createArg1, createArg2) \
79        IMPLEMENT_CREATE_SINGLETON_WITH_ARGS2(className, createArg1, createArg2) \
80        IMPLEMENT_DESTROY_SINGLETON(className)
81
82/**     Implements the construction of a singleton.
83        Use this macros within the class implementation.
84        @param className The name of the class. */
85#define IMPLEMENT_CREATE_SINGLETON(className) \
86        className* className::m_pSingleton; \
87        className* Get##className() { return className::m_pSingleton; } \
88        bool className::CreateSingleton() { \
89                m_pSingleton = new className(); \
90                return true; }
91
92/** Implements the construction of a singleton.
93        Use this macros within the class implementation.
94        @param className The name of the class.
95        @param createArg1 The argument for the constructor of the singleton. */
96#define IMPLEMENT_CREATE_SINGLETON_WITH_ARGS(className, createArg1) \
97        className* className::m_pSingleton; \
98        className* Get##className() { return className::m_pSingleton; } \
99        bool className::CreateSingleton(createArg1 argument1) { \
100                m_pSingleton = new className(argument1); }
101
102/** Implements the construction of a singleton.
103        Use this macros within the class implementation.
104        @param className The name of the class.
105        @param createArg1 A first argument for the constructor of the singleton.
106        @param createArg2 A second argument for the constructor of the singleton. */
107#define IMPLEMENT_CREATE_SINGLETON_WITH_ARGS2(className, createArg1, createArg2) \
108        className* className::m_pSingleton; \
109        className* Get##className() { return className::m_pSingleton; } \
110        bool className::CreateSingleton(createArg1 argument1, createArg2 argument2) { \
111                m_pSingleton = new className(argument1, argument2); }
112
113/** Implements the destruction of a singleton.
114        Use this macros within the class implementation.
115        @param className The name of the class. */
116#define IMPLEMENT_DESTROY_SINGLETON(className) \
117        void className::DestroySingleton() \
118        { SAFE_DELETE(m_pSingleton); }
119
120#endif // _FU_SINGLETON_H_
Note: See TracBrowser for help on using the repository browser.