summaryrefslogtreecommitdiff
path: root/include/s/s_FState.hpp
blob: fe38f8c42308b284466ee20ea7839700e8375aa5 (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
#ifndef S_FSTATE_H
#define S_FSTATE_H

// clang-format off
#include "s/s_StateInterfaces.hpp"
#include "s/s_FStateID.hpp"
// clang-format on

// Note: Ported from https://github.com/NSMBW-Community/NSMBW-Decomp/tree/master/include/dol/sLib
// See include/s/README.txt for changes made

/// @brief A state holder for a given class.
/// @tparam T The class that this state belongs to.
/// @ingroup state
template <class T>
class sFState_c : public sStateIf_c {
public:
    sFState_c(T &owner) : mpOwner(owner) {
        mpID = nullptr;
    }

    enum STATE_ACTION_e {
        INITIALIZE,
        EXECUTE,
        FINALIZE
    };

    /// @brief Performs a state action.
    /// @details [Gets inlined, needed for function order to match].
    /// @param action The action to perform.
    void performAction(STATE_ACTION_e action) {
        if (action == FINALIZE) {
            mpID->finalizeState(mpOwner);
        } else if (action == EXECUTE) {
            mpID->executeState(mpOwner);
        } else if (action == INITIALIZE) {
            mpID->initializeState(mpOwner);
        }
    }

    virtual const void initialize() {
        performAction(INITIALIZE);
    }
    virtual const void execute() {
        performAction(EXECUTE);
    }
    virtual const void finalize() {
        performAction(FINALIZE);
    }

    void setID(const sFStateID_c<T> *id) {
        mpID = id;
    }

private:
    T &mpOwner;                 ///< The owner of this state.
    const sFStateID_c<T> *mpID; ///< The state ID that runs the state methods.
};

#endif