1
0
mirror of https://github.com/wqking/eventpp.git synced 2025-01-13 16:17:57 +08:00

Update AnyData tutorial and document

This commit is contained in:
wqking 2023-05-16 10:56:35 +08:00
parent 260a82911b
commit 9231cbb93b
2 changed files with 159 additions and 129 deletions

View File

@ -149,6 +149,14 @@ queue.appendListener(EventType::key, [](const Event & e) {
```
Since `AnyData` can convert to any types automatically, here the listener functions can receive `const Event &` instead of `AnyData`. When `EventQueue` passes `AnyData` to the listener, `AnyData` can cast to `const Event &` automatically.
Even better, we can use the concrete type as the argument directly, for example,
```c++
queue.appendListener(EventType::key, [](const KeyEvent & e) {
std::cout << "Received KeyEvent, key=" << e.getKey() << std::endl;
});
```
Note such listener should only receive the specified type, here is `KeyEvent`. If it receives other data type, it will crash your program.
`AnyData` can convert to reference or pointer. When it converts to reference, the reference refers to the underlying data. When it converts to pointer, the pointer points to the address of the underlying data. The special conversion of pointer allow we use unrelated data types as event arguments and receive the argument as `const void *`. For example,
```c++
@ -222,6 +230,12 @@ maxSizeOf<KeyEvent, MouseEvent, int, double>();
Below is the tutorial code. The complete code can be found in `tests/tutorial/tutorial_anydata.cpp`
```c++
void onMessageEvent(const MessageEvent & e)
{
std::cout << "Received MessageEvent in free function, message="
<< e.getMessage() << std::endl;
}
TEST_CASE("AnyData tutorial 1, basic")
{
std::cout << std::endl << "AnyData tutorial 1, basic" << std::endl;
@ -237,10 +251,15 @@ TEST_CASE("AnyData tutorial 1, basic")
std::cout << "Received MouseEvent, x=" << static_cast<const MouseEvent &>(e).getX()
<< " y=" << static_cast<const MouseEvent &>(e).getY() << std::endl;
});
queue.appendListener(EventType::message, [](const Event & e) {
// Even more convenient, the argument type can be the concrete class such as MessageEvent,
// but be sure the listener only receive MessageEvent. If it also receives MouseEvent,
// we can expect crash.
queue.appendListener(EventType::message, [](const MessageEvent & e) {
std::cout << "Received MessageEvent, message="
<< static_cast<const MessageEvent &>(e).getMessage() << std::endl;
<< e.getMessage() << std::endl;
});
// Not only lambda, we can also use free function, or member function as the listener.
queue.appendListener(EventType::message, &onMessageEvent);
// Put events into the queue. Any data type, such as KeyEvent, MouseEvent, can be put
// as long as the data size doesn't exceed eventMaxSize.
queue.enqueue(EventType::key, KeyEvent(255));

View File

@ -97,6 +97,12 @@ constexpr std::size_t eventMaxSize = eventpp::maxSizeOf<
std::string
>();
void onMessageEvent(const MessageEvent & e)
{
std::cout << "Received MessageEvent in free function, message="
<< e.getMessage() << std::endl;
}
TEST_CASE("AnyData tutorial 1, basic")
{
std::cout << std::endl << "AnyData tutorial 1, basic" << std::endl;
@ -112,10 +118,15 @@ TEST_CASE("AnyData tutorial 1, basic")
std::cout << "Received MouseEvent, x=" << static_cast<const MouseEvent &>(e).getX()
<< " y=" << static_cast<const MouseEvent &>(e).getY() << std::endl;
});
queue.appendListener(EventType::message, [](const Event & e) {
// Even more convenient, the argument type can be the concrete class such as MessageEvent,
// but be sure the listener only receive MessageEvent. If it also receives MouseEvent,
// we can expect crash.
queue.appendListener(EventType::message, [](const MessageEvent & e) {
std::cout << "Received MessageEvent, message="
<< static_cast<const MessageEvent &>(e).getMessage() << std::endl;
<< e.getMessage() << std::endl;
});
// Not only lambda, we can also use free function, or member function as the listener.
queue.appendListener(EventType::message, &onMessageEvent);
// Put events into the queue. Any data type, such as KeyEvent, MouseEvent, can be put
// as long as the data size doesn't exceed eventMaxSize.
queue.enqueue(EventType::key, KeyEvent(255));