aboutsummaryrefslogtreecommitdiff
path: root/src/libjin/common/singleton.h
blob: ff8a9ce7edf407dd8abd41bbb1ccc4b2af8fdaa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef __JIN_SINGLETON_H
#define __JIN_SINGLETON_H

namespace jin
{
    template<class T>
    class Singleton
    {
    public:
        static T* get()
        {
            if (_instance == nullptr)
                _instance = new T;
            return _instance;
        }
        static void destroy()
        {
            delete _instance;
            _instance = nullptr;
        }
    protected:
        Singleton() {};
        virtual ~Singleton() {};
    private:
        Singleton(const Singleton&);
        Singleton& operator = (const Singleton&);

        static T* _instance;
    };

    template<class T> T* Singleton<T>::_instance = nullptr;

#define SINGLETON(T) \
    friend Singleton<T>

} // jin

#endif