User Manual, Developers Guide and API Documentation

Singleton.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_SINGLETON_HPP
00029 #define WNS_SINGLETON_HPP
00030 
00031 #include <assert.h>
00032 #include <cstdlib>
00033 #include <WNS/NonCopyable.hpp>
00034 #include <WNS/simulator/ISimulator.hpp>
00035 
00036 namespace wns {
00037 
00038     struct AtSimulatorExit
00039     {
00040         static void
00041         scheduleDestruction(void (*functionPtr)())
00042         {
00043             wns::simulator::getShutdownSignal()->connect(functionPtr);
00044         }
00045     };
00046 
00047     struct AtApplicationExit
00048     {
00049         static void
00050         scheduleDestruction(void (*functionPtr)())
00051         {
00052             std::atexit(functionPtr);
00053         }
00054     };
00055 
00056     template <class T>
00057     struct DefaultCreation
00058     {
00059         static T*
00060         create()
00061         {
00062             return new T;
00063         }
00064 
00065         static void
00066         destroy(T* p)
00067         {
00068             delete p;
00069         }
00070     };
00071 
00072     template <class T>
00073     class SingletonHolderStaticData
00074     {
00075     public:
00076         static T* instance;
00077         static bool destroyed;
00078     };
00079 
00080     template<class T>
00081     T* SingletonHolderStaticData<T>::instance;
00082 
00083     template<class T>
00084     bool SingletonHolderStaticData<T>::destroyed;
00085 
00090     template<class T, 
00091         template <class> class CreationPolicy = DefaultCreation, 
00092         class DestructTimePolicy = AtSimulatorExit>
00093     class SingletonHolder :
00094         private NonCopyable
00095     {
00096         typedef SingletonHolderStaticData<T> Static;
00097     public:
00102         static T&
00103         Instance()
00104         {
00105             if (Static::instance == NULL)
00106             {
00107                 Static::instance = CreationPolicy<T>::create();
00108                 Static::destroyed = false;
00109 
00110                 DestructTimePolicy::scheduleDestruction(&destroySingleton);
00111             }
00112 
00113             assert(Static::destroyed == false); // using a destroyed singleton
00114 
00115             return *Static::instance;
00116         }
00117 
00121         static T*
00122         getInstance()
00123         {
00124             return &Instance();
00125         }
00126 
00130         static void
00131         reset()
00132         {
00133             CreationPolicy<T>::destroy(Static::instance);
00134             Static::instance = CreationPolicy<T>::create();
00135         }
00136 
00137     private:
00142         static void
00143         destroySingleton()
00144         {
00145             assert(Static::destroyed == false); // using a destroyed singleton
00146             CreationPolicy<T>::destroy(Static::instance);
00147             Static::instance = NULL;
00148             Static::destroyed = true;
00149         }
00150 
00154         SingletonHolder();
00155 
00159         ~SingletonHolder(); // forbidden
00160     };
00161 }
00162 #endif // NOT defined WNS_SINGLETON_HPP
00163 
00164 

Generated on Fri May 25 03:31:56 2012 for openWNS by  doxygen 1.5.5