5#ifndef OMEMOLIBWRAPPERS_H
6#define OMEMOLIBWRAPPERS_H
9#include <session_builder.h>
10#include <session_cipher.h>
11#include <signal_protocol.h>
16template<
typename T,
void(destruct)(T *)>
22 OmemoLibPtr(T *ptr =
nullptr) : m_ptr(ptr) { }
23 OmemoLibPtr(
const OmemoLibPtr &) =
delete;
30 OmemoLibPtr &operator=(
const OmemoLibPtr &) =
delete;
31 OmemoLibPtr<T, destruct> &operator=(T *ptr)
36 operator bool()
const {
return m_ptr !=
nullptr; }
37 T *operator->()
const {
return m_ptr; }
38 T *get()
const {
return m_ptr; }
39 T **ptrRef() {
return &m_ptr; }
50void omemoLibUnrefHelper(T *ptr)
56using RefCountedPtr = OmemoLibPtr<T, omemoLibUnrefHelper<T>>;
58static QByteArray omemoLibBufferToByteArray(signal_buffer *buffer)
60 return QByteArray(
reinterpret_cast<const char *
>(signal_buffer_data(buffer)), signal_buffer_len(buffer));
63static signal_buffer *omemoLibBufferFromByteArray(
const QByteArray &bytes)
65 return signal_buffer_create(
reinterpret_cast<const unsigned char *
>(bytes.constData()), bytes.size());
68template<
void(destruct)(signal_buffer *)>
69class BufferPtrBase :
public OmemoLibPtr<signal_buffer, destruct>
72 BufferPtrBase(signal_buffer *ptr)
73 : OmemoLibPtr<signal_buffer, destruct>(ptr)
77 QByteArray toByteArray()
const
79 return omemoLibBufferToByteArray(this->get());
83class BufferSecurePtr :
public BufferPtrBase<signal_buffer_bzero_free>
86 BufferSecurePtr() : BufferPtrBase<signal_buffer_bzero_free>(nullptr) { }
87 BufferSecurePtr(signal_buffer *ptr)
88 : BufferPtrBase<signal_buffer_bzero_free>(ptr)
92 static BufferSecurePtr fromByteArray(
const QByteArray &bytes)
94 return { omemoLibBufferFromByteArray(bytes) };
98class BufferPtr :
public BufferPtrBase<signal_buffer_free>
101 BufferPtr() : BufferPtrBase<signal_buffer_free>(nullptr) { }
102 BufferPtr(signal_buffer *ptr)
103 : BufferPtrBase<signal_buffer_free>(ptr)
107 static BufferPtr fromByteArray(
const QByteArray &bytes)
109 return { omemoLibBufferFromByteArray(bytes) };
113using KeyListNodePtr = OmemoLibPtr<signal_protocol_key_helper_pre_key_list_node, signal_protocol_key_helper_key_list_free>;
114using SessionCipherPtr = OmemoLibPtr<session_cipher, session_cipher_free>;
115using SessionBuilderPtr = OmemoLibPtr<session_builder, session_builder_free>;
116using OmemoContextPtr = OmemoLibPtr<signal_context, signal_context_destroy>;
117using StoreContextPtr = OmemoLibPtr<signal_protocol_store_context, signal_protocol_store_context_destroy>;