Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / util / currenton.hpp
1 /*
2 **  Windstille - A Sci-Fi Action-Adventure Game
3 **  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
4 **
5 **  This program is free software: you can redistribute it and/or modify
6 **  it under the terms of the GNU General Public License as published by
7 **  the Free Software Foundation, either version 3 of the License, or
8 **  (at your option) any later version.
9 **  
10 **  This program is distributed in the hope that it will be useful,
11 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 **  GNU General Public License for more details.
14 **  
15 **  You should have received a copy of the GNU General Public License
16 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef HEADER_WINDSTILLE_UTIL_CURRENTON_HPP
20 #define HEADER_WINDSTILLE_UTIL_CURRENTON_HPP
21
22 #include <assert.h>
23
24 /** 
25  *   A 'Currenton' allows access to the currently active instance of a
26  *   class via the static current() function. It is kind of like a
27  *   singleton, but without handling the object construction itself or
28  *   in other words its a glorified global variable that points to the
29  *   current instance of a class.
30  */
31 template<class C>
32 class Currenton
33 {
34 private:
35   static C* s_current; 
36
37 protected:
38   Currenton()  
39   { 
40     // FIXME: temporarly disabled, as Sector() for the main menu,
41     // doesn't get cleaned up before a real Sector() starts
42     // assert(!s_current); 
43     s_current = static_cast<C*>(this); 
44   }
45
46   virtual ~Currenton()
47   {
48     s_current = 0; 
49   }
50   
51 public:
52   static C* current() { return s_current; }
53 };
54
55 template<class C> C* Currenton<C>::s_current = 0;
56
57 #endif
58
59 /* EOF */