diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-23 01:43:09 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-23 01:43:09 +0200 |
commit | 3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b (patch) | |
tree | 1d7ee76b4f2bfb3f205f6cacb39afdbd68062545 /c/util.h | |
parent | 74b9361ccc77bcbb6b8188ad5914d6b26530d26a (diff) | |
download | EpisodeBrowser-3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b.tar.gz |
Add Managed (generic RAII type).
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 |