1
0

fogot to add promise classes

This commit is contained in:
Tycho Bickerstaff 2013-12-31 16:13:13 +00:00
parent 512d1b9ebe
commit 098ed91a48
2 changed files with 92 additions and 0 deletions

54
src/OSSupport/Promise.cpp Normal file
View File

@ -0,0 +1,54 @@
#include "Globals.h"
#include "Promise.h"
cPromise * cPromise::WaitFor(cPromise * a_Promise)
{
return new cCombinedPromise(this, a_Promise);
}
cPromise * cPromise::CancelOn(volatile bool& cancelation)
{
return new cCancelablePromise(this, cancelation);
}
void cPromise::Wait()
{
while(!IsCompleted()){}; //busywait best we can do until waitany
}
cCombinedPromise::cCombinedPromise(cPromise* a_left, cPromise* a_right) :
cPromise(),
m_left(a_left),
m_right(a_right)
{
}
cCombinedPromise::~cCombinedPromise()
{
}
bool cCombinedPromise::IsCompleted()
{
return m_left->IsCompleted() || m_right->IsCompleted();
}
cCancelablePromise::cCancelablePromise(cPromise* a_wrapped, volatile bool& a_cancel) :
cPromise(),
m_cancel(a_cancel),
m_wrapped(a_wrapped)
{
}
cCancelablePromise::~cCancelablePromise ()
{
}
bool cCancelablePromise::IsCompleted()
{
return m_cancel || m_wrapped->IsCompleted();
}

38
src/OSSupport/Promise.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
class cCombinedPromise;
class cPromise {
public:
cPromise() {}
virtual ~cPromise () {}
cPromise * WaitFor(cPromise * a_Promise);
cPromise * CancelOn(volatile bool& cancelationtoken);
void Wait();
virtual bool IsCompleted() = 0;
//TODO:Expose Events for waiting on
};
class cCombinedPromise : public cPromise {
public:
cCombinedPromise(cPromise*, cPromise*);
~cCombinedPromise();
virtual bool IsCompleted();
private:
cPromise* m_left;
cPromise* m_right;
};
class cCancelablePromise : public cPromise {
public:
cCancelablePromise(cPromise*, volatile bool&);
~cCancelablePromise();
virtual bool IsCompleted();
private:
volatile bool& m_cancel;
cPromise* m_wrapped;
};