不多说,贴代码:
#include "stdafx.h"
#include <list>
#include <thread>
#include <chrono> struct ICoroutine
{
virtual void reset(){}
virtual bool move_next(int & r, float & fv) { return false; }
virtual ~ICoroutine() {}
public:
float mWaitSeconds;
}; template<typename T>
struct _IGenerator : public ICoroutine
{
T* _stack;
int _line;
_IGenerator() :_stack(), _line(-) {}
virtual void reset()
{
_line = -;
}
void _push() { T* n = new T; *n = *static_cast<T*>(this); _stack = n; }
bool _pop() { if (!_stack) return false; T* t = _stack; *static_cast<T*>(this) = *_stack; t->_stack = ; delete t; return true; }
~_IGenerator() { while (_pop()); }
}; #define $coroutine(NAME) struct NAME : public _IGenerator<NAME> #define $begin virtual bool move_next(int& _rv, float& _rv2) { \
if(_line < ) _line=; \
$START: switch(_line) { case :; #define $stop } _line = 0; if(_pop()) goto $START; return false; } #define $restart(WITH) { _push(); _stack->_line = __LINE__; _line=0; WITH; goto $START; case __LINE__:; } #define $yield(V) \
do {\
_line=__LINE__;\
_rv = (V); return true; case __LINE__:;\
} while () #define $yield_f(V, V2) \
do {\
_line=__LINE__;\
_rv = (V); _rv2 = V2; return true; case __LINE__:;\
} while () enum CoroutineState
{
CO_None,
CO_WaitForNextUpdate,
CO_WaitForSeconds,
CO_Exit
}; class GScheduler
{
protected:
std::list<ICoroutine *> mActivityGList;
std::list<ICoroutine *> mWaitingGList;
std::list<ICoroutine *> mDeadingGList; std::list<ICoroutine *> mGList; void DestroyAllCoroutine()
{
std::list<ICoroutine *>::iterator iter;
for (iter = mGList.begin(); iter != mGList.end(); iter++)
{
ICoroutine * co = *iter;
delete co;
}
mGList.clear();
mDeadingGList.clear();
mWaitingGList.clear();
mActivityGList.clear();
}
public:
~GScheduler()
{
DestroyAllCoroutine();
} template<typename T, typename T2>
ICoroutine* StartCoroutine(T2 * tObj)
{
ICoroutine * gen = new T(tObj);
mGList.push_back(gen);
mActivityGList.push_back(gen);
return gen;
} void StopCoroutine(ICoroutine *)
{
} void RestartAllCoroutine()
{
std::list<ICoroutine *>::iterator iter;
for (iter = mGList.begin(); iter != mGList.end(); iter++)
{
ICoroutine * co = *iter;
co->reset();
mActivityGList.push_back(co);
}
} void StopAllCoroutine()
{
mDeadingGList.clear();
mWaitingGList.clear();
mActivityGList.clear();
}
void UpdateAllCoroutine(float dt)
{
std::list<ICoroutine *>::iterator iter, next;
for (iter = mWaitingGList.begin(); iter != mWaitingGList.end();iter = next)
{
next = iter; next++; ICoroutine * co = *iter;
co->mWaitSeconds -= dt;
if (co->mWaitSeconds <= )
{
next = mWaitingGList.erase(iter);
mActivityGList.push_back(co);
}
} for (iter = mActivityGList.begin(); iter != mActivityGList.end(); iter = next)
{
next = iter; next++; ICoroutine * co = *iter; bool isDeading = false; int retValue = ;
float retFValue = ;
if (!co->move_next(retValue, retFValue))
{
isDeading = true;
}
CoroutineState state = (CoroutineState)retValue;
if (state == CO_Exit)
{
isDeading = true;
}
else if (state == CO_WaitForNextUpdate)
{ }
else if (state == CO_WaitForSeconds)
{
float seconds = retFValue;
co->mWaitSeconds = seconds;
next = mActivityGList.erase(iter);
mWaitingGList.push_back(co);
} if (isDeading)
{
next = mActivityGList.erase(iter);
mDeadingGList.push_back(co);
}
}
}
};
//**********************************************************************************************************
//以下是测试程序:
class TestCoroutine1;
class TestCoroutine2;
class UIMain : public GScheduler
{
public:
UIMain()
{
}
void Enable()
{
RestartAllCoroutine();
}
void Disable()
{
StopAllCoroutine();
} void Start()
{
ICoroutine *testCo = StartCoroutine<TestCoroutine1, UIMain>(this);
StartCoroutine<TestCoroutine2, UIMain>(this);
} void Update(float dt)
{
UpdateAllCoroutine(dt);
} void Test1(int v)
{
printf("Test1, v = %d\n", v);
}
void Test2(int v)
{
printf("Test2, v = %d\n", v);
}
}; $coroutine(TestCoroutine1)
{
UIMain* n;
TestCoroutine1(UIMain* root = ) : n(root) {}
int i = ;
$begin
for (i = ; i < ; i++)
{
n->Test1(i);
$yield(CO_WaitForNextUpdate);
}
$yield(CO_Exit);
n->Test1();
$stop
}; $coroutine(TestCoroutine2)
{
UIMain* n;
TestCoroutine2(UIMain* root = ) : n(root) {}
int i = ;
$begin
for (i = ; i < ; i++)
{
n->Test2(i);
$yield_f(CO_WaitForSeconds, 0.5f);
}
$yield(CO_Exit);
n->Test1();
$stop
}; int _tmain(int argc, _TCHAR* argv[])
{
UIMain uiMain; uiMain.Enable();
uiMain.Start(); float dt = 0.05f;
float time = ;
while (true)
{
uiMain.Update(dt);
std::this_thread::sleep_for(std::chrono::milliseconds((int)(dt*)));
time += dt;
if (time > ) //10秒后重开协程
{
uiMain.Disable();
uiMain.Enable(); //重新开始协程
time = ;
}
}
return ;
}