FougTools  0.7.0dev-046fb6a
Handy tools for C++, Qt and OpenCascade
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
singleton.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** FougTools
3 ** Copyright Fougue (30 Mar. 2015)
4 ** contact@fougue.pro
5 **
6 ** This software is a computer program whose purpose is to provide utility
7 ** tools for the C++ language and the Qt toolkit.
8 **
9 ** This software is governed by the CeCILL-C license under French law and
10 ** abiding by the rules of distribution of free software. You can use,
11 ** modify and/ or redistribute the software under the terms of the CeCILL-C
12 ** license as circulated by CEA, CNRS and INRIA at the following URL
13 ** "http://www.cecill.info".
14 ****************************************************************************/
15 
16 #pragma once
17 
18 #include <QtCore/QAtomicPointer>
19 #include <QtCore/QMutex>
20 
21 namespace qtcore {
22 
23 template<typename T>
24 class Singleton
25 {
26 public:
27  static T* instance();
28  static void release();
29 
30 private:
31  static QAtomicPointer<T> m_instance;
32  static QMutex m_mutex;
33 };
34 
35 // --
36 // -- Implementation
37 // --
38 
39 template<typename T>
41 {
42  if (!m_instance) {
43  m_mutex.lock();
44  if (!m_instance)
45  m_instance = new T;
46  m_mutex.unlock();
47  }
48  return m_instance;
49 }
50 
51 template<typename T>
53 {
54  m_mutex.lock();
55  if (m_instance)
56  delete m_instance;
57  m_instance = NULL;
58  m_mutex.unlock();
59 }
60 
61 } // namespace qtcore
62 
63 template<typename T>
64 QAtomicPointer<T> qtcore::Singleton<T>::m_instance;
65 
66 template<typename T>
Definition: grid_numbering.cpp:19
static T * instance()
Definition: singleton.h:40
static void release()
Definition: singleton.h:52
Definition: singleton.h:24