/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MOCK_NATIVE_INCLUDE_SINGLETON_H #define MOCK_NATIVE_INCLUDE_SINGLETON_H #include "nocopyable.h" #include #include namespace OHOS { #define DECLARE_DELAYED_SINGLETON(MyClass) \ do \ { \ public: \ ~MyClass(); \ private: \ friend DelayedSingleton; \ MyClass(); \ } while (0) #define DECLARE_DELAYED_REF_SINGLETON(MyClass) \ private: \ friend DelayedRefSingleton; \ ~MyClass(); \ MyClass() #define DECLARE_SINGLETON(MyClass) \ do \ { \ private: \ friend Singleton; \ MyClass& operator=(const MyClass&) = delete; \ MyClass(const MyClass&) = delete; \ MyClass(); \ ~MyClass(); \ } while (0) template class DelayedSingleton : public NoCopyable { public: static std::shared_ptr GetInstance(); static void DestroyInstance(); private: static std::shared_ptr instance_; static std::mutex mutex_; }; template std::shared_ptr DelayedSingleton::instance_ = nullptr; template std::mutex DelayedSingleton::mutex_; template std::shared_ptr DelayedSingleton::GetInstance() { if (instance_ == nullptr) { std::lock_guard lock(mutex_); if (instance_ == nullptr) { std::shared_ptr temp(new T); instance_ = temp; } } return instance_; } template void DelayedSingleton::DestroyInstance() { std::lock_guard lock(mutex_); if (instance_ != nullptr) { instance_.reset(); instance_ = nullptr; } } template class DelayedRefSingleton : public NoCopyable { public: static T& GetInstance(); private: static T* instance_; static std::mutex mutex_; }; template T* DelayedRefSingleton::instance_ = nullptr; template std::mutex DelayedRefSingleton::mutex_; template T& DelayedRefSingleton::GetInstance() { if (instance_ == nullptr) { std::lock_guard lock(mutex_); if (instance_ == nullptr) { instance_ = new T(); } } return *instance_; } template class Singleton : public NoCopyable { public: static T& GetInstance() { return instance_; } private: static T instance_; }; template T Singleton::instance_; } // namespace OHOS #endif // MOCK_NATIVE_INCLUDE_SINGLETON_H