• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef UTILS_BASE_SINGLETON_H
17 #define UTILS_BASE_SINGLETON_H
18 
19 #include "nocopyable.h"
20 #include <mutex>
21 #include <memory>
22 
23 namespace OHOS {
24 #define DECLARE_DELAYED_SINGLETON(MyClass) \
25 public: \
26     ~MyClass(); \
27 private: \
28     friend DelayedSingleton<MyClass>; \
29     MyClass();
30 
31 #define DECLARE_DELAYED_REF_SINGLETON(MyClass) \
32 private: \
33     friend DelayedRefSingleton<MyClass>; \
34     ~MyClass(); \
35     MyClass();
36 
37 
38 #define DECLARE_SINGLETON(MyClass) \
39 private: \
40     friend Singleton<MyClass>; \
41     MyClass& operator=(const MyClass&) = delete; \
42     MyClass(const MyClass&) = delete; \
43     MyClass(); \
44     ~MyClass();
45 
46 
47 template<typename T>
48 class DelayedSingleton : public NoCopyable {
49 public:
50     static std::shared_ptr<T> GetInstance();
51     static void DestroyInstance();
52 
53 private:
54     static std::shared_ptr<T> instance_;
55     static std::mutex mutex_;
56 };
57 
58 template<typename T>
59 std::shared_ptr<T> DelayedSingleton<T>::instance_ = nullptr;
60 
61 template<typename T>
62 std::mutex DelayedSingleton<T>::mutex_;
63 
64 template<typename T>
GetInstance()65 std::shared_ptr<T> DelayedSingleton<T>::GetInstance()
66 {
67     if (instance_ == nullptr) {
68         std::lock_guard<std::mutex> lock(mutex_);
69         if (instance_ == nullptr) {
70             instance_ = std::make_shared<T>();
71         }
72     }
73 
74     return instance_;
75 }
76 
77 template<typename T>
DestroyInstance()78 void DelayedSingleton<T>::DestroyInstance()
79 {
80     std::lock_guard<std::mutex> lock(mutex_);
81     if (instance_ != nullptr) {
82         instance_.reset();
83         instance_ = nullptr;
84     }
85 }
86 
87 template<typename T>
88 class DelayedRefSingleton : public NoCopyable {
89 public:
90     static T& GetInstance();
91 
92 private:
93     static T* instance_;
94     static std::mutex mutex_;
95 };
96 
97 template<typename T>
98 T* DelayedRefSingleton<T>::instance_ = nullptr;
99 
100 template<typename T>
101 std::mutex DelayedRefSingleton<T>::mutex_;
102 
103 template<typename T>
GetInstance()104 T& DelayedRefSingleton<T>::GetInstance()
105 {
106     if (instance_ == nullptr) {
107         std::lock_guard<std::mutex> lock(mutex_);
108         if (instance_ == nullptr) {
109             instance_ = new T();
110         }
111     }
112 
113     return *instance_;
114 }
115 
116 template<typename T>
117 class Singleton : public NoCopyable {
118 public:
GetInstance()119     static T& GetInstance()
120     {
121         return instance_;
122     }
123 
124 private:
125     static T instance_;
126 };
127 
128 template<typename T>
129 T Singleton<T>::instance_;
130 } // namespace OHOS
131 
132 #endif
133