summaryrefslogtreecommitdiff
path: root/include/d/snd/d_snd_util.h
blob: b3be21ff90ae5de910c32a9e6198435369594d34 (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef D_SND_UTIL_H
#define D_SND_UTIL_H

#include "common.h"
#include "egg/core/eggDisposer.h"

#include <cstring>

inline bool streq(const char *left, const char *right) {
    return !std::strcmp(left, right);
}

inline bool strneq(const char *left, const char *right, size_t len) {
    return !std::strncmp(left, right, len);
}

// This list setup is convenient, and it allows declaring local variables in
// a consistent order without causing regswaps when multiple loops are involved
// in a single funcion.

#define LIST_MEMBER(ty, name)                                                                                          \
    nw4r::ut::List m##name##List;                                                                                      \
    inline ty *get##name##First() {                                                                                    \
        return static_cast<ty *>(nw4r::ut::List_GetFirst(&m##name##List));                                             \
    }                                                                                                                  \
    inline ty *get##name##Next(ty *p) {                                                                                \
        return static_cast<ty *>(nw4r::ut::List_GetNext(&m##name##List, p));                                           \
    }                                                                                                                  \
    inline void append##name(ty *p) {                                                                                  \
        nw4r::ut::List_Append(&m##name##List, p);                                                                      \
    }                                                                                                                  \
    inline void remove##name(ty *p) {                                                                                  \
        nw4r::ut::List_Remove(&m##name##List, p);                                                                      \
    }

// This setup is only inferred. d/snd uses it all over the place.
// This works for dSndPlayerMgr_c, which has a vtable of its own but the Disposer at offset 0.
// It also works for the factory at 0x80399c20, which calls a base class ctor,
// an inline ctor, and has the Disposer at offset 0x18

template <typename T>
struct SndMgrDisposer : public EGG::Disposer {
    virtual ~SndMgrDisposer();

    static T *create();
    static void remove();
};

template <typename T>
SndMgrDisposer<T>::~SndMgrDisposer() {
    if (this == T::sDisposer) {
        remove();
    }
}

template <typename T>
T *SndMgrDisposer<T>::create() {
    if (T::sInstance == nullptr) {
        T::sInstance = new T();
        T::sDisposer = T::sInstance->GetDisposer();
    }
    return T::sInstance;
}

template <typename T>
void SndMgrDisposer<T>::remove() {
    T::sInstance = nullptr;
    T::sDisposer = nullptr;
}

#define SND_DISPOSER_FORWARD_DECL(class_name)                                                                          \
    class class_name;                                                                                                  \
    extern template class SndMgrDisposer<class_name>;

#define SND_DISPOSER_DEFINE(class_name)                                                                                \
    template class SndMgrDisposer<class_name>;                                                                         \
    class_name *class_name::sInstance;                                                                                 \
    SndMgrDisposer<class_name> *class_name::sDisposer;

#define SND_DISPOSER_MEMBERS(class_name)                                                                               \
public:                                                                                                                \
    SndMgrDisposer<class_name> *GetDisposer() {                                                                        \
        return &mDisposer;                                                                                             \
    }                                                                                                                  \
                                                                                                                       \
    static class_name *GetInstance() {                                                                                 \
        return sInstance;                                                                                              \
    }                                                                                                                  \
                                                                                                                       \
    static void create() {                                                                                             \
        SndMgrDisposer<class_name>::create();                                                                          \
    }                                                                                                                  \
                                                                                                                       \
    static class_name *sInstance;                                                                                      \
    static SndMgrDisposer<class_name> *sDisposer;                                                                      \
                                                                                                                       \
private:                                                                                                               \
    SndMgrDisposer<class_name> mDisposer;

#endif