mirror of
https://github.com/wqking/eventpp.git
synced 2025-01-14 00:27:57 +08:00
Update AnyData tutorial and document
This commit is contained in:
parent
260a82911b
commit
9231cbb93b
@ -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.
|
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,
|
`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++
|
```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`
|
Below is the tutorial code. The complete code can be found in `tests/tutorial/tutorial_anydata.cpp`
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
|
void onMessageEvent(const MessageEvent & e)
|
||||||
|
{
|
||||||
|
std::cout << "Received MessageEvent in free function, message="
|
||||||
|
<< e.getMessage() << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE("AnyData tutorial 1, basic")
|
TEST_CASE("AnyData tutorial 1, basic")
|
||||||
{
|
{
|
||||||
std::cout << std::endl << "AnyData tutorial 1, basic" << std::endl;
|
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()
|
std::cout << "Received MouseEvent, x=" << static_cast<const MouseEvent &>(e).getX()
|
||||||
<< " y=" << static_cast<const MouseEvent &>(e).getY() << std::endl;
|
<< " 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="
|
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
|
// 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.
|
// as long as the data size doesn't exceed eventMaxSize.
|
||||||
queue.enqueue(EventType::key, KeyEvent(255));
|
queue.enqueue(EventType::key, KeyEvent(255));
|
||||||
|
@ -97,6 +97,12 @@ constexpr std::size_t eventMaxSize = eventpp::maxSizeOf<
|
|||||||
std::string
|
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")
|
TEST_CASE("AnyData tutorial 1, basic")
|
||||||
{
|
{
|
||||||
std::cout << std::endl << "AnyData tutorial 1, basic" << std::endl;
|
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()
|
std::cout << "Received MouseEvent, x=" << static_cast<const MouseEvent &>(e).getX()
|
||||||
<< " y=" << static_cast<const MouseEvent &>(e).getY() << std::endl;
|
<< " 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="
|
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
|
// 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.
|
// as long as the data size doesn't exceed eventMaxSize.
|
||||||
queue.enqueue(EventType::key, KeyEvent(255));
|
queue.enqueue(EventType::key, KeyEvent(255));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user