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 MOCK_NATIVE_INCLUDE_SINGLETON_H
17 #define MOCK_NATIVE_INCLUDE_SINGLETON_H
18 
19 #include "nocopyable.h"
20 #include <mutex>
21 #include <memory>
22 
23 namespace OHOS {
24 #define DECLARE_DELAYED_SINGLETON(MyClass) \
25 do \
26 { \
27 public: \
28     ~MyClass(); \
29 private: \
30     friend DelayedSingleton<MyClass>; \
31     MyClass(); \
32 } while (0)
33 
34 #define DECLARE_DELAYED_REF_SINGLETON(MyClass) \
35 private: \
36     friend DelayedRefSingleton<MyClass>; \
37     ~MyClass(); \
38     MyClass()
39 
40 #define DECLARE_SINGLETON(MyClass) \
41 do \
42 { \
43 private: \
44     friend Singleton<MyClass>; \
45     MyClass& operator=(const MyClass&) = delete; \
46     MyClass(const MyClass&) = delete; \
47     MyClass(); \
48     ~MyClass(); \
49 } while (0)
50 
51 template<typename T>
52 class DelayedSingleton : public NoCopyable {
53 public:
54     static std::shared_ptr<T> GetInstance();
55     static void DestroyInstance();
56 
57 private:
58     static std::shared_ptr<T> instance_;
59     static std::mutex mutex_;
60 };
61 
62 template<typename T>
63 std::shared_ptr<T> DelayedSingleton<T>::instance_ = nullptr;
64 
65 template<typename T>
66 std::mutex DelayedSingleton<T>::mutex_;
67 
68 template<typename T>
GetInstance()69 std::shared_ptr<T> DelayedSingleton<T>::GetInstance()
70 {
71     if (instance_ == nullptr) {
72         std::lock_guard<std::mutex> lock(mutex_);
73         if (instance_ == nullptr) {
74             std::shared_ptr<T> temp(new T);
75             instance_ = temp;
76         }
77     }
78     return instance_;
79 }
80 
81 template<typename T>
DestroyInstance()82 void DelayedSingleton<T>::DestroyInstance()
83 {
84     std::lock_guard<std::mutex> lock(mutex_);
85     if (instance_ != nullptr) {
86         instance_.reset();
87         instance_ = nullptr;
88     }
89 }
90 
91 template<typename T>
92 class DelayedRefSingleton : public NoCopyable {
93 public:
94     static T& GetInstance();
95 
96 private:
97     static T* instance_;
98     static std::mutex mutex_;
99 };
100 
101 template<typename T>
102 T* DelayedRefSingleton<T>::instance_ = nullptr;
103 
104 template<typename T>
105 std::mutex DelayedRefSingleton<T>::mutex_;
106 
107 template<typename T>
GetInstance()108 T& DelayedRefSingleton<T>::GetInstance()
109 {
110     if (instance_ == nullptr) {
111         std::lock_guard<std::mutex> lock(mutex_);
112         if (instance_ == nullptr) {
113             instance_ = new T();
114         }
115     }
116     return *instance_;
117 }
118 
119 template<typename T>
120 class Singleton : public NoCopyable {
121 public:
GetInstance()122     static T& GetInstance()
123     {
124         return instance_;
125     }
126 
127 private:
128     static T instance_;
129 };
130 
131 template<typename T>
132 T Singleton<T>::instance_;
133 } // namespace OHOS
134 #endif // MOCK_NATIVE_INCLUDE_SINGLETON_H
135