KillableSingleton.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file KillableSingleton.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
36 */
37 
38 
39 
40 #ifndef __OPENFLUID_UTILS_KILLABLESINGLETON_HPP__
41 #define __OPENFLUID_UTILS_KILLABLESINGLETON_HPP__
42 
43 
44 namespace openfluid { namespace utils {
45 
46 /**
47  Singleton template class implementing a killable singleton design.
48  This implementation is not thread safe.
49 
50  @warning This template class does not work in a cross-dll context on Windows.
51  Due to this restriction, it must not be used in the OpenFLUID framework.
52 
53  Example of use to design an Example class as a killable singleton
54  @code
55 class Example : public openfluid::utils::KillableSingleton<Example>
56 {
57  friend class openfluid::utils::KillableSingleton<Example>;
58 
59 
60  private:
61 
62  Example();
63 
64  ~Example();
65 
66 
67  public:
68 
69  // some public members
70 };
71  @endcode
72 */
73 template<typename T>
75 {
76 
77  private:
78 
79  KillableSingleton<T>(T const&) = delete;
80 
81  void operator=(T const&) = delete;
82 
83 
84  protected:
85 
86  static T* mp_Instance;
87 
89  { }
90 
92  { }
93 
94 
95  public:
96 
97  /**
98  Returns a pointer to the singleton object.
99  Instantiates the class if it not has not been instantiated before or it has been killed before.
100  @return A pointer to the singleton object
101  */
102  static T* instance()
103  {
104  if (!mp_Instance)
105  mp_Instance = new T();
106 
107  return mp_Instance;
108  }
109 
110 
111  // =====================================================================
112  // =====================================================================
113 
114 
115  /**
116  Kills the singleton class
117  */
118  static void kill()
119  {
120  if (mp_Instance)
121  {
122  delete mp_Instance;
123  mp_Instance = nullptr;
124  }
125  }
126 };
127 
128 
129 template <typename T>
131 
132 
133 } } //namespaces
134 
135 #endif /* __OPENFLUID_UTILS_KILLABLESINGLETON_HPP__ */
static T * mp_Instance
Definition: KillableSingleton.hpp:86
Definition: KillableSingleton.hpp:74
Definition: ApplicationException.hpp:47
static T * instance()
Definition: KillableSingleton.hpp:102
static void kill()
Definition: KillableSingleton.hpp:118