Last active
January 22, 2020 08:23
-
-
Save nariakiiwatani/5d5067a663df9170daafd77fa9bdae29 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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