Last active
July 24, 2022 13:59
-
-
Save mak1a/cc551a28b1f2c703e1caf8b51e433114 to your computer and use it in GitHub Desktop.
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
# include <Siv3D.hpp> // OpenSiv3D v0.6.4 | |
template <class... Args> | |
class SivEvent | |
{ | |
Array<std::function<void(Args...)>> m_funcs; | |
public: | |
SivEvent() = default; | |
void AddListener(std::function<void(Args...)>&& func) | |
{ | |
m_funcs.emplace_back(std::move(func)); | |
} | |
void Invoke(Args&&... args) | |
{ | |
for (const auto& func : m_funcs) | |
{ | |
func(args...); | |
} | |
} | |
}; | |
class MyClass | |
{ | |
public: | |
void PrintStr(StringView str) | |
{ | |
Print << str; | |
} | |
}; | |
void Hoge(StringView str, int num) | |
{ | |
Print << str << U"と" << num; | |
} | |
void Main() | |
{ | |
{ | |
SivEvent<> myEvent; | |
myEvent.AddListener([]() { Print << U"mak1a"; }); | |
myEvent.Invoke(); | |
} | |
{ | |
SivEvent<int> myEvent; | |
myEvent.AddListener([](int num) { Print << num; }); | |
myEvent.Invoke(100); | |
} | |
{ | |
SivEvent<MyClass> myEvent; | |
myEvent.AddListener([](MyClass c) { c.PrintStr(U"MyClass"); }); | |
myEvent.Invoke(MyClass()); | |
} | |
{ | |
SivEvent<String, int> myEvent; | |
myEvent.AddListener([](StringView str, int num) | |
{ | |
Print << str << U", " << num; | |
}); | |
myEvent.AddListener(Hoge); | |
myEvent.Invoke(U"hoge", 3); | |
} | |
while (System::Update()) | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment