currenton.hpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. ** Windstille - A Sci-Fi Action-Adventure Game
  3. ** Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
  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. #ifndef HEADER_SUPERTUX_UTIL_CURRENTON_HPP
  19. #define HEADER_SUPERTUX_UTIL_CURRENTON_HPP
  20. /**
  21. * A 'Currenton' allows access to the currently active instance of a
  22. * class via the static current() function. It is kind of like a
  23. * singleton, but without handling the object construction itself or
  24. * in other words its a glorified global variable that points to the
  25. * current instance of a class.
  26. */
  27. template<class C>
  28. class Currenton
  29. {
  30. private:
  31. static Currenton<C>* s_current;
  32. protected:
  33. Currenton()
  34. {
  35. // FIXME: temporarly disabled, as Sector() for the main menu,
  36. // doesn't get cleaned up before a real Sector() starts
  37. // assert(!s_current);
  38. s_current = this;
  39. }
  40. virtual ~Currenton()
  41. {
  42. if (s_current == this)
  43. {
  44. s_current = nullptr;
  45. }
  46. }
  47. public:
  48. static C* current() { return static_cast<C*>(s_current); }
  49. };
  50. template<class C>
  51. Currenton<C>* Currenton<C>::s_current = nullptr;
  52. #endif
  53. /* EOF */