mirror of
https://github.com/gelldur/EventBus.git
synced 2025-01-14 01:07:59 +08:00
Update code format according to clang-format
No code changes
This commit is contained in:
parent
bc466a3d6b
commit
2edc809a74
@ -8,8 +8,8 @@
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include <eventbus/internal/common.h>
|
||||
#include <eventbus/internal/AsyncCallbackVector.h>
|
||||
#include <eventbus/internal/common.h>
|
||||
|
||||
namespace Dexode
|
||||
{
|
||||
@ -26,7 +26,7 @@ public:
|
||||
|
||||
~AsyncEventBus()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_callbacksMutex};
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
_callbacks.clear();
|
||||
}
|
||||
|
||||
@ -69,10 +69,9 @@ public:
|
||||
{
|
||||
static_assert(Internal::validateEvent<Event>(), "Invalid event");
|
||||
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
_commandsQueue.push_back([this, token, callback = std::move(callback)]()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_callbacksMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_commandsQueue.push_back([this, token, callback = std::move(callback)]() {
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
|
||||
using Vector = Internal::AsyncCallbackVector<Event>;
|
||||
|
||||
@ -82,7 +81,7 @@ public:
|
||||
_callbacks[Internal::type_id<Event>()];
|
||||
if(vector == nullptr)
|
||||
{
|
||||
vector.reset(new Vector{});
|
||||
vector.reset(new Vector {});
|
||||
}
|
||||
assert(dynamic_cast<Vector*>(vector.get()));
|
||||
Vector* callbacks = static_cast<Vector*>(vector.get());
|
||||
@ -98,10 +97,9 @@ public:
|
||||
*/
|
||||
void unlistenAll(const int token)
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
_commandsQueue.push_back([this, token]()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_callbacksMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_commandsQueue.push_back([this, token]() {
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
for(auto& element : _callbacks)
|
||||
{
|
||||
element.second->remove(token);
|
||||
@ -121,10 +119,9 @@ public:
|
||||
{
|
||||
static_assert(Internal::validateEvent<Event>(), "Invalid event");
|
||||
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
_commandsQueue.push_back([this, token]()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_callbacksMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_commandsQueue.push_back([this, token]() {
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
|
||||
auto found = _callbacks.find(Internal::type_id<Event>);
|
||||
if(found != _callbacks.end())
|
||||
@ -144,10 +141,9 @@ public:
|
||||
{
|
||||
static_assert(Internal::validateEvent<Event>(), "Invalid event");
|
||||
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
_eventQueue.push_back([this, event = std::move(event)]()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_callbacksMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
_eventQueue.push_back([this, event = std::move(event)]() {
|
||||
std::lock_guard<std::mutex> guard {_callbacksMutex};
|
||||
|
||||
using Vector = Internal::AsyncCallbackVector<Event>;
|
||||
auto found = _callbacks.find(Internal::type_id<Event>());
|
||||
@ -177,14 +173,14 @@ public:
|
||||
|
||||
std::size_t getQueueEventCount() const
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
return _eventQueue.size();
|
||||
}
|
||||
|
||||
private:
|
||||
int newToken()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
int token = ++_tokener;
|
||||
return token;
|
||||
}
|
||||
|
@ -6,8 +6,8 @@
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
#include <eventbus/internal/common.h>
|
||||
#include <eventbus/internal/TransactionCallbackVector.h>
|
||||
#include <eventbus/internal/common.h>
|
||||
|
||||
namespace Dexode
|
||||
{
|
||||
@ -62,7 +62,7 @@ public:
|
||||
std::unique_ptr<Internal::CallbackVector>& vector = _callbacks[Internal::type_id<Event>()];
|
||||
if(vector == nullptr)
|
||||
{
|
||||
vector.reset(new Vector{});
|
||||
vector.reset(new Vector {});
|
||||
}
|
||||
assert(dynamic_cast<Vector*>(vector.get()));
|
||||
Vector* vectorImpl = static_cast<Vector*>(vector.get());
|
||||
|
@ -15,4 +15,4 @@ namespace Dexode
|
||||
// [[deprecated("Deprecating EventCollector. Try move to: TokenHolder<>")]]
|
||||
using EventCollector = TokenHolder<EventBus>;
|
||||
|
||||
}
|
||||
} // namespace Dexode
|
||||
|
@ -9,8 +9,7 @@ namespace
|
||||
|
||||
template <class Bus>
|
||||
void null_deleter(Bus*)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
} // namespace
|
||||
|
||||
@ -25,20 +24,18 @@ class TokenHolder
|
||||
{
|
||||
public:
|
||||
TokenHolder(const std::shared_ptr<Bus>& bus)
|
||||
: _bus{bus}
|
||||
: _bus {bus}
|
||||
{
|
||||
assert(_bus);
|
||||
}
|
||||
|
||||
TokenHolder(Bus* bus)
|
||||
: _bus(bus, &null_deleter<Bus>)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
TokenHolder(const TokenHolder& other)
|
||||
: _bus(other._bus)
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
TokenHolder(TokenHolder&& other)
|
||||
: _token(other._token)
|
||||
|
@ -21,8 +21,7 @@ struct AsyncCallbackVector : public CallbackVector
|
||||
virtual void remove(const int token) override
|
||||
{
|
||||
auto removeFrom = std::remove_if(
|
||||
container.begin(), container.end(), [token](const ContainerElement& element)
|
||||
{
|
||||
container.begin(), container.end(), [token](const ContainerElement& element) {
|
||||
return element.first == token;
|
||||
});
|
||||
if(removeFrom != container.end())
|
||||
|
@ -32,8 +32,7 @@ struct TransactionCallbackVector : public CallbackVector
|
||||
|
||||
//Invalidation rules: https://stackoverflow.com/questions/6438086/iterator-invalidation-rules
|
||||
auto removeFrom = std::remove_if(
|
||||
container.begin(), container.end(), [token](const ContainerElement& element)
|
||||
{
|
||||
container.begin(), container.end(), [token](const ContainerElement& element) {
|
||||
return element.first == token;
|
||||
});
|
||||
if(removeFrom != container.end())
|
||||
|
@ -15,8 +15,6 @@ type_id_t type_id() // Helper for getting "type id"
|
||||
return typeid(T).hash_code();
|
||||
}
|
||||
|
||||
|
||||
|
||||
template <class Event>
|
||||
constexpr bool validateEvent()
|
||||
{
|
||||
|
@ -7,13 +7,15 @@ namespace Dexode
|
||||
|
||||
std::size_t AsyncEventBus::processCommandsAndGetQueuedEventsCount()
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
while(_commandsQueue.empty() == false)
|
||||
{
|
||||
_commandsQueue.front()();//This can't add any extra commands, because in this queue we story only listen/unlisten stuff
|
||||
_commandsQueue
|
||||
.front()(); //This can't add any extra commands, because in this queue we story only listen/unlisten stuff
|
||||
_commandsQueue.pop_front();
|
||||
}
|
||||
return _eventQueue.size(); //Yeah we want to return events count. So don't have to call getQueueEventCount
|
||||
//Yeah we want to return events count. So don't have to call getQueueEventCount
|
||||
return _eventQueue.size();
|
||||
}
|
||||
|
||||
int AsyncEventBus::consume(int max)
|
||||
@ -29,7 +31,7 @@ int AsyncEventBus::consume(int max)
|
||||
while(processCommandsAndGetQueuedEventsCount() > 0 && consumed < max) //order is important
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> guard{_eventMutex};
|
||||
std::lock_guard<std::mutex> guard {_eventMutex};
|
||||
eventCommand = std::move(_eventQueue.front());
|
||||
_eventQueue.pop_front();
|
||||
}
|
||||
|
@ -16,7 +16,7 @@ struct Gold // Event that will be proceed when our gold changes
|
||||
{
|
||||
int value = 0;
|
||||
};
|
||||
}
|
||||
} // namespace Event
|
||||
|
||||
enum class Monster
|
||||
{
|
||||
@ -29,9 +29,8 @@ class Character
|
||||
{
|
||||
public:
|
||||
Character(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
: _bus{eventBus}
|
||||
{
|
||||
}
|
||||
: _bus {eventBus}
|
||||
{}
|
||||
|
||||
void kill(Monster monsterType)
|
||||
{
|
||||
@ -47,7 +46,7 @@ public:
|
||||
{
|
||||
_gold += 25;
|
||||
}
|
||||
_bus->notify(Event::Gold{_gold});
|
||||
_bus->notify(Event::Gold {_gold});
|
||||
}
|
||||
|
||||
private:
|
||||
@ -59,9 +58,8 @@ class UIWallet
|
||||
{
|
||||
public:
|
||||
UIWallet(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
: _listener{eventBus}
|
||||
{
|
||||
}
|
||||
: _listener {eventBus}
|
||||
{}
|
||||
|
||||
void onDraw() // Example "draw" of UI
|
||||
{
|
||||
@ -69,10 +67,10 @@ public:
|
||||
}
|
||||
|
||||
void onEnter() // We could also do such things in ctor and dtor but a lot of UI has something
|
||||
// like this
|
||||
// like this
|
||||
{
|
||||
_listener.listen<Event::Gold>(
|
||||
[this](const auto& event) { _gold = std::to_string(event.value); });
|
||||
[this](const auto& event) { _gold = std::to_string(event.value); });
|
||||
}
|
||||
|
||||
void onExit()
|
||||
@ -86,15 +84,15 @@ private:
|
||||
};
|
||||
|
||||
class ShopButton // Shop button is only enabled when we have some gold (odd decision but for sample
|
||||
// good :P)
|
||||
// good :P)
|
||||
{
|
||||
public:
|
||||
ShopButton(const std::shared_ptr<Dexode::EventBus>& eventBus)
|
||||
: _listener{eventBus}
|
||||
: _listener {eventBus}
|
||||
{
|
||||
// We can use lambda or bind your choice
|
||||
_listener.listen<Event::Gold>(
|
||||
std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
|
||||
std::bind(&ShopButton::onGoldUpdated, this, std::placeholders::_1));
|
||||
// Also we use RAII idiom to handle unlisten
|
||||
}
|
||||
|
||||
@ -118,11 +116,11 @@ int main(int argc, char* argv[])
|
||||
{
|
||||
std::shared_ptr<Dexode::EventBus> eventBus = std::make_shared<Dexode::EventBus>();
|
||||
|
||||
Character characterController{eventBus};
|
||||
Character characterController {eventBus};
|
||||
|
||||
UIWallet wallet{eventBus}; // UIWallet doesn't know anything about character
|
||||
// or even who store gold
|
||||
ShopButton shopButton{eventBus}; // ShopButton doesn't know anything about character
|
||||
UIWallet wallet {eventBus}; // UIWallet doesn't know anything about character
|
||||
// or even who store gold
|
||||
ShopButton shopButton {eventBus}; // ShopButton doesn't know anything about character
|
||||
{
|
||||
wallet.onEnter();
|
||||
}
|
||||
@ -134,9 +132,9 @@ int main(int argc, char* argv[])
|
||||
wallet.onDraw();
|
||||
|
||||
// It is easy to test UI eg.
|
||||
eventBus->notify(Event::Gold{1});
|
||||
eventBus->notify(Event::Gold {1});
|
||||
assert(shopButton.isEnabled() == true);
|
||||
eventBus->notify(Event::Gold{0});
|
||||
eventBus->notify(Event::Gold {0});
|
||||
assert(shopButton.isEnabled() == false);
|
||||
|
||||
wallet.onExit();
|
||||
|
@ -1,7 +1,7 @@
|
||||
#include <thread>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
@ -10,8 +10,8 @@
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
const auto ns2 = std::chrono::nanoseconds{2};// GCC 7 has some issues with 2ms :/
|
||||
const auto ns3 = std::chrono::nanoseconds{3};
|
||||
const auto ns2 = std::chrono::nanoseconds {2}; // GCC 7 has some issues with 2ms :/
|
||||
const auto ns3 = std::chrono::nanoseconds {3};
|
||||
|
||||
TEST_CASE("Should consume events in synchronous way When using AsyncEventBus", "[AsyncEventBus]")
|
||||
{
|
||||
@ -24,25 +24,22 @@ TEST_CASE("Should consume events in synchronous way When using AsyncEventBus", "
|
||||
|
||||
int counter = 0;
|
||||
|
||||
bus.listen<SimpleEvent>([&counter](const SimpleEvent& event)
|
||||
{
|
||||
bus.listen<SimpleEvent>([&counter](const SimpleEvent& event) {
|
||||
std::cout << "Event from: " << event.id << std::endl;
|
||||
++counter;
|
||||
});
|
||||
|
||||
std::thread worker1{[&bus]()
|
||||
{
|
||||
std::thread worker1 {[&bus]() {
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
std::this_thread::sleep_for(ns3);
|
||||
}
|
||||
}};
|
||||
std::thread worker2{[&bus]()
|
||||
{
|
||||
std::thread worker2 {[&bus]() {
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
std::this_thread::sleep_for(ns2);
|
||||
}
|
||||
}};
|
||||
@ -73,26 +70,25 @@ TEST_CASE("Should unlisten for event When call unlisten inside Listener", "[Asyn
|
||||
|
||||
const int myToken = 0x23167;
|
||||
|
||||
bus.listen<SimpleEvent>(myToken, [&counter, &bus](const SimpleEvent& event)
|
||||
{
|
||||
bus.listen<SimpleEvent>(myToken, [&counter, &bus](const SimpleEvent& event) {
|
||||
std::cout << "Event from: " << event.id << std::endl;
|
||||
++counter;
|
||||
bus.unlistenAll(myToken);//This doesn't mean that unlisten will be ASAP!
|
||||
bus.unlistenAll(myToken); //This doesn't mean that unlisten will be ASAP!
|
||||
});
|
||||
|
||||
REQUIRE(counter == 0);
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
//This should consume (listen request), SimpleEvent, ()unlisten request
|
||||
REQUIRE(bus.consume(1) == 1);
|
||||
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.consume();
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
bus.consume();
|
||||
}
|
||||
|
||||
REQUIRE(counter == 1);//Should be called only once
|
||||
REQUIRE(counter == 1); //Should be called only once
|
||||
}
|
||||
|
||||
TEST_CASE("Should listen for only 1 event When call unlisten inside Listener", "[AsyncEventBus]")
|
||||
@ -108,28 +104,25 @@ TEST_CASE("Should listen for only 1 event When call unlisten inside Listener", "
|
||||
|
||||
const int myToken = 0x23167;
|
||||
|
||||
bus.listen<SimpleEvent>(myToken, [&counter, &bus](const SimpleEvent& event)
|
||||
{
|
||||
bus.listen<SimpleEvent>(myToken, [&counter, &bus](const SimpleEvent& event) {
|
||||
std::cout << "Event from: " << event.id << std::endl;
|
||||
++counter;
|
||||
bus.unlistenAll(myToken);//This doesn't mean that unlisten will be ASAP!
|
||||
bus.unlistenAll(myToken); //This doesn't mean that unlisten will be ASAP!
|
||||
});
|
||||
|
||||
//Workers in this test are only extra stuff
|
||||
std::thread worker1{[&bus]()
|
||||
{
|
||||
std::thread worker1 {[&bus]() {
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
std::this_thread::sleep_for(ns3);
|
||||
}
|
||||
}};
|
||||
//Workers in this test are only extra stuff
|
||||
std::thread worker2{[&bus]()
|
||||
{
|
||||
std::thread worker2 {[&bus]() {
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
std::this_thread::sleep_for(ns2);
|
||||
}
|
||||
}};
|
||||
@ -138,12 +131,12 @@ TEST_CASE("Should listen for only 1 event When call unlisten inside Listener", "
|
||||
|
||||
for(int i = 0; i < 10; ++i)
|
||||
{
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent{std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
bus.schedule(SimpleEvent {std::this_thread::get_id()});
|
||||
bus.consume();
|
||||
}
|
||||
|
||||
REQUIRE(counter == 1);//Should be called only once
|
||||
REQUIRE(counter == 1); //Should be called only once
|
||||
worker1.join();
|
||||
worker2.join();
|
||||
}
|
||||
|
@ -16,16 +16,15 @@ TEST_CASE("eventbus/EventCollector sample", "Simple test for EventCollector")
|
||||
Dexode::EventBus bus;
|
||||
int callCount = 0;
|
||||
{
|
||||
Dexode::EventCollector listener{&bus};
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent{3});
|
||||
Dexode::EventCollector listener {&bus};
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event) {
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent {3});
|
||||
REQUIRE(callCount == 1);
|
||||
}
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
REQUIRE(callCount == 1);
|
||||
}
|
||||
|
||||
@ -36,18 +35,17 @@ TEST_CASE("eventbus/EventCollector unlistenAll", "EventCollector::unlistenAll")
|
||||
int value;
|
||||
};
|
||||
Dexode::EventBus bus;
|
||||
Dexode::EventCollector listener{&bus};
|
||||
Dexode::EventCollector listener {&bus};
|
||||
|
||||
int callCount = 0;
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent{3});
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event) {
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent {3});
|
||||
listener.unlistenAll();
|
||||
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
REQUIRE(callCount == 1);
|
||||
}
|
||||
|
||||
@ -60,16 +58,15 @@ TEST_CASE("eventbus/EventCollector reset", "EventCollector reset when we reasign
|
||||
|
||||
Dexode::EventBus bus;
|
||||
int callCount = 0;
|
||||
Dexode::EventCollector listener{&bus};
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent{3});
|
||||
Dexode::EventCollector listener {&bus};
|
||||
listener.listen<SimpleEvent>([&](const SimpleEvent& event) {
|
||||
REQUIRE(event.value == 3);
|
||||
++callCount;
|
||||
});
|
||||
bus.notify(SimpleEvent {3});
|
||||
REQUIRE(callCount == 1);
|
||||
listener = {nullptr};
|
||||
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
REQUIRE(callCount == 1);
|
||||
}
|
||||
|
@ -15,20 +15,15 @@ TEST_CASE("eventbus/Simple test", "Simple test")
|
||||
int value;
|
||||
};
|
||||
|
||||
const auto token = bus.listen<SimpleEvent>([](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 3);
|
||||
});
|
||||
const auto token =
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event) { REQUIRE(event.value == 3); });
|
||||
|
||||
bus.notify(SimpleEvent{3});
|
||||
bus.notify(SimpleEvent {3});
|
||||
bus.unlistenAll(token);
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.notify(SimpleEvent{1});
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event) { REQUIRE(event.value == 1); });
|
||||
bus.notify(SimpleEvent {1});
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/Simple test2", "Simple test")
|
||||
@ -39,23 +34,19 @@ TEST_CASE("eventbus/Simple test2", "Simple test")
|
||||
int value;
|
||||
};
|
||||
|
||||
const auto token = bus.listen<SimpleEvent>([](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 3);
|
||||
});
|
||||
const auto token =
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event) { REQUIRE(event.value == 3); });
|
||||
|
||||
bus.notify<SimpleEvent>({3});
|
||||
bus.unlistenAll(token);
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event)
|
||||
{
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.notify(SimpleEvent{1});
|
||||
bus.listen<SimpleEvent>([](const SimpleEvent& event) { REQUIRE(event.value == 1); });
|
||||
bus.notify(SimpleEvent {1});
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus listen & notify", "Listen & notify without notification object. Using only string")
|
||||
TEST_CASE("eventbus/EventBus listen & notify",
|
||||
"Listen & notify without notification object. Using only string")
|
||||
{
|
||||
int isCalled = 0;
|
||||
Dexode::EventBus bus;
|
||||
@ -64,24 +55,22 @@ TEST_CASE("eventbus/EventBus listen & notify", "Listen & notify without notifica
|
||||
int value;
|
||||
};
|
||||
|
||||
const auto token = bus.listen<SimpleEvent>([&](const SimpleEvent& event)
|
||||
{
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 3);
|
||||
});
|
||||
const auto token = bus.listen<SimpleEvent>([&](const SimpleEvent& event) {
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 3);
|
||||
});
|
||||
REQUIRE(isCalled == 0);
|
||||
bus.notify(SimpleEvent{3});
|
||||
bus.notify(SimpleEvent {3});
|
||||
REQUIRE(isCalled == 1);
|
||||
bus.unlistenAll(token);
|
||||
bus.notify(SimpleEvent{2});
|
||||
bus.notify(SimpleEvent {2});
|
||||
REQUIRE(isCalled == 1);
|
||||
|
||||
bus.listen<SimpleEvent>([&](const SimpleEvent& event)
|
||||
{
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.notify(SimpleEvent{1});
|
||||
bus.listen<SimpleEvent>([&](const SimpleEvent& event) {
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.notify(SimpleEvent {1});
|
||||
REQUIRE(isCalled == 2);
|
||||
}
|
||||
|
||||
@ -100,27 +89,25 @@ TEST_CASE("eventbus/Different notification", "Valid check notification")
|
||||
bool called1 = false;
|
||||
bool called2 = false;
|
||||
|
||||
bus.listen<SimpleEvent1>([&called1](const SimpleEvent1& event)
|
||||
{
|
||||
called1 = true;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.listen<SimpleEvent1>([&called1](const SimpleEvent1& event) {
|
||||
called1 = true;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
|
||||
bus.listen<SimpleEvent2>([&called2](const SimpleEvent2& event)
|
||||
{
|
||||
called2 = true;
|
||||
REQUIRE(event.value == 2);
|
||||
});
|
||||
bus.listen<SimpleEvent2>([&called2](const SimpleEvent2& event) {
|
||||
called2 = true;
|
||||
REQUIRE(event.value == 2);
|
||||
});
|
||||
|
||||
REQUIRE(called1 == false);
|
||||
|
||||
bus.notify(SimpleEvent1{1});
|
||||
bus.notify(SimpleEvent1 {1});
|
||||
|
||||
REQUIRE(called1 == true);
|
||||
REQUIRE(called2 == false);
|
||||
called1 = false;
|
||||
|
||||
bus.notify(SimpleEvent2{2});
|
||||
bus.notify(SimpleEvent2 {2});
|
||||
|
||||
REQUIRE(called1 == false);
|
||||
REQUIRE(called2 == true);
|
||||
@ -132,77 +119,74 @@ struct SimpleEvent
|
||||
{
|
||||
int value;
|
||||
};
|
||||
}
|
||||
} // namespace Scope1
|
||||
namespace Scope2
|
||||
{
|
||||
struct SimpleEvent
|
||||
{
|
||||
int value;
|
||||
};
|
||||
}
|
||||
} // namespace Scope2
|
||||
|
||||
TEST_CASE("eventbus/EventBus different events", "Events with the same name but different scope should be different")
|
||||
TEST_CASE("eventbus/EventBus different events",
|
||||
"Events with the same name but different scope should be different")
|
||||
{
|
||||
int isCalled = 0;
|
||||
Dexode::EventBus bus;
|
||||
|
||||
bus.listen<Scope1::SimpleEvent>([&](const Scope1::SimpleEvent& event)
|
||||
{
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.listen<Scope2::SimpleEvent>([&](const Scope2::SimpleEvent& event)
|
||||
{
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 2);
|
||||
});
|
||||
bus.listen<Scope1::SimpleEvent>([&](const Scope1::SimpleEvent& event) {
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 1);
|
||||
});
|
||||
bus.listen<Scope2::SimpleEvent>([&](const Scope2::SimpleEvent& event) {
|
||||
++isCalled;
|
||||
REQUIRE(event.value == 2);
|
||||
});
|
||||
REQUIRE(isCalled == 0);
|
||||
|
||||
bus.notify(Scope1::SimpleEvent{1});
|
||||
bus.notify(Scope1::SimpleEvent {1});
|
||||
REQUIRE(isCalled == 1);
|
||||
|
||||
bus.notify(Scope2::SimpleEvent{2});
|
||||
bus.notify(Scope2::SimpleEvent {2});
|
||||
REQUIRE(isCalled == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during notify"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during notify",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during notify2"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during notify2",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
@ -210,43 +194,38 @@ TEST_CASE("eventbus/EventBus modification during notify2"
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token3, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token3, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 3);
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during notify3"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during notify3",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
@ -254,38 +233,33 @@ TEST_CASE("eventbus/EventBus modification during notify3"
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token3, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
bus.listen<TestEvent>(token3, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during notify4"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during notify4",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
@ -293,41 +267,36 @@ TEST_CASE("eventbus/EventBus modification during notify4"
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token3, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
});
|
||||
bus.listen<TestEvent>(token3, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during notify5"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during notify5",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
@ -335,42 +304,38 @@ TEST_CASE("eventbus/EventBus modification during notify5"
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token3, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
});
|
||||
bus.listen<TestEvent>(token3, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 3);
|
||||
}
|
||||
|
||||
TEST_CASE("eventbus/EventBus modification during nested notify"
|
||||
, "Remove listener during notification should not malform EventBus")
|
||||
TEST_CASE("eventbus/EventBus modification during nested notify",
|
||||
"Remove listener during notification should not malform EventBus")
|
||||
{
|
||||
Dexode::EventBus bus;
|
||||
struct TestEvent {};
|
||||
struct TestEvent2 {};
|
||||
struct TestEvent
|
||||
{};
|
||||
struct TestEvent2
|
||||
{};
|
||||
|
||||
int token1 = 1;
|
||||
int token2 = 2;
|
||||
@ -378,34 +343,28 @@ TEST_CASE("eventbus/EventBus modification during nested notify"
|
||||
|
||||
int calls = 0;
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token1, [&](const TestEvent& event)
|
||||
{
|
||||
bus.notify(TestEvent2{});
|
||||
bus.listen<TestEvent>(token1, [&](const TestEvent& event) {
|
||||
bus.notify(TestEvent2 {});
|
||||
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
|
||||
bus.listen<TestEvent>
|
||||
(token2, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
});
|
||||
bus.listen<TestEvent>
|
||||
(token3, [&](const TestEvent& event)
|
||||
{
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
bus.listen<TestEvent>(token2, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token3);
|
||||
bus.unlistenAll(token2);
|
||||
});
|
||||
});
|
||||
bus.listen<TestEvent>(token3, [&](const TestEvent& event) {
|
||||
++calls;
|
||||
bus.unlistenAll(token1);
|
||||
bus.unlistenAll(token2);
|
||||
bus.unlistenAll(token3);
|
||||
});
|
||||
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent{}));
|
||||
REQUIRE_NOTHROW(bus.notify(TestEvent {}));
|
||||
REQUIRE(calls == 2);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user