From 3962b1bdfb2a8a2e3a5ff4f4e51a61b0c44f2e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Tue, 23 Aug 2022 01:43:09 +0200 Subject: Add Managed (generic RAII type). --- c/util.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'c/util.h') 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 +struct Managed +{ + T obj; + Managed(T obj) : obj(obj) + { + if (obj == reinterpret_cast(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 struct Buf -- cgit v1.2.3