@@ -33,11 +33,15 @@ normal C++ inheritance -- Derived must extend Base via the
3333mpm::polymorphic_event class rather than directly. Rather than the following
3434"normal" inheritance:
3535
36- struct Derived : Base {}
36+ ~~~ {.cpp}
37+ struct Derived : Base {}
38+ ~~~
3739
3840Derived should be defined as
3941
40- struct Derived : mpm::polymorphic_event<Derived, Base> {}
42+ ~~~ {.cpp}
43+ struct Derived : mpm::polymorphic_event<Derived, Base> {}
44+ ~~~
4145
4246Defining Derived in this manner will allow and event of this type to be
4347delivered as both Derived and Base.
@@ -50,69 +54,76 @@ mpm::polymorphic_event must be used.
5054
5155Let's define some events
5256
53- struct my_base_event : mpm::polymorphic_event<my_base_event>
54- {
55- int x = 12;
56- };
57+ ~~~ {.cpp}
58+ struct my_base_event : mpm::polymorphic_event<my_base_event>
59+ {
60+ int x = 12;
61+ };
5762
58- struct my_derived_event : mpm::polymorphic_event<my_derived_event, my_base_event>
59- {
60- int y = 5;
61- };
63+ struct my_derived_event : mpm::polymorphic_event<my_derived_event, my_base_event>
64+ {
65+ int y = 5;
66+ };
6267
63- struct my_object {};
68+ struct my_object {};
6469
65- struct my_non_polymorphic_event : my_object
66- {
67- int foo = -1;
68- };
70+ struct my_non_polymorphic_event : my_object
71+ {
72+ int foo = -1;
73+ };
74+ ~~~
6975
7076Here's a quick look at publishing and subscribing to a polymorphic event
7177
72- mpm::eventbus ebus;
78+ ~~~ {.cpp}
79+ mpm::eventbus ebus;
7380
74- // two subscriptions - 1 for my_base_event and 1 for my_derived_event
75- auto base_subscription = scoped_subscription<my_base_event> { ebus,
76- [](const my_base_event& mbe) noexcept { std::cout << "handling a base event" << mbe.x; }
77- );
78- auto derived_subscription = scoped_subscription<my_derived_event> { ebus,
79- [](const my_derived_event& mde) noexcept { std::cout << "handling a derived event" << mde.y; }
80- );
81-
82- // publish
83- ebus.publish(my_derived_event{});
81+ // two subscriptions - 1 for my_base_event and 1 for my_derived_event
82+ auto base_subscription = scoped_subscription<my_base_event> {
83+ ebus, [](const my_base_event& mbe) noexcept {
84+ std::cout << "handling a base event" << mbe.x;
85+ }
86+ );
87+ auto derived_subscription = scoped_subscription<my_derived_event> { ebus,
88+ ebus, [](const my_derived_event& mde) noexcept {
89+ std::cout << "handling a derived event" << mde.y;
90+ }
91+ );
8492
85- // subscriptions terminate at scope exit
93+ // publish
94+ ebus.publish(my_derived_event{});
95+
96+ // subscriptions terminate at scope exit
97+ ~~~
8698
8799Some things worth noting here
88100* Both event handlers will fire
89- * It's generally preferable to use the provided mpm::scoped_subscription\< EventType>
90- RAII container to handle the unsubscribe call automatically. I have elided
91- unsubscription here for clarity.
92101* If you have a C++14 compiler, the callback can be declared with auto (e.g.
93102 const auto& event), removing the duplication in specifying the event type.
94103
95104For non-polymorphic dispatch, any object type can be published and it will be
96105handled by handlers for _ only_ that exact type.
97106
98- mpm::eventbus ebus;
107+ ~~~ {.cpp}
108+ mpm::eventbus ebus;
99109
100- // two subscriptions - 1 for my_object, 1 for my_non_polymorphic_event
101- auto base_subscription = scoped_subscription<my_object> {
102- ebus, [](const my_object& mo) noexcept {
103- std::cout << "handling a my_object";
104- }
105- };
106- auto non_poly_subscription = scoped_subscription<my_non_polymorhpic_event> {
107- ebus, [](const my_non_polymorphic_event& mnpe) noexcept {
108- std::cout << "handling a my_non_polymorphic_event " << mnpe.foo;
109- }
110- );
110+ // two subscriptions - 1 for my_object, 1 for my_non_polymorphic_event
111+ auto base_subscription = scoped_subscription<my_object> {
112+ ebus, [](const my_object& mo) noexcept {
113+ std::cout << "handling a my_object";
114+ }
115+ };
116+ auto non_poly_subscription = scoped_subscription<my_non_polymorhpic_event> {
117+ ebus, [](const my_non_polymorphic_event& mnpe) noexcept {
118+ std::cout << "handling a my_non_polymorphic_event " << mnpe.foo;
119+ }
120+ );
111121
112- // publish
113- ebus.publish(my_non_polymorphic_event{});
122+ // publish
123+ ebus.publish(my_non_polymorphic_event{});
114124
115- // subscriptions terminate at scope exit
125+ // subscriptions terminate at scope exit
126+ ~~~
116127
117128Note with the above example that _ only_ the handler for my_non_polymorphic_event
118129will fire because the inheritance relationship was not established via
@@ -124,11 +135,13 @@ There's really no build needed as this is a header-only library, however if you
124135want to run the unit tests or generate docs you can use the cmake build. To
125136perform an out-of-tree build
126137
127- $ cd /tmp # or wherever
128- $ mkdir eventbus-debug && cd $_
129- $ cmake -DCMAKE_BUILD_TYPE=debug /path/to/eventbus/repository
130- $ make && make test
131- $ make docs # generates doxygen documentation under /tmp/eventbus-debug/docs
138+ ~~~ {.txt}
139+ $ cd /tmp # or wherever
140+ $ mkdir eventbus-debug && cd $_
141+ $ cmake -DCMAKE_BUILD_TYPE=debug /path/to/eventbus/repository
142+ $ make && make test
143+ $ make docs # generates doxygen documentation under /tmp/eventbus-debug/docs
144+ ~~~
132145
133146## TODO
134147- As it stands, publishing events from within event handlers is allowed. It's
0 commit comments