Created
September 23, 2016 16:05
-
-
Save ydakoukn/8a2f3e405b645d94e171a2b9ca4b4065 to your computer and use it in GitHub Desktop.
C#のSystem.Action型をC++で実装してみた
This file contains 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
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