Pass a unique_ptr parameter with custom deleter to dbo Session's setConnection method
Added by Giovanni Ferranti 24 days ago
Hi,
Is it possible in your opinion to pass a unique_ptr parameter with custom deleter to dbo Session's setConnection method? If so, how do I properly convert the parameter before passing it to the function? I want to call my custom deleter instead of the default when I go out of scope.
Thank you in advance,
Giovanni
Replies (1)
RE: Pass a unique_ptr parameter with custom deleter to dbo Session's setConnection method - Added by Matthias Van Ceulebroeck 24 days ago
Hello Giovanni,
as per the support ticket:
it is not possible to add a custom Deleter
to the unique_ptr
, since that would be regarded as a different type to the default deleter. When calling setConnection
, the conversion from your custom Deleter to the std::default_delete<Wt::Dbo::SqlConnection>>
would fail.
Now, what IS possible, is to create a wrapper around your connection object. A small example would look like this:
class ConnectionWrapper : public Wt::Dbo::backend::Sqlite3
{
public:
ConnectionWrapper(const std::string& location)
: Wt::Dbo::backend::Sqlite3(location)
{
}
~ConnectionWrapper()
{
Wt::log("==========") << "DO DESTRUCTION";
}
};
It can be created like any other backend, and then set as the session's connection:
ConnectionWrapper *sqlite3 = new ConnectionWrapper(appRoot() + "database.db");
auto connection = std::unique_ptr<Wt::Dbo::SqlConnection>(sqlite3);
session_.setConnection(std::move(connection));
Note that I took Sqlite3 as the example here, but it ought to work for any backend.