Skip to content

Instantly share code, notes, and snippets.

@nariakiiwatani
Last active January 22, 2020 08:23
Show Gist options
  • Save nariakiiwatani/5d5067a663df9170daafd77fa9bdae29 to your computer and use it in GitHub Desktop.
Save nariakiiwatani/5d5067a663df9170daafd77fa9bdae29 to your computer and use it in GitHub Desktop.
#pragma once
#include <functional>
class Once
{
public:
using Work = std::function<void()>;
Once(){}
Once(Work work) {
setWork(work);
}
void setWork(Work work) {
work_ = work;
}
void expire() { is_expired_ = true; }
void restore() { is_expired_ = false; }
bool run() {
if(is_expired_) {
return false;
}
force();
return true;
}
void force() {
work_();
is_expired_ = true;
}
private:
Work work_;
bool is_expired_=false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment