summaryrefslogtreecommitdiff
path: root/include/egg/core/eggSingleton.h
blob: a9b4817f7528bd5afd231c3f10c71ab5cd9118c3 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef EGG_CORE_SINGLETON_H
#define EGG_CORE_SINGLETON_H

#include "egg/core/eggDisposer.h"
#include "egg/prim/eggAssert.h"

/**
 * @brief Declares functions and data for a disposable singleton class
 */
#define EGG_SINGLETON_DECL(T)                                                                                          \
protected:                                                                                                             \
    class T__Disposer : public EGG::Disposer {                                                                         \
    public:                                                                                                            \
        virtual ~T__Disposer(); /* at 0x8 */                                                                           \
        static T__Disposer *sStaticDisposer;                                                                           \
    };                                                                                                                 \
                                                                                                                       \
    T__Disposer mDisposer;                                                                                             \
                                                                                                                       \
public:                                                                                                                \
    static T *createInstance();                                                                                        \
    static void deleteInstance();                                                                                      \
                                                                                                                       \
    static T *instance() {                                                                                             \
        return sInstance;                                                                                              \
    }                                                                                                                  \
                                                                                                                       \
protected:                                                                                                             \
    static T *sInstance;

/**
 * @brief Defines functions and data for a disposable singleton class
 */
#define EGG_SINGLETON_IMPL(T)                                                                                          \
    T::T__Disposer::~T__Disposer() {                                                                                   \
        T::deleteInstance();                                                                                           \
    }                                                                                                                  \
                                                                                                                       \
    T *T::createInstance() {                                                                                           \
        if (sInstance == NULL) {                                                                                       \
            sInstance = new T();                                                                                       \
        } else {                                                                                                       \
            EGG_ASSERT_MSG(false, "Create Singleton Twice (%s) : addr %x\n", #T, sInstance);                           \
        }                                                                                                              \
                                                                                                                       \
        return sInstance;                                                                                              \
    }                                                                                                                  \
                                                                                                                       \
    void T::deleteInstance() {                                                                                         \
        sInstance = NULL;                                                                                              \
    }                                                                                                                  \
                                                                                                                       \
    T *T::sInstance = NULL;

#endif