单例-class single

#include <atomic>
#include <functional>
#include <iostream>
#include <macro.h>
#include <memory>
#include <mutex>
#include <string>

class Noncopyable {
public:
    Noncopyable(const Noncopyable&) = delete;
    Noncopyable& operator=(const Noncopyable&) = delete;

protected:
    Noncopyable() = default;
    virtual ~Noncopyable() = default;
};

template <typename T>
class Singleton : public Noncopyable {
public:
    template <typename... Args>
    static T& getInstance(Args&&... args)
    {
        static std::unique_ptr<T> self { nullptr };
        if (!self) {
            std::unique_lock<std::mutex> uniqueLock(mutexLock);
            if (!self) {
                self.reset(new T(std::forward<Args>(args)...));
            }
        }
        return *self.get();
    }

private:
    static std::mutex mutexLock;
    static std::unique_ptr<T> self;
};
template <typename T>
std::mutex Singleton<T>::mutexLock;
template <typename T>
std::unique_ptr<T> Singleton<T>::self { nullptr };
//test.hpp
class TestSingle : public Singleton<TestSingle> {
    friend class Singleton<TestSingle>;

    int count;
    std::string info;

private:
    explicit TestSingle()
    {
    }

    void print() {
        std::cout<<" info:"<<info<<std::endl;
    }
};

//main.cpp
int main(int argc,char* argv[]) {
    auto& ts=TestSingle::getInstance();
    ts.print();

    return 0;
}
posted @ 2022-01-26 11:11  JohnRed  阅读(32)  评论(0编辑  收藏  举报