I am converting my code to use unique_ptr instead of just pointers. I am trying to create a unique_ptr to a sqlite3_statement that automatically calls the function sqlite3_finalize(sqlite3_stmt *pStmt) in its custom deleter. How can I get this to work in my header file? I am completely at loss here, any help is welcome. Thanks.
With a lot of help from igleyy ( his solution gave me memory access errors when the deleter was called for some reason) I came up with this solution that seems to work and looks elegant;
Datasource.h
#pragma once
#include <sqlite\sqlite3.h>
#include <string>
#include <vector>
#include <memory>
class Datasource
{
void statementDeleter(sqlite3_stmt* object);
std::unique_ptr<sqlite3_stmt, std::function<void(sqlite3_stmt*)>> statement;
Datasource(Datasource const &);
Datasource &operator=(Datasource const &);
public:
Datasource(void);
~Datasource(void);
};
Datasource.cpp
#include "Datasource.h"
Datasource::Datasource(void) :
statement(nullptr,std::bind(&Datasource::statementDeleter,this,std::placeholders::_1))
{
}
void Datasource::statementDeleter(sqlite3_stmt * s)
{
sqlite3_finalize(s);
}
Datasource::~Datasource(void)
{
}