1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-25 23:30:49 +08:00

Added tip on how to add getter/setter prefix automatically in event maker document and unit tests

This commit is contained in:
wqking 2023-11-17 08:58:49 +08:00
parent eb15c456a2
commit 45a410135e
2 changed files with 41 additions and 0 deletions

View File

@ -121,3 +121,23 @@ Similar to above example, except the base class is constructed with `(EventType:
```
Similar with `EVENTPP_MAKE_EVENT`, but EVENTPP_MAKE_EMPTY_EVENT declares event class which doesn't have any data member.
## Tip: add getter/setter prefix automatically
If you don't want to specify "get" or "set" prefix explictly, or you want the field definition looks like a property rather than getter/setter function, you can define some auxiliary macros to achieve that. For example,
```
// auto add "get" prefix
#define G(type, name) (type, get ## name)
// auto add "get" and "set" prefix
#define GS(type, name) (type, get ## name, set ## name)
EVENTPP_MAKE_EVENT(EventDrawGS, Event, EventType::draw,
GS(std::string, Text),
G(int, X),
G(double, Size)
);
```
Of course you need to choose better macro names than `G` and `GS`, or `#undef` them after using them.

View File

@ -106,3 +106,24 @@ TEST_CASE("eventmake, TemplatedEvent")
REQUIRE(e.getText() == "world");
}
#define G(type, name) (type, get ## name)
#define GS(type, name) (type, get ## name, set ## name)
EVENTPP_MAKE_EVENT(EventDrawGS, Event, EventType::draw,
GS(std::string, Text),
G(int, X),
G(double, Size)
);
TEST_CASE("eventmake, EventDrawGS with auto getter/setter")
{
EventDrawGS e("Hello", 98, 3.5);
REQUIRE(e.getType() == EventType::draw);
REQUIRE(e.getText() == "Hello");
REQUIRE(e.getX() == 98);
REQUIRE(e.getSize() == 3.5);
e.setText("world");
REQUIRE(e.getText() == "world");
}