mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-05 19:11:50 +00:00
add observer_handler class
This commit is contained in:
parent
b3feb5945b
commit
3beafe953f
6 changed files with 72 additions and 13 deletions
40
src/util/observer_handler.hpp
Normal file
40
src/util/observer_handler.hpp
Normal file
|
|
@ -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;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue