Skip to content

Instantly share code, notes, and snippets.

@macrat
Created January 31, 2017 18:30
Show Gist options
  • Save macrat/4af2fea9544d5d4757bf29902423acc3 to your computer and use it in GitHub Desktop.
Save macrat/4af2fea9544d5d4757bf29902423acc3 to your computer and use it in GitHub Desktop.
簡易的なタスクシステムの試作
#include <iostream>
#include <string>
template<class T> class Pod {
protected:
virtual void OnReceive(T msg) {}
void Send(T msg) {
Pod<T>* x = this;
while((x = x->Next()) && x != this){
x->OnReceive(msg);
}
}
public:
virtual ~Pod() {}
virtual Pod* Next() = 0;
virtual void Update() = 0;
void Loop() {
Pod<T>* x = this;
do{
x->Update();
}while(x = x->Next());
}
};
template<class T> class SimplePod : public Pod<T> {
protected:
Pod<T>* next;
public:
SimplePod(Pod<T>* next) : next(next) {}
SimplePod() : next(NULL) {}
virtual ~SimplePod() {}
void SetNext(Pod<T>* next) {
this->next = next;
}
virtual Pod<T>* Next() {
return next;
}
};
class NamedPod : public SimplePod<std::string> {
private:
std::string name;
public:
NamedPod(std::string name, Pod<std::string>* next) : SimplePod<std::string>(next), name(name) {}
NamedPod(std::string name) : name(name) {}
virtual void OnReceive(std::string msg) {
std::cout << name << ": " << msg << std::endl;
}
virtual void Update() {
std::cout << name << std::endl;
Send(std::string("hello by ") + name);
}
};
template<class T> class EndOfPods : public Pod<T> {
public:
virtual Pod<T>* Next() {
return NULL;
}
virtual void Update() {}
};
int main() {
NamedPod c("tec"), b("teb", &c), a("tea", &b);
c.SetNext(&a);
a.Loop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment