mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-06-08 03:37:02 -04:00
824263fa6e
Roughly inspired by what I've learned from my work on Space Station 14, without some of the unnecessary cruft and complexity. Implementation is relatively simple once I figured out all the template order shenanigans.
30 lines
679 B
C++
30 lines
679 B
C++
#ifndef DUSK_SCOPE_HPP
|
|
#define DUSK_SCOPE_HPP
|
|
|
|
namespace dusk {
|
|
|
|
/**
|
|
* A simple value wrapper that will destroy the value at the end of its scope.
|
|
* @tparam T The type of value contained.
|
|
* @tparam Destructor The type of function used to destroy the value.
|
|
*/
|
|
template <typename T, typename Destructor>
|
|
struct ScopeValue {
|
|
T value;
|
|
Destructor destructor;
|
|
|
|
explicit ScopeValue(T value, Destructor destructor) : value(value), destructor(destructor) {
|
|
}
|
|
|
|
~ScopeValue() {
|
|
destructor(value);
|
|
}
|
|
|
|
constexpr operator T&() const noexcept {
|
|
return value;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // DUSK_SCOPE_HPP
|