summaryrefslogtreecommitdiff
path: root/include/statelib.h
blob: 34ab282ade5c5bad53f2790f16e643e32d621dcf (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/******************************************************************************/
// StateBase class
/******************************************************************************/

class StateBase {
public:
	StateBase(const char *name);

	virtual ~StateBase();
	virtual bool isInvalid();
	virtual bool isEqualNotUsedForSomeReason(StateBase *another);
	virtual bool isEqual(StateBase *another);
	virtual bool isNotEqual(StateBase *another);
	virtual bool isSameStateName(const char *name);
	virtual const char *getName();
	virtual int getID();

	static StateBase mNoState;

private:
	const char *mName;
	int mID;

	static int mLastID;
};

/******************************************************************************/
// State<TOwner> : StateBase class
/******************************************************************************/

template <class TOwner>
class State : public StateBase {
public:
	typedef void (TOwner::*funcPtr)();

	State(const char *name, funcPtr begin, funcPtr execute, funcPtr end) : StateBase(name) {
		mBegin = begin;
		mExecute = execute;
		mEnd = end;
	}

	~State();
	bool isSameStateName(const char *name);

	virtual void doBegin(TOwner *owner);
	virtual void doExecute(TOwner *owner);
	virtual void doEnd(TOwner *owner);

	funcPtr mBegin;
	funcPtr mExecute;
	funcPtr mEnd;
};

template <class TOwner>
State<TOwner>::~State() { }

template <class TOwner>
bool State<TOwner>::isSameStateName(const char *name) {
	const char *p = strrchr(name, ':');
	if (p)
		name = p + 1;

	int cmp = strcmp(strrchr(getName(), ':')+1, name);
	if (cmp == 0)
		return true;
	else
		return false;
}

template <class TOwner>
void State<TOwner>::doBegin(TOwner *owner) {
	(owner->*mBegin)();
}

template <class TOwner>
void State<TOwner>::doExecute(TOwner *owner) {
	(owner->*mExecute)();
}

template <class TOwner>
void State<TOwner>::doEnd(TOwner *owner) {
	(owner->*mEnd)();
}

/******************************************************************************/
// StateMgrBase class
/******************************************************************************/

class StateMgrBase {
public:
	StateMgrBase(void *one, void *two, StateBase *pInitState);

	virtual ~StateMgrBase();
	virtual void _vf0C();
	virtual void execute();
	virtual void _vf14();
	virtual void setState(StateBase *pNewState);
	virtual void setField10ToOne();
	virtual StateBase *getField20();
	virtual StateBase *getField14();
	virtual StateBase *getCurrentState_maybe();
	virtual StateBase *getField18();
	virtual bool _vf30() = 0;
	virtual void _vf34() = 0;
	virtual void _vf38() = 0;
	virtual void _vf3C(StateBase *pState) = 0;

private:
	void *m04, *m08;
	bool m0C, m0D, m0E, m0F, m10;
	StateBase *m14, *m18, *m1C;
	StateBase *m20; // maybe not a StateBase?
};

/******************************************************************************/
// StateMgr<TOwner> : StateMgrBase class
/******************************************************************************/

template <class TOwner>
class StateMgr : public StateMgrBase {
public:
	StateMgr(void *one, void *two, StateBase *pInitState);
	~StateMgr();

	bool _vf30();
	void _vf34();
	void _vf38();
	void _vf3C(StateBase *pState);
};

// TODO: add template methods