diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-28 04:02:24 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-28 04:02:24 +0200 |
commit | e327e4469ac2847615f7c847facd5879b5c2e5bc (patch) | |
tree | b7db3fb2383be3364ea2692769a8d02e708e3478 /c/util.h | |
parent | a8a73a21495bf54f720088cf789d8cf6cf682661 (diff) | |
download | EpisodeBrowser-e327e4469ac2847615f7c847facd5879b5c2e5bc.tar.gz |
Replace Managed with Unique.
Diffstat (limited to 'c/util.h')
-rw-r--r-- | c/util.h | 56 |
1 files changed, 44 insertions, 12 deletions
@@ -28,21 +28,53 @@ struct Finally }; #define FINALLY Finally _ = [=]() -/* Generic RAII type. */ -template <typename T, auto F, typename U, auto E = 0> -struct Managed +template <typename T> +inline void Delete(T v) +{ + delete v; +} + +template <typename T, auto F = Delete<T>> +struct Unique { - T obj; - Managed(T obj) : obj(obj) + T v; + bool ok = true; + Unique() : ok(false) {} + Unique(T v) : v(v) {} + Unique& operator =(T v_) + { + if (ok) + F(v); + v = v_; + ok = true; + return *this; + } + Unique(Unique&& other) + { + v = std::move(other.v); + other.ok = false; + } + Unique& operator =(Unique&& other) + { + if (ok) + F(v); + v = std::move(other.v); + ok = other.ok; + other.ok = false; + return *this; + } + bool Not(T u) + { + if (v == u) + return ok = false; + else + return true; + } + ~Unique() { - if (obj == reinterpret_cast<T>(E)) - throw U(); + if (ok) + F(v); } - ~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. */ |