Logo  0.95.0-final
Finite Element Embedded Library and Language in C++
Feel++ Feel++ on Github Feel++ community
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
singleton.hpp
Go to the documentation of this file.
1 /* -*- mode: c++; coding: utf-8; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; show-trailing-whitespace: t -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
2 
3  This file is part of the Feel library
4 
5  Author(s): Christophe Prud'homme <prudhomm@mit.edu>
6  Date: 2004-09-10
7 
8  Copyright (C) 2006,2009 Université Joseph Fourier (Grenoble I)
9  Copyright (C) 2004 EPFL
10 
11  This library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU Lesser General Public
13  License as published by the Free Software Foundation; either
14  version 3.0 of the License, or (at your option) any later version.
15 
16  This library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  Lesser General Public License for more details.
20 
21  You should have received a copy of the GNU Lesser General Public
22  License along with this library; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
30 #ifndef __Singleton_H
31 #define __Singleton_H 1
32 
33 #include <cstdlib>
34 #include <cassert>
35 #include <exception>
36 #include <stdexcept>
37 #include <algorithm>
38 #include <new>
39 
40 #include <feel/feelcore/policy.hpp>
41 
42 namespace Feel
43 {
56 template <typename T>
57 class Singleton
58 {
59 public:
60 
61  typedef T singleton_type;
62 
65 
66 
70  static singleton_type& instance();
71 
72 private:
73  // Helpers
74  static void makeInstance();
75 
79  static void destroySingleton();
80 
81  // Protection
82  Singleton();
83 
84  // Data
85  typedef singleton_type* instance_type;
86  static instance_type _S_instance;
87  static bool _S_destroyed;
88 };
89 
90 //
91 // Instantiate Singleton's static data
92 //
93 template <class T>
94 typename Singleton<T>::instance_type Singleton<T>::_S_instance;
95 
96 template <class T>
98 
99 
100 template <class T>
101 inline
102 T&
104 {
105  if ( !_S_instance )
106  {
107  makeInstance();
108  }
109 
110  return *_S_instance;
111 }
112 
113 
114 template <class T>
115 void
117 {
118  if ( !_S_instance )
119  {
120  if ( _S_destroyed )
121  {
122  feeltime_policy::onDeadReference();
123  _S_destroyed = false;
124  }
125 
126  _S_instance = creation_policy::create();
127  feeltime_policy::scheduleDestruction( _S_instance, &destroySingleton );
128  }
129 }
130 
131 template <class T>
132 void
133 Singleton<T>::destroySingleton()
134 {
135  assert( !_S_destroyed );
136  creation_policy::destroy( _S_instance );
137  _S_instance = 0;
138  _S_destroyed = true;
139 }
140 }
141 #endif /* __singleton_HPP */

Generated on Fri Oct 25 2013 14:24:24 for Feel++ by doxygen 1.8.4