2024-01-30 11:21:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex/api/event_manager.hpp>
|
2024-02-19 22:06:46 +01:00
|
|
|
#include <hex/api/imhex_api.hpp>
|
2024-01-30 11:21:34 +01:00
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
2024-02-21 00:06:52 +01:00
|
|
|
namespace impl {
|
|
|
|
|
|
|
|
class AutoResetBase {
|
|
|
|
public:
|
|
|
|
virtual ~AutoResetBase() = default;
|
|
|
|
virtual void reset() = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2024-02-19 22:06:46 +01:00
|
|
|
|
2024-01-30 11:21:34 +01:00
|
|
|
template<typename T>
|
2024-02-21 00:06:52 +01:00
|
|
|
class AutoReset : public impl::AutoResetBase {
|
2024-01-30 11:21:34 +01:00
|
|
|
public:
|
|
|
|
using Type = T;
|
|
|
|
|
|
|
|
AutoReset() {
|
2024-02-19 22:06:46 +01:00
|
|
|
ImHexApi::System::impl::addAutoResetObject(this);
|
2024-01-30 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() {
|
|
|
|
return &m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T* operator->() const {
|
|
|
|
return &m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator*() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T&() {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator const T&() const {
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator=(const T &value) {
|
|
|
|
m_value = value;
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator=(T &&value) noexcept {
|
|
|
|
m_value = std::move(value);
|
|
|
|
return m_value;
|
|
|
|
}
|
|
|
|
|
2024-02-19 22:06:46 +01:00
|
|
|
void reset() override {
|
|
|
|
if constexpr (requires { m_value.reset(); }) {
|
|
|
|
m_value.reset();
|
|
|
|
} else if constexpr (requires { m_value.clear(); }) {
|
|
|
|
m_value.clear();
|
|
|
|
} else if constexpr (requires(T t) { t = nullptr; }) {
|
|
|
|
m_value = nullptr;
|
|
|
|
} else {
|
|
|
|
m_value = { };
|
|
|
|
}
|
2024-01-30 11:21:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T m_value;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|