singleton.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2006 by Marc Boris Duerner
3  * Copyright (C) 2006 by Aloysius Indrayanto
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 #ifndef cxxtools_Singleton_h
30 #define cxxtools_Singleton_h
31 
32 #include <memory>
33 #include <cstdlib>
34 
35 namespace cxxtools {
36 
58  template <typename T, typename A = std::allocator<T> >
59  class Singleton
60  {
61  Singleton(const Singleton&) { }
62  Singleton& operator=(const Singleton&) { return *this; }
63 
64  public:
65  typedef A Allocator;
66 
67  public:
76  static T& instance()
77  {
78  if(!_instance)
79  {
80  try
81  {
82  _instance = (T*)_allocator.allocate(1);
83  new (_instance) T();
84  std::atexit(&atExit);
85  }
86  catch( const std::bad_alloc& e )
87  {
88  throw e;
89  }
90  catch(...)
91  {
92  _allocator.deallocate(_instance, 1);
93  _instance = 0;
94  throw;
95  }
96  }
97 
98  return *_instance;
99  }
100 
101  protected:
105  { }
106 
110  { }
111 
112  private:
119  static void atExit()
120  {
121  _instance->~T();
122  _allocator.deallocate(_instance, 1);
123  _instance = 0;
124  }
125 
126  private:
127  static A _allocator;
128  static T* _instance;
129  };
130 
131 
132  template <typename T, typename A>
133  A Singleton<T, A>::_allocator;
134 
135 
136  template <typename T, typename A>
137  T* Singleton<T, A>::_instance = 0;
138 
139 } // namespace cxxtools
140 
141 #endif