LifeV
FactorySingleton.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5 Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6 Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8 This file is part of LifeV.
9 
10 LifeV is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 
15 LifeV is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 /*!
27  @file
28  @brief Singleton pattern class
29 
30  @date 10-09-2004
31  @author Christophe Prud'homme <christophe.prudhomme@epfl.ch>
32 
33  @maintainer Radu Popescu <radu.popescu@epfl.ch>
34 */
35 
36 #ifndef FACTORY_SINGLETON_H
37 #define FACTORY_SINGLETON_H 1
38 
39 #include <algorithm>
40 #include <cassert>
41 #include <cstdlib>
42 #include <exception>
43 #include <new>
44 #include <stdexcept>
45 
46 #include <lifev/core/util/FactoryPolicy.hpp>
47 
48 namespace LifeV
49 {
50 /**
51  \class FactorySingleton
52  \brief implement the FactorySingleton pattern
53 
54  A FactorySingleton pattern implementation using the ideas
55  from Alexandrescu's book "modern C++ design"
56  http://www.moderncppdesign.com/
57 */
58 template <typename SingletonType>
60 {
61 public:
62  //! @name Public typedefs
63  //@{
64  typedef SingletonType Singleton_Type;
67  //@}
68 
69  //! @name Public static methods
70  //@{
71  /**
72  return the instance of the FactorySingleton
73  */
74  static Singleton_Type& instance();
75  //@}
76 private:
77  //! @name Private typedefs
78  //@{
80  //@}
81 
82  // Disable the constructor
84 
85  //! @name Private static methods
86  //@{
87  static void makeInstance();
88 
89  /**
90  FactorySingleton::makeInstance (helper for Instance)
91  */
92  static void destroyFactorySingleton();
93 
94  //@}
96  static bool S_destroyed;
97 };
98 
99 // ===================================
100 // FactorySingleton Implementation
101 // ===================================
102 
103 template <class SingletonType>
104 typename FactorySingleton<SingletonType>::instance_Type FactorySingleton<SingletonType>::S_instance;
105 
106 template <class SingletonType>
107 bool FactorySingleton<SingletonType>::S_destroyed;
108 
109 template <class SingletonType>
110 inline SingletonType& FactorySingleton<SingletonType>::instance()
111 {
112  if ( !S_instance )
113  {
115  }
116  return *S_instance;
117 }
118 
119 template <class SingletonType>
120 void FactorySingleton<SingletonType>::makeInstance()
121 {
122  if ( !S_instance )
123  {
124  if ( S_destroyed )
125  {
126  lifetimeFactoryPolicy_Type::onDeadReference();
127  S_destroyed = false;
128  }
131  }
132 }
133 
134 template <class SingletonType>
136 {
137  assert ( !S_destroyed );
139  S_instance = 0;
140  S_destroyed = true;
141 }
142 
143 } // Namespace LifeV
144 
145 #endif // FACTORY_SINGLETON_H
FactoryPolicyLifeTimeDefault< Singleton_Type > lifetimeFactoryPolicy_Type
FactoryPolicyCreationUsingNew< Singleton_Type > creationFactoryPolicy_Type
static Singleton_Type & instance()
return the instance of the FactorySingleton
void updateInverseJacobian(const UInt &iQuadPt)
Singleton_Type * instance_Type
static instance_Type S_instance
static void destroyFactorySingleton()
FactorySingleton::makeInstance (helper for Instance)
implement the FactorySingleton pattern