Skip to content

Commit

Permalink
check for removed observer (#13)
Browse files Browse the repository at this point in the history
* check for removed observer

* update Readme
  • Loading branch information
TheLartians authored Apr 23, 2019
1 parent 2217f49 commit 42c295a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
11 changes: 3 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ after_success:
- cmake -H. -Bbuild/coverage -DCMAKE_BUILD_TYPE=Debug -DLARS_EVENT_ENABLE_TESTS=On -DENABLE_TEST_COVERAGE=On -DCMAKE_CXX_COMPILER=g++-8
- cmake --build build/coverage
- cmake --build build/coverage --target test
# collect coverage data
- lcov --gcov-tool $(which gcov-8) --directory . --capture --output-file coverage.info
# remove lib files
- lcov --gcov-tool $(which gcov-8) --remove coverage.info '/usr/*' "${HOME}"'/.cache/*' --output-file coverage.info
# remove test files
- lcov --gcov-tool $(which gcov-8) --remove coverage.info $(pwd)/tests/*.cpp --output-file coverage.info
# print coverage info
# create coverage report
- lcov --gcov-tool $(which gcov-8) --directory . --capture --no-external --exclude "*tests*" --exclude "*_deps*" --quiet --output-file coverage.info
# output coverage
- lcov --gcov-tool $(which gcov-8) --list coverage.info
# upload coverage data
- bash <(curl -s https://codecov.io/bash) -f coverage.info || echo "Codecov did not collect coverage reports"
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# ---- Project ----

project(LarsEvent
VERSION 2.0.2
VERSION 2.0.3
LANGUAGES CXX
)

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ With [CPM](https://github.com/TheLartians/CPM), lars::Event can be used in a CMa
```cmake
CPMAddPackage(
NAME LarsEvent
VERSION 2.0.2
VERSION 2.0.3
GIT_REPOSITORY https://github.com/TheLartians/Event.git
)
Expand Down
17 changes: 12 additions & 5 deletions include/lars/event.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <utility>
#include <mutex>
#include <algorithm>

namespace lars{

Expand Down Expand Up @@ -45,7 +46,7 @@ namespace lars{

struct StoredHandler {
HandlerID id;
Handler callback;
std::shared_ptr<Handler> callback;
};

using HandlerList = std::vector<StoredHandler>;
Expand All @@ -60,7 +61,7 @@ namespace lars{

HandlerID addHandler(Handler h)const{
std::lock_guard<std::mutex> lock(data->observerMutex);
data->observers.emplace_back(StoredHandler{data->IDCounter,h});
data->observers.emplace_back(StoredHandler{data->IDCounter,std::make_shared<Handler>(h)});
return data->IDCounter++;
}

Expand Down Expand Up @@ -108,11 +109,17 @@ namespace lars{
}

void emit(Args ... args) const {
std::vector<std::weak_ptr<Handler>> handlers;
handlers.resize(data->observers.size());
data->observerMutex.lock();
auto tmpObservers = data->observers;
std::transform(data->observers.begin(), data->observers.end(), handlers.begin(), [](auto &h){
return h.callback;
});
data->observerMutex.unlock();
for(auto &observer: tmpObservers){
observer.callback(args...);
for(auto &weakCallback: handlers){
if(auto callback = weakCallback.lock()){
(*callback)(args...);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set_target_properties(lars-event-tests PROPERTIES CXX_STANDARD 17 COMPILE_FLAGS

ENABLE_TESTING()
ADD_TEST(lars-event-tests lars-event-tests)

# ---- code coverage ----

if (${ENABLE_TEST_COVERAGE})
Expand Down
20 changes: 15 additions & 5 deletions tests/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ TEST_CASE("Event"){
lars::Event<> event;
lars::Event<>::Observer observer;
unsigned count = 0;
observer = event.createObserver([&](){ observer.reset(); count++; });
event.emit();
REQUIRE(count == 1);
event.emit();
REQUIRE(count == 1);
SECTION("self removing"){
observer = event.createObserver([&](){ observer.reset(); count++; });
event.emit();
REQUIRE(count == 1);
event.emit();
REQUIRE(count == 1);
}
SECTION("other removing"){
event.connect([&](){ observer.reset(); });
observer = event.createObserver([&](){ count++; });
event.emit();
REQUIRE(count == 0);
event.emit();
REQUIRE(count == 0);
}
}

SECTION("adding observers during emit"){
Expand Down

0 comments on commit 42c295a

Please sign in to comment.