SingletonMacros.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 SingletonMacros.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@supagro.inra.fr>
36 */
37 
38 
39 
40 #ifndef __OPENFLUID_UTILS_SINGLETONMACROS_HPP__
41 #define __OPENFLUID_UTILS_SINGLETONMACROS_HPP__
42 
43 /**
44  Macro for definition of a dynamic singleton,
45  providing instance() and kill() methods to the class in which it is defined.
46  It must be put in the .hpp file, at the beginning of the class.
47  This implementation is not thread safe.
48 
49  Example of use to design an Example class as a dynamic singleton
50  @code
51 // .hpp file
52 
53 class Example
54 {
55  OPENFLUID_SINGLETON_DEFINITION(Example)
56 
57  private:
58 
59  Example();
60 
61  ~Example();
62 
63 
64  public:
65 
66  // some public members
67 };
68  @endcode
69  @code
70 // .cpp file
71 
72 OPENFLUID_SINGLETON_INITIALIZATION(Example)
73 };
74  @endcode
75 
76  @see OPENFLUID_SINGLETON_INITIALIZATION
77 */
78 #define OPENFLUID_SINGLETON_DEFINITION(T) \
79  private: \
80  T(T const&) = delete; \
81  void operator=(T const&) = delete; \
82  \
83  protected: \
84  static T* mp_Instance; \
85  \
86  public: \
87  static T* instance() \
88  { \
89  if (!mp_Instance) \
90  mp_Instance = new T(); \
91  return mp_Instance; \
92  }\
93  static void kill()\
94  {\
95  if (mp_Instance)\
96  {\
97  delete mp_Instance;\
98  mp_Instance = nullptr;\
99  }\
100  }
101 
102 
103 // =====================================================================
104 // =====================================================================
105 
106 /**
107  Macro for initialization of a dynamic singleton, must be put in the .cpp file.
108  @see OPENFLUID_SINGLETON_DEFINITION
109 */
110 #define OPENFLUID_SINGLETON_INITIALIZATION(T) \
111  T* T::mp_Instance = nullptr;
112 
113 
114 #endif /* __OPENFLUID_UTILS_SINGLETONMACROS_HPP__ */