aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'c/util.h')
-rw-r--r--c/util.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/c/util.h b/c/util.h
index 7ca8bfe..c08050d 100644
--- a/c/util.h
+++ b/c/util.h
@@ -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