Created
April 16, 2015 10:11
-
-
Save kritzikratzi/56ee7f671d9f58f2eae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "ofApp.h" | |
#include <csetjmp> | |
class Continuation{ | |
public: | |
Continuation() : done(false), jumped(false){} | |
bool work(){ | |
if( done ) return true; | |
// remember this place, we will jump back here! | |
int val=setjmp(of_env); | |
if( val == 0 ){ | |
// we got here through the call from ofApp#update | |
if( jumped ){ | |
// todo: restore 2 stack levels somehow?? | |
longjmp(work_env, 1); | |
} | |
else{ | |
run(); | |
// eventually, at one point, we'll end up here. | |
done = true; | |
return true; | |
} | |
} | |
else if( val == 1 ){ | |
// we got here because continueLater() was called from work | |
return false; | |
} | |
else if( val == 2 ){ | |
// we got here because work is done. hurray! | |
done = true; | |
return true; | |
} | |
} | |
protected: | |
// override this. | |
// call continueLater() to pause execution | |
virtual void run() = 0; | |
void continueLater(){ | |
// remember this point for later ... | |
jumped = true; | |
int val = setjmp(work_env); | |
if( val == 0 ){ | |
// we got here because of a call from run() | |
// todo: save 2 stack levels, somehow?? | |
// jump back to openframeworks method | |
longjmp(of_env, 1); // pass 1, means we got work to do | |
} | |
else if( val == 1 ){ | |
// we got here because of a call from work() | |
// weirdly this also means that run() is below us in the stack. | |
// so we can just return, and continue were we left off. | |
} | |
} | |
private: | |
jmp_buf work_env; | |
jmp_buf of_env; | |
bool done; | |
bool jumped; | |
}; | |
class ContinuationTest : public Continuation{ | |
void run(){ | |
int x = 3; | |
cout << "x = " << 3 << endl; | |
// back to OF for a little while... | |
continueLater(); | |
int y = 2; | |
cout << "x = " << x << ", y = " << y << endl; | |
// back to OF for a little while... | |
continueLater(); | |
int z = 7; | |
cout << "x = " << x << ", y = " << y << ", z = " << z << endl; | |
} | |
}; | |
ContinuationTest test; | |
//-------------------------------------------------------------- | |
void ofApp::setup(){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::update(){ | |
test.work(); | |
} | |
//-------------------------------------------------------------- | |
void ofApp::draw(){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyPressed(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::keyReleased(int key){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseMoved(int x, int y){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseDragged(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mousePressed(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::mouseReleased(int x, int y, int button){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::windowResized(int w, int h){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::gotMessage(ofMessage msg){ | |
} | |
//-------------------------------------------------------------- | |
void ofApp::dragEvent(ofDragInfo dragInfo){ | |
} | |
void ofApp::doWork(){ | |
while( true ){ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment