Webmaster  |  Imprint 
C++ Server Pages
Main  |  License  |  Documentation  |  Download 

/home/tommi/cxxtools/include/cxxtools/singleton.h

00001 /*
00002  * Copyright (C) 2005-2006 by Marc Boris Duerner
00003  * Copyright (C)      2006 by Aloysius Indrayanto
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  * 
00010  * As a special exception, you may use this file as part of a free
00011  * software library without restriction. Specifically, if other files
00012  * instantiate templates or use macros or inline functions from this
00013  * file, or you compile this file and link it with other files to
00014  * produce an executable, this file does not by itself cause the
00015  * resulting executable to be covered by the GNU General Public
00016  * License. This exception does not however invalidate any other
00017  * reasons why the executable file might be covered by the GNU Library
00018  * General Public License.
00019  * 
00020  * This library is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023  * Lesser General Public License for more details.
00024  * 
00025  * You should have received a copy of the GNU Lesser General Public
00026  * License along with this library; if not, write to the Free Software
00027  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00028  */
00029 #ifndef cxxtools_Singleton_h
00030 #define cxxtools_Singleton_h
00031 
00032 #include <cxxtools/noncopyable.h>
00033 
00034 #include <memory>
00035 #include <cstdlib>
00036 
00037 namespace cxxtools {
00038 
00060     template <typename T, typename A = std::allocator<T> >
00061     class Singleton : private NonCopyable
00062     {
00063         public:
00064             typedef A Allocator;
00065 
00066         public:
00075             static T& instance()
00076             {
00077                 if(!_instance)
00078                 {
00079                     try
00080                     {
00081                         _instance = (T*)_allocator.allocate(1);
00082                         new (_instance) T();
00083                         std::atexit(&atExit);
00084                     }
00085                     catch( const std::bad_alloc& e )
00086                     {
00087                         throw e;
00088                     }
00089                     catch(...)
00090                     {
00091                         _allocator.deallocate(_instance, 1);
00092                         _instance = 0;
00093                         throw;
00094                     }
00095                 }
00096 
00097                 return *_instance;
00098             }
00099 
00100         protected:
00103             Singleton()
00104             { }
00105 
00108             ~Singleton()
00109             { }
00110 
00111         private:
00118             static void atExit()
00119             {
00120                 _instance->~T();
00121                 _allocator.deallocate(_instance, 1);
00122                 _instance = 0;
00123             }
00124 
00125         private:
00126             static A  _allocator;
00127             static T* _instance;
00128     };
00129 
00130 
00131     template <typename T, typename A>
00132     A Singleton<T, A>::_allocator;
00133 
00134 
00135     template <typename T, typename A>
00136     T* Singleton<T, A>::_instance = 0;
00137 
00138 } // namespace cxxtools
00139 
00140 #endif
Copyright © 2008 The Tntnet Development Team
Tntnet 1.6