00001 /*===========================================================================*\ 00002 * * 00003 * OpenMesh * 00004 * Copyright (C) 2001-2010 by Computer Graphics Group, RWTH Aachen * 00005 * www.openmesh.org * 00006 * * 00007 *---------------------------------------------------------------------------* 00008 * This file is part of OpenMesh. * 00009 * * 00010 * OpenMesh is free software: you can redistribute it and/or modify * 00011 * it under the terms of the GNU Lesser General Public License as * 00012 * published by the Free Software Foundation, either version 3 of * 00013 * the License, or (at your option) any later version with the * 00014 * following exceptions: * 00015 * * 00016 * If other files instantiate templates or use macros * 00017 * or inline functions from this file, or you compile this file and * 00018 * link it with other files to produce an executable, this file does * 00019 * not by itself cause the resulting executable to be covered by the * 00020 * GNU Lesser General Public License. This exception does not however * 00021 * invalidate any other reasons why the executable file might be * 00022 * covered by the GNU Lesser General Public License. * 00023 * * 00024 * OpenMesh is distributed in the hope that it will be useful, * 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00027 * GNU Lesser General Public License for more details. * 00028 * * 00029 * You should have received a copy of the GNU LesserGeneral Public * 00030 * License along with OpenMesh. If not, * 00031 * see <http://www.gnu.org/licenses/>. * 00032 * * 00033 \*===========================================================================*/ 00034 00035 /*===========================================================================*\ 00036 * * 00037 * $Revision: 325 $ * 00038 * $Date: 2010-06-17 12:45:58 +0200 (Do, 17 Jun 2010) $ * 00039 * * 00040 \*===========================================================================*/ 00041 00042 00043 //============================================================================= 00044 // 00045 // Implements a simple singleton template 00046 // 00047 //============================================================================= 00048 00049 00050 #ifndef __SINGLETON_HH__ 00051 #define __SINGLETON_HH__ 00052 00053 00054 //=== INCLUDES ================================================================ 00055 00056 // OpenMesh 00057 #include <OpenMesh/Core/System/config.h> 00058 00059 // STL 00060 #include <stdexcept> 00061 #include <iostream> 00062 00063 00064 //== NAMESPACES =============================================================== 00065 00066 00067 namespace OpenMesh { 00068 00069 00070 //=== IMPLEMENTATION ========================================================== 00071 00072 00077 template <typename T> 00078 class SingletonT 00079 { 00080 public: 00081 00088 static T& Instance() 00089 { 00090 if (!pInstance__) 00091 { 00092 // check if singleton alive 00093 if (destroyed__) 00094 { 00095 OnDeadReference(); 00096 } 00097 // first time request -> initialize 00098 else 00099 { 00100 Create(); 00101 } 00102 } 00103 return *pInstance__; 00104 } 00105 00106 00107 private: 00108 00109 // Disable constructors/assignment to enforce uniqueness 00110 SingletonT(); 00111 SingletonT(const SingletonT&); 00112 SingletonT& operator=(const SingletonT&); 00113 00114 // Create a new singleton and store its pointer 00115 static void Create() 00116 { 00117 static T theInstance; 00118 pInstance__ = &theInstance; 00119 } 00120 00121 // Will be called if instance is accessed after its lifetime has expired 00122 static void OnDeadReference() 00123 { 00124 throw std::runtime_error("[Singelton error] - Dead reference detected!\n"); 00125 } 00126 00127 virtual ~SingletonT() 00128 { 00129 pInstance__ = 0; 00130 destroyed__ = true; 00131 } 00132 00133 static T* pInstance__; 00134 static bool destroyed__; 00135 }; 00136 00137 00138 00139 //============================================================================= 00140 } // namespace OpenMesh 00141 //============================================================================= 00142 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_SINGLETON_C) 00143 # define OPENMESH_SINGLETON_TEMPLATES 00144 # include "SingletonT.cc" 00145 #endif 00146 //============================================================================= 00147 #endif // __SINGLETON_HH__ 00148 //=============================================================================