~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/framework/Singleton.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2004  Erik Hjortsberg
 
3
    
 
4
    Taken from the file OgreSingleton.h from the Ogre project, release 0.14
 
5
    (www.ogre3d.org)
 
6
 
 
7
    This program is free software; you can redistribute it and/or modify
 
8
    it under the terms of the GNU General Public License as published by
 
9
    the Free Software Foundation; either version 2 of the License, or
 
10
    (at your option) any later version.
 
11
 
 
12
    This program is distributed in the hope that it will be useful,
 
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
    GNU General Public License for more details.
 
16
 
 
17
    You should have received a copy of the GNU General Public License
 
18
    along with this program; if not, write to the Free Software
 
19
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
*/
 
21
 
 
22
#ifndef _EMBER_SINGLETON_H__
 
23
#define _EMBER_SINGLETON_H__
 
24
 
 
25
#include <cassert>
 
26
 
 
27
namespace Ember {
 
28
 
 
29
    /** 
 
30
    @brief Template class for creating single-instance global classes.
 
31
    
 
32
    Whenever you need a singleton in the system you should inherit from this, such as
 
33
    @code
 
34
    class Foo : public Ember::Singleton<Foo>
 
35
    @endcode
 
36
    
 
37
    In your implementation you're then required to intialize ms_Singleton to 0, such as this
 
38
    @code
 
39
    template<> Ember::Foo* Ember::Singleton<Ember::Foo>::ms_Singleton = 0;
 
40
    @endcode
 
41
    
 
42
    Whenever you need the singleton instance in your project you can then get it by calling @see getSingleton() or @see getSingletonPtr()
 
43
    Note that you still have to provide lifetime service and ownership and need to both create and destroy the instance where appropriate.
 
44
    */
 
45
    template <typename T> class Singleton
 
46
    {
 
47
    protected:
 
48
 
 
49
                /**
 
50
                @brief The static variable holding the singleton instance.
 
51
                Remember to instanciate this to 0 in your implementation.
 
52
                */
 
53
                static T* ms_Singleton;
 
54
 
 
55
    public:
 
56
                
 
57
                /**
 
58
                 * @brief Standard constructor.
 
59
                 */
 
60
                Singleton( void )
 
61
                {
 
62
                        assert( !ms_Singleton );
 
63
                        ms_Singleton = static_cast< T* >( this );
 
64
                }
 
65
                
 
66
                ~Singleton( void )
 
67
                {  
 
68
                        assert( ms_Singleton );  ms_Singleton = 0;  
 
69
                }
 
70
                
 
71
                /**
 
72
                 *       @brief Gets the singleton instance.
 
73
                 * @return The singleton instance.
 
74
                 */
 
75
                static T& getSingleton( void )
 
76
                {       
 
77
                        assert( ms_Singleton );  
 
78
                        return ( *ms_Singleton ); 
 
79
                }
 
80
                
 
81
                /**
 
82
                 *       @brief Gets a pointer to the singleton instance.
 
83
                 * @return A pointer to the singleton instance.
 
84
                 */
 
85
                static T* getSingletonPtr( void )
 
86
                { 
 
87
                        assert( ms_Singleton ); 
 
88
                        return ms_Singleton; 
 
89
                }
 
90
                
 
91
                /**
 
92
                 *       @brief Gets the singleton instance.
 
93
                 * @return The singleton instance.
 
94
                 */
 
95
                static T& instance( void )
 
96
                {
 
97
                        return getSingleton();
 
98
                }
 
99
                
 
100
                /**
 
101
                 *       @brief Returns true if there's a singleton registered with the system.
 
102
                 * @return True if there's a singleton available.
 
103
                 */
 
104
                static bool hasInstance()
 
105
                {
 
106
                        return ms_Singleton != 0;
 
107
                }
 
108
        };
 
109
}
 
110
#endif
 
111