singleton.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright (C) 2001-2003
  2. // Mac Murrett
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org for most recent version including documentation.
  9. #ifndef BOOST_SINGLETON_MJM012402_HPP
  10. #define BOOST_SINGLETON_MJM012402_HPP
  11. #include <boost/thread/detail/config.hpp>
  12. namespace boost {
  13. namespace detail {
  14. namespace thread {
  15. // class singleton has the same goal as all singletons: create one instance of
  16. // a class on demand, then dish it out as requested.
  17. template <class T>
  18. class singleton : private T
  19. {
  20. private:
  21. singleton();
  22. ~singleton();
  23. public:
  24. static T &instance();
  25. };
  26. template <class T>
  27. inline singleton<T>::singleton()
  28. {
  29. /* no-op */
  30. }
  31. template <class T>
  32. inline singleton<T>::~singleton()
  33. {
  34. /* no-op */
  35. }
  36. template <class T>
  37. /*static*/ T &singleton<T>::instance()
  38. {
  39. // function-local static to force this to work correctly at static
  40. // initialization time.
  41. static singleton<T> s_oT;
  42. return(s_oT);
  43. }
  44. } // namespace thread
  45. } // namespace detail
  46. } // namespace boost
  47. #endif // BOOST_SINGLETON_MJM012402_HPP