Skip to content

Instantly share code, notes, and snippets.

@ydakoukn
Created September 23, 2016 16:05
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 ydakoukn/8a2f3e405b645d94e171a2b9ca4b4065 to your computer and use it in GitHub Desktop.
Save ydakoukn/8a2f3e405b645d94e171a2b9ca4b4065 to your computer and use it in GitHub Desktop.
C#のSystem.Action型をC++で実装してみた
class VoidAction{
public:
// コンストラクタ、デストラクタの設定
VoidAction() {
m_function = nullptr;
}
VoidAction(void(*function)(void)) {
m_function = (*function);
}
~VoidAction() {
m_function = nullptr;
}
// 関数呼び出し用
// 何も設定されてない場合は無視
void Invoke() {
if (IsEmpty())return;
m_function();
}
// からのときtrue
bool IsEmpty() {
return (m_function) ? false : true;
}
// オペレーター定義
void operator=(void (*function)(void)) {
m_function = *function;
}
void operator()() {
Invoke();
}
private:
std::function<void(void)> m_function;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment