OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SingletonT.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 1188 $ *
38  * $Date: 2015-01-05 16:34:10 +0100 (Mo, 05 Jan 2015) $ *
39  * *
40 \*===========================================================================*/
41 
42 
43 //=============================================================================
44 //
45 // Implements a simple singleton template
46 //
47 //=============================================================================
48 
49 
50 #ifndef __SINGLETON_HH__
51 #define __SINGLETON_HH__
52 
53 
54 //=== INCLUDES ================================================================
55 
56 // OpenMesh
57 #include <OpenMesh/Core/System/config.h>
58 
59 // STL
60 #include <stdexcept>
61 #include <iostream>
62 
63 
64 //== NAMESPACES ===============================================================
65 
66 
67 namespace OpenMesh {
68 
69 
70 //=== IMPLEMENTATION ==========================================================
71 
72 
77 template <typename T>
79 {
80 public:
81 
88  static T& Instance()
89  {
90  if (!pInstance__)
91  {
92  // check if singleton alive
93  if (destroyed__)
94  {
95  OnDeadReference();
96  }
97  // first time request -> initialize
98  else
99  {
100  Create();
101  }
102  }
103  return *pInstance__;
104  }
105 
106 
107 private:
108 
109  // Disable constructors/assignment to enforce uniqueness
110  SingletonT();
111  SingletonT(const SingletonT&);
112  SingletonT& operator=(const SingletonT&);
113 
114  // Create a new singleton and store its pointer
115  static void Create()
116  {
117  static T theInstance;
118  pInstance__ = &theInstance;
119  }
120 
121  // Will be called if instance is accessed after its lifetime has expired
122  static void OnDeadReference()
123  {
124  throw std::runtime_error("[Singelton error] - Dead reference detected!\n");
125  }
126 
127  virtual ~SingletonT()
128  {
129  pInstance__ = 0;
130  destroyed__ = true;
131  }
132 
133  static T* pInstance__;
134  static bool destroyed__;
135 };
136 
137 
138 
139 //=============================================================================
140 } // namespace OpenMesh
141 //=============================================================================
142 #if defined(OM_INCLUDE_TEMPLATES) && !defined(OPENMESH_SINGLETON_C)
143 # define OPENMESH_SINGLETON_TEMPLATES
144 # include "SingletonT.cc"
145 #endif
146 //=============================================================================
147 #endif // __SINGLETON_HH__
148 //=============================================================================
A simple singleton template.
Definition: SingletonT.hh:78
static T & Instance()
Singleton access function.
Definition: SingletonT.hh:88
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:56

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .