diff options
Diffstat (limited to 'c/util.h')
-rw-r--r-- | c/util.h | 16 |
1 files changed, 16 insertions, 0 deletions
@@ -23,6 +23,22 @@ inline int Cmp(const int a, const int b) return -1; } +template <typename T, auto F, typename U, auto E = 0> +struct Managed +{ + T obj; + Managed(T obj) : obj(obj) + { + if (obj == reinterpret_cast<T>(E)) + throw U(); + } + ~Managed() { F(obj); } + operator T() { return obj; } + auto& operator *() { return *obj; } + auto& operator ->() { return obj; } + auto& operator [](size_t i) { return obj[i]; } +}; + /* Buf is a span-like structure of a buffer and its size. */ template <typename T> struct Buf |