Debian Codegraph @

include/base/dsingleton.h

raw
1 // SPDX-FileCopyrightText: 2016 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #ifndef DSINGLETON_H
6 #define DSINGLETON_H
7
8 #include "dtkcore_global.h"
9
10 DCORE_BEGIN_NAMESPACE
11
12 /*!
13 a simple singleton template for std c++ 11 or later.
14
15 example:
16
17 \code
18 class ExampleSingleton : public QObject, public Dtk::DSingleton<ExampleSingleton>
19 {
20 Q_OBJECT
21 friend class DSingleton<ExampleSingleton>;
22 };
23 \endcode
24
25 \note: for Qt, "public DSingleton<ExampleSingleton>" must be after QObject.
26 */
27
28 /*!
29 通过c++11的特性实现的单例模板
30
31 使用示例:
32
33 ```
34 class ExampleSingleton : public QObject, public Dtk::DSingleton<ExampleSingleton>
35 {
36 Q_OBJECT
37 friend class DSingleton<ExampleSingleton>;
38 };
39 ```
40
41 \note 对于Qt程序 public DSingleton<ExampleSingleton>" 必须在卸载QObject后面出现。
42 */
43
44 template <class T>
45 class LIBDTKCORESHARED_EXPORT DSingleton
46 {
47 public:
48 #if DTK_VERSION < DTK_VERSION_CHECK(6, 0, 0, 0)
49 QT_DEPRECATED_X("Use ref")
50 static inline T *instance()
51 {
52 static T *_instance = new T;
53 return _instance;
54 }
55 #endif
56 static T& ref()
57 {
58 static T instance;
59 return instance;
60 }
61
62 DSingleton(T&&) = delete;
63 DSingleton(const T&) = delete;
64 void operator= (const T&) = delete;
65
66 protected:
67 DSingleton() = default;
68 virtual ~DSingleton() = default;
69 };
70
71 DCORE_END_NAMESPACE
72
73 #endif // DSINGLETON_H