/**************************************************
WinMain.cpp
堆栈式的函数管理器
http://blog.csdn.net/chinayaosir
**************************************************/
//0.头文件
#include <windows.h>
#include <stdio.h>
//1.管理器类定义
class cProcessManager
{
// A structure that stores a function pointer and linked list
typedef struct sProcess {
void (*Function)();
sProcess *Next;
} sProcess;
protected:
sProcess *m_ProcessParent; // The top state in the stack
// (the head of the stack)
public:
cProcessManager() { m_ProcessParent = NULL; }
~cProcessManager() {
sProcess *ProcessPtr;
// Remove all processes from the stack
while((ProcessPtr = m_ProcessParent) != NULL) {
m_ProcessParent = ProcessPtr->Next;
delete ProcessPtr;
}
}
// Add function on to the stack
void Add(void (*Process)())
{
// Don't push a NULL value
if(Process != NULL) {
// Allocate a new process and push it on stack
sProcess *ProcessPtr = new sProcess;
ProcessPtr->Next = m_ProcessParent;
m_ProcessParent = ProcessPtr;
ProcessPtr->Function = Process;
}
}
// Process all functions
void Process()
{
sProcess *ProcessPtr = m_ProcessParent;
while(ProcessPtr != NULL) {
ProcessPtr->Function();
ProcessPtr = ProcessPtr->Next;
}
}
};
//3.生成此类的一个对象g_ProcessManager
cProcessManager g_ProcessManager;
//4.WinMain主函数
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
// Macro to ease the use of MessageBox function
#define MB(s) MessageBox(NULL, s, s, MB_OK);
// Processfunction prototypes - must follow this prototype!
void Func1() { MB("function1"); }
void Func2() { MB("function2"); }
void Func3() { MB("function3"); }
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
g_ProcessManager.Add(Func1);//函数压入堆栈
g_ProcessManager.Add(Func2);//函数压入堆栈
g_ProcessManager.Add(Func3);//函数压入堆栈
g_ProcessManager.Process();//依次遍历堆栈中的函数
return 0;
}