#pragma once #include #include /// Generic owner for a notmuch handle with a destroy function. template class NmHandle { public: NmHandle() = default; explicit NmHandle(T *handle) : m_handle(handle) {} ~NmHandle() { reset(); } NmHandle(const NmHandle &) = delete; NmHandle &operator=(const NmHandle &) = delete; NmHandle(NmHandle &&other) noexcept : m_handle(std::exchange(other.m_handle, nullptr)) {} NmHandle &operator=(NmHandle &&other) noexcept { if (this != &other) { reset(); m_handle = std::exchange(other.m_handle, nullptr); } return *this; } void reset(T *handle = nullptr) { if (m_handle) Destroy(m_handle); m_handle = handle; } T *get() const { return m_handle; } explicit operator bool() const { return m_handle != nullptr; } private: T *m_handle = nullptr; }; using NmQuery = NmHandle; using NmThreads = NmHandle; using NmMessages = NmHandle; using NmThread = NmHandle; using NmMessage = NmHandle; using NmTags = NmHandle;