File indexing completed on 2024-05-05 17:43:12

0001 /*
0002  *   SPDX-FileCopyrightText: 2015 Ivan Cukic <ivan.cukic (at) kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #ifndef PLASMAVAULT_KDED_ENGINE_SINGLETON_P_H
0008 #define PLASMAVAULT_KDED_ENGINE_SINGLETON_P_H
0009 
0010 #include <memory>
0011 #include <mutex>
0012 
0013 namespace singleton
0014 {
0015 template<typename BackendType>
0016 static std::shared_ptr<BackendType> instance()
0017 {
0018     static std::mutex s_instanceMutex;
0019     static std::weak_ptr<BackendType> s_instance;
0020 
0021     std::unique_lock<std::mutex> locker(s_instanceMutex);
0022 
0023     auto ptr = s_instance.lock();
0024 
0025     if (!ptr) {
0026         ptr = std::make_shared<BackendType>();
0027         s_instance = ptr;
0028     }
0029 
0030     return ptr;
0031 }
0032 
0033 } // namespace singleton
0034 
0035 #endif // include guard