Singleton.h

Go to the documentation of this file.
00001 #ifndef __SINGLETON_H__
00002 #define __SINGLETON_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include <typeinfo>
00011 
00012 #include "Error.h"
00013 
00014 #include "SingletonDeclaration.h"
00015 
00016 // Implementation.
00017 
00018 template<class T>
00019 T *Singleton<T>::_instance(NULL);
00020 
00021 template<class T>
00022 bool Singleton<T>::_fabricaLocked(false);
00023 
00024 // Public methods.
00025 
00026 template<class T>
00027 T *Singleton<T>::instance()
00028 {
00029   if (_instance)
00030     return _instance;
00031 
00032   if (_fabricaLocked)
00033     ERROR_FATAL(MSG_UNPERMITTED_RECURSION);
00034 
00035   _fabricaLocked = true;
00036   if (!(_instance = new T()))
00037     ERROR_FATAL(MSG_OBJECT_CANNOT_CREATE, typeid(T).name());
00038   else {
00039     int res;
00040 
00041     if (FAILED(res = _instance->failureCode())) {
00042       ERROR_BACKTRACE(res);
00043       delete _instance;
00044       ERROR_FATAL(MSG_OBJECT_CANNOT_CREATE, typeid(T).name());
00045     }
00046   }
00047 
00048   _fabricaLocked = false;
00049 
00050   return _instance;
00051 }
00052 
00053 // Protected methods.
00054 
00055 template<class T>
00056 Singleton<T>::~Singleton()
00057 {
00058   _instance = NULL;
00059   _fabricaLocked = false;
00060 }
00061 
00062 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1