Is the safe exception of the example above?
H ** l no!
It is exactly the same as you say. The code you see as a leak, as a sieve after exceptions.
RAII or pointers should be used std::unique.
Even for pre-standard C ++ code, there was a good ole std::auto_ptr.
, .
, Open Source - "", - "", , .
.
, ( MySQL):
, , main() , .
. , "CTRL-C CTRL-V codez ", , .
Conclusio:
, , , .
.
, .
, :
(. ) -:
class MySQLQuery;
class MySQLConnection {
std::function<void(void*)> driverCleanup_;
std::function<void>(void*) connectionCleanup_;
std::unique_ptr<sql::Driver> driver_;
std::shared_ptr<sql::Connection> con_;
public:
MySQLConnection
( const std::string& ipAddress
, const std::string& user
, const std::string& password
) :
driverCleanup_([](void *driver) {
delete driver;
}) ,
connectionCleanup_([](void *connection) {
delete connection;
}),
driver_(get_driver_instance(),driverCleanup_),
con_(std::make_shared(driver_->connect(ipAddress,user,password)
,connectionCleanup_))
{}
std::unique_ptr<MySQLQuery> CreateQuery(const std::string& query);
};
class MySQLQuery {
friend class MySQLConnection;
std::shared_ptr<sql::Connection> con_;
std::unique_ptr<sql::Statement stmt_;
MySQLQuery
( std::shared_ptr<sql::Connection> connection
, const std::string& query
) : con_(connection)
, stmt_(con_->createStatement()
{
}
public:
MySQLResultSet Execute() {
return stmt_->executeQuery("SELECT 'Hello World!' AS _message");
}
};
std::unique_ptr<MySQLQuery> MySQLConnection::CreateQuery(const std::string& query)
{
return std::make_unique<MySQLQuery>(con_,query);
}
, . * ( ) . .
, , .
user9212993