Replace auto_delete* with defer

This commit is contained in:
Tatsuhiro Tsujikawa 2014-09-16 23:39:38 +09:00
parent 6fc12caa6d
commit e4751a798a
6 changed files with 26 additions and 48 deletions

View File

@ -106,8 +106,8 @@ void Http2Session::on_connect()
nghttp2_session_callbacks_new(&callbacks);
util::auto_delete<nghttp2_session_callbacks*> callbacks_deleter
(callbacks, nghttp2_session_callbacks_del);
auto callbacks_deleter =
util::defer(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_frame_recv_callback
(callbacks, on_frame_recv_callback);

View File

@ -1832,8 +1832,7 @@ int run(char **uris, int n)
nghttp2_session_callbacks *callbacks;
nghttp2_session_callbacks_new(&callbacks);
util::auto_delete<nghttp2_session_callbacks*> cbsdel
(callbacks, nghttp2_session_callbacks_del);
auto cbsdel = util::defer(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback);

View File

@ -1377,8 +1377,8 @@ int Http2Session::on_connect()
return -1;
}
util::auto_delete<nghttp2_session_callbacks*> callbacks_deleter
(callbacks, nghttp2_session_callbacks_del);
auto callbacks_deleter =
util::defer(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback);

View File

@ -637,8 +637,8 @@ Http2Upstream::Http2Upstream(ClientHandler *handler)
assert(rv == 0);
util::auto_delete<nghttp2_session_callbacks*> callbacks_deleter
(callbacks, nghttp2_session_callbacks_del);
auto callbacks_deleter =
util::defer(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback);

View File

@ -602,8 +602,7 @@ void get_altnames(X509 *cert,
static_cast<GENERAL_NAMES*>
(X509_get_ext_d2i(cert, NID_subject_alt_name, nullptr, nullptr));
if(altnames) {
util::auto_delete<GENERAL_NAMES*> altnames_deleter(altnames,
GENERAL_NAMES_free);
auto altnames_deleter = util::defer(altnames, GENERAL_NAMES_free);
size_t n = sk_GENERAL_NAME_num(altnames);
for(size_t i = 0; i < n; ++i) {
const GENERAL_NAME *altname = sk_GENERAL_NAME_value(altnames, i);
@ -665,7 +664,7 @@ int check_cert(SSL *ssl)
LOG(ERROR) << "No certificate found";
return -1;
}
util::auto_delete<X509*> cert_deleter(cert, X509_free);
auto cert_deleter = util::defer(cert, X509_free);
long verify_res = SSL_get_verify_result(ssl);
if(verify_res != X509_V_OK) {
LOG(ERROR) << "Certificate verification failed: "
@ -864,7 +863,7 @@ int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx,
LOG(ERROR) << "BIO_new failed";
return -1;
}
util::auto_delete<BIO*> bio_deleter(bio, BIO_vfree);
auto bio_deleter = util::defer(bio, BIO_vfree);
if(!BIO_read_filename(bio, certfile)) {
LOG(ERROR) << "Could not read certificate file '" << certfile << "'";
return -1;
@ -875,7 +874,7 @@ int cert_lookup_tree_add_cert_from_file(CertLookupTree *lt, SSL_CTX *ssl_ctx,
<< certfile << "'";
return -1;
}
util::auto_delete<X509*> cert_deleter(cert, X509_free);
auto cert_deleter = util::defer(cert, X509_free);
std::string common_name;
std::vector<std::string> dns_names;
std::vector<std::string> ip_addrs;

View File

@ -51,46 +51,26 @@ constexpr size_t array_size(T (&)[N])
return N;
}
template<typename T>
class auto_delete {
private:
T obj_;
void (*deleter_)(T);
public:
auto_delete(T obj, void (*deleter)(T)):obj_(obj), deleter_(deleter) {}
template<typename T, typename F>
struct Defer {
Defer(T t, F f)
: t(t), f(std::move(f))
{}
~auto_delete()
~Defer()
{
deleter_(obj_);
f(t);
}
T t;
F f;
};
template<typename T>
class auto_delete_d {
private:
T obj_;
public:
auto_delete_d(T obj):obj_(obj) {}
~auto_delete_d()
{
delete obj_;
}
};
template<typename T, typename R>
class auto_delete_r {
private:
T obj_;
R (*deleter_)(T);
public:
auto_delete_r(T obj, R (*deleter)(T)):obj_(obj), deleter_(deleter) {}
~auto_delete_r()
{
(void)deleter_(obj_);
}
};
template<typename T, typename F>
Defer<T, F> defer(T&& t, F f)
{
return Defer<T, F>(std::forward<T>(t), std::forward<F>(f));
}
extern const char DEFAULT_STRIP_CHARSET[];