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 25 #define DECLARE_DELAYED_SINGLETON(MyClass)\ 26 public:\ 27 ~MyClass();\ 28 private:\ 29 friend DelayedSingleton<MyClass>;\ 30 MyClass(); 31 32 #define DECLARE_DELAYED_REF_SINGLETON(MyClass)\ 33 private:\ 34 friend DelayedRefSingleton<MyClass>;\ 35 ~MyClass();\ 36 MyClass(); 37 38 39 #define DECLARE_SINGLETON(MyClass)\ 40 private:\ 41 friend Singleton<MyClass>;\ 42 MyClass& operator=(const MyClass&) = delete;\ 43 MyClass(const MyClass&) = delete;\ 44 MyClass();\ 45 ~MyClass(); 46 47 48 template<typename T> 49 class DelayedSingleton : public NoCopyable { 50 public: 51 static std::shared_ptr<T> GetInstance(); 52 static void DestroyInstance(); 53 54 private: 55 static std::shared_ptr<T> instance_; 56 static std::mutex mutex_; 57 }; 58 59 template<typename T> 60 std::shared_ptr<T> DelayedSingleton<T>::instance_ = nullptr; 61 62 template<typename T> 63 std::mutex DelayedSingleton<T>::mutex_; 64 65 template<typename T> GetInstance()66std::shared_ptr<T> DelayedSingleton<T>::GetInstance() 67 { 68 if (instance_ == nullptr) { 69 std::lock_guard<std::mutex> lock(mutex_); 70 if (instance_ == nullptr) { 71 std::shared_ptr<T> temp(new T); 72 instance_ = temp; 73 } 74 } 75 76 return instance_; 77 } 78 79 template<typename T> DestroyInstance()80void DelayedSingleton<T>::DestroyInstance() 81 { 82 std::lock_guard<std::mutex> lock(mutex_); 83 if (instance_ != nullptr) { 84 instance_.reset(); 85 instance_ = nullptr; 86 } 87 } 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()108T& 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 117 return *instance_; 118 } 119 120 121 122 template<typename T> 123 class Singleton : public NoCopyable { 124 public: GetInstance()125 static T& GetInstance() 126 { 127 return instance_; 128 } 129 130 private: 131 static T instance_; 132 }; 133 134 template<typename T> 135 T Singleton<T>::instance_; 136 137 138 } 139 140 #endif 141