diff options
author | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-09-21 16:11:39 +0200 |
---|---|---|
committer | Alexey Rusakov <Kitsune-Ral@users.sf.net> | 2022-09-26 10:46:34 +0200 |
commit | bc1ded73bedf593acda80b00eb7da32f688c4843 (patch) | |
tree | 48867f314c028cdf4ddb17d72ef964f5f5954b89 /lib/e2ee/qolmutils.h | |
parent | bcc05aa1d52cae2b6d8e70bb6cf04fa49904687a (diff) | |
download | libquotient-bc1ded73bedf593acda80b00eb7da32f688c4843.tar.gz libquotient-bc1ded73bedf593acda80b00eb7da32f688c4843.zip |
RandomBuffer
A convenient abstraction swallowing all the type casts and, more
importantly, cleanup on destruction (previous code only cleaned up
the buffer upon a successful call to Olm API but not upon an error).
Diffstat (limited to 'lib/e2ee/qolmutils.h')
-rw-r--r-- | lib/e2ee/qolmutils.h | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/lib/e2ee/qolmutils.h b/lib/e2ee/qolmutils.h index 7a8511c3..da9d2d18 100644 --- a/lib/e2ee/qolmutils.h +++ b/lib/e2ee/qolmutils.h @@ -12,5 +12,26 @@ namespace Quotient { // Convert PicklingMode to key QUOTIENT_API QByteArray toKey(const PicklingMode &mode); -QUOTIENT_API QByteArray getRandom(size_t bufferSize); + +class QUOTIENT_API RandomBuffer : public QByteArray { +public: + explicit RandomBuffer(size_t size); + ~RandomBuffer() { clear(); } + + // NOLINTNEXTLINE(google-explicit-constructor) + QUO_IMPLICIT operator void*() { return data(); } + char* chars() { return data(); } + uint8_t* bytes() { return reinterpret_cast<uint8_t*>(data()); } + + Q_DISABLE_COPY(RandomBuffer) + RandomBuffer(RandomBuffer&&) = default; + void operator=(RandomBuffer&&) = delete; +}; + +[[deprecated("Create RandomBuffer directly")]] inline auto getRandom( + size_t bufferSize) +{ + return RandomBuffer(bufferSize); } + +} // namespace Quotient |