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); nghttp2_session_callbacks_new(&callbacks);
util::auto_delete<nghttp2_session_callbacks*> callbacks_deleter auto callbacks_deleter =
(callbacks, nghttp2_session_callbacks_del); util::defer(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_frame_recv_callback nghttp2_session_callbacks_set_on_frame_recv_callback
(callbacks, 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 *callbacks;
nghttp2_session_callbacks_new(&callbacks); nghttp2_session_callbacks_new(&callbacks);
util::auto_delete<nghttp2_session_callbacks*> cbsdel auto cbsdel = util::defer(callbacks, nghttp2_session_callbacks_del);
(callbacks, nghttp2_session_callbacks_del);
nghttp2_session_callbacks_set_on_stream_close_callback nghttp2_session_callbacks_set_on_stream_close_callback
(callbacks, on_stream_close_callback); (callbacks, on_stream_close_callback);

View File

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

View File

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

View File

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

View File

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