add observer_handler class
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
#include "delegates.hpp"
|
||||
#include "typedefs.hpp"
|
||||
#include "util/observer_handler.hpp"
|
||||
|
||||
namespace util {
|
||||
template <class... Types>
|
||||
@@ -35,13 +36,12 @@ namespace util {
|
||||
int id = nextid++;
|
||||
handlers[id] = std::move(handler);
|
||||
order.push_back(id);
|
||||
return observer_handler(new int(id), [this](int* id) { //-V508
|
||||
return observer_handler([this, id]() {
|
||||
std::lock_guard lock(mutex);
|
||||
handlers.erase(*id);
|
||||
handlers.erase(id);
|
||||
order.erase(
|
||||
std::remove(order.begin(), order.end(), *id), order.end()
|
||||
std::remove(order.begin(), order.end(), id), order.end()
|
||||
);
|
||||
delete id;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -3,20 +3,39 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "util/observer_handler.hpp"
|
||||
|
||||
namespace util {
|
||||
/// @brief Keeps shared pointers alive until destruction
|
||||
class ObjectsKeeper {
|
||||
std::vector<std::shared_ptr<void>> ptrs;
|
||||
std::vector<observer_handler> handlers;
|
||||
public:
|
||||
ObjectsKeeper() = default;
|
||||
|
||||
ObjectsKeeper(const ObjectsKeeper&) = delete;
|
||||
|
||||
ObjectsKeeper(ObjectsKeeper&& keeper) noexcept
|
||||
: ptrs(std::move(keeper.ptrs)),
|
||||
handlers(std::move(keeper.handlers)) {
|
||||
}
|
||||
|
||||
ObjectsKeeper& operator=(ObjectsKeeper&& keeper) noexcept = default;
|
||||
|
||||
virtual ~ObjectsKeeper() {
|
||||
}
|
||||
|
||||
virtual void keepAlive(std::shared_ptr<void> ptr) {
|
||||
ptrs.push_back(ptr);
|
||||
ptrs.push_back(std::move(ptr));
|
||||
}
|
||||
|
||||
virtual void keepAlive(observer_handler&& ptr) {
|
||||
handlers.emplace_back(std::move(ptr));
|
||||
}
|
||||
|
||||
virtual void clearKeepedObjects() {
|
||||
ptrs.clear();
|
||||
handlers.clear();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
class observer_handler {
|
||||
public:
|
||||
observer_handler() = default;
|
||||
|
||||
observer_handler(std::function<void()> destructor)
|
||||
: destructor(std::move(destructor)) {
|
||||
}
|
||||
|
||||
observer_handler(const observer_handler&) = delete;
|
||||
|
||||
observer_handler(observer_handler&& handler) noexcept
|
||||
: destructor(std::move(handler.destructor)) {
|
||||
}
|
||||
|
||||
~observer_handler() {
|
||||
if (destructor) {
|
||||
destructor();
|
||||
}
|
||||
}
|
||||
|
||||
bool operator==(std::nullptr_t) const {
|
||||
return destructor == nullptr;
|
||||
}
|
||||
|
||||
observer_handler& operator=(const observer_handler& handler) = delete;
|
||||
|
||||
observer_handler& operator=(observer_handler&& handler) noexcept {
|
||||
if (destructor) {
|
||||
destructor();
|
||||
}
|
||||
destructor = std::move(handler.destructor);
|
||||
return *this;
|
||||
}
|
||||
private:
|
||||
std::function<void()> destructor;
|
||||
};
|
||||
Reference in New Issue
Block a user