Skip to content

Instantly share code, notes, and snippets.

@toroidal-code
Created June 28, 2014 20:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toroidal-code/bcb771510ef3b54f8c72 to your computer and use it in GitHub Desktop.
Save toroidal-code/bcb771510ef3b54f8c72 to your computer and use it in GitHub Desktop.
#include <functional>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
struct Runnable {
virtual ~Runnable(){};
virtual int operator()() = 0;
};
struct TaskContainer;
typedef function<void(TaskContainer&)> block_t;
struct It : public Runnable {
string descr;
function<int(It&)> body;
int operator()() {
std::cout << "Called It::()" << std::endl;
return body(*this);
}
It(string descr, function<int(It&)> body) : descr(descr), body(body) {}
};
struct TaskContainer : public Runnable {
vector <Runnable*> tasks;
void it(string name, function<int(It&)> body) {
std::cout << "Called TaskContainer::it" << std::endl;
tasks.push_back(new It(name, body));
};
int operator()() {
std::cout << "Called TaskContainer::()" << std::endl;
int result = 0;
for (Runnable *task : tasks) {
result |= (*task)();
}
return result;
}
};
class Description : public TaskContainer {
string descr;
block_t body;
public:
Description(string descr, block_t body) : descr(descr), body(body) {};
int operator()(){
body(*this);
return TaskContainer::operator()();
}
};
#define _desc [](TaskContainer &self)
#define _it [](It &self)
#define it self.it
typedef Description describe;
int main(void) {
describe my_test("my test", _desc{
it("adds two numbers", _it{
return 2 + 2 ;
});
});
cout << my_test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment