aboutsummaryrefslogtreecommitdiff
path: root/c/util.h
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2022-08-23 01:43:09 +0200
committerJohn Ankarström <john@ankarstrom.se>2022-08-23 01:43:09 +0200
commit3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b (patch)
tree1d7ee76b4f2bfb3f205f6cacb39afdbd68062545 /c/util.h
parent74b9361ccc77bcbb6b8188ad5914d6b26530d26a (diff)
downloadEpisodeBrowser-3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b.tar.gz
Add Managed (generic RAII type).
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