Skip to content

Commit ed355f0

Browse files
committed
Change std::bind calls to lambdas
1 parent 71227af commit ed355f0

File tree

4 files changed

+87
-75
lines changed

4 files changed

+87
-75
lines changed

boost/network/protocol/http/policies/async_connection.hpp

+26-24
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,30 @@ struct async_connection_policy : resolver_policy<Tag>::type {
2828
typedef typename resolver_policy<Tag>::type resolver_base;
2929
typedef typename resolver_base::resolver_type resolver_type;
3030
typedef typename resolver_base::resolve_function resolve_function;
31+
typedef typename resolver_base::resolve_completion_function
32+
resolve_completion_function;
3133
typedef std::function<void(iterator_range<char const*> const&,
32-
std::error_code const&)> body_callback_function_type;
34+
std::error_code const&)>
35+
body_callback_function_type;
3336
typedef std::function<bool(string_type&)> body_generator_function_type;
3437

3538
struct connection_impl {
36-
connection_impl(bool follow_redirect, bool always_verify_peer,
37-
resolve_function resolve, resolver_type& resolver,
38-
bool https, int timeout,
39-
optional<string_type> /*unused*/const& certificate_filename,
40-
optional<string_type> const& verify_path,
41-
optional<string_type> const& certificate_file,
42-
optional<string_type> const& private_key_file,
43-
optional<string_type> const& ciphers, long ssl_options) {
44-
pimpl = impl::async_connection_base<
45-
Tag, version_major,
46-
version_minor>::new_connection(resolve, resolver, follow_redirect,
47-
always_verify_peer, https, timeout,
48-
certificate_filename, verify_path,
49-
certificate_file, private_key_file,
50-
ciphers, ssl_options);
39+
connection_impl(
40+
bool follow_redirect, bool always_verify_peer, resolve_function resolve,
41+
resolver_type& resolver, bool https, int timeout,
42+
optional<string_type> /*unused*/ const& certificate_filename,
43+
optional<string_type> const& verify_path,
44+
optional<string_type> const& certificate_file,
45+
optional<string_type> const& private_key_file,
46+
optional<string_type> const& ciphers, long ssl_options) {
47+
pimpl = impl::async_connection_base<Tag, version_major, version_minor>::
48+
new_connection(resolve, resolver, follow_redirect, always_verify_peer,
49+
https, timeout, certificate_filename, verify_path,
50+
certificate_file, private_key_file, ciphers,
51+
ssl_options);
5152
}
5253

53-
basic_response<Tag> send_request(string_type /*unused*/const& method,
54+
basic_response<Tag> send_request(string_type /*unused*/ const& method,
5455
basic_request<Tag> const& request_,
5556
bool get_body,
5657
body_callback_function_type callback,
@@ -60,14 +61,14 @@ struct async_connection_policy : resolver_policy<Tag>::type {
6061

6162
private:
6263
std::shared_ptr<http::impl::async_connection_base<Tag, version_major,
63-
version_minor> > pimpl;
64+
version_minor> > pimpl;
6465
};
6566

6667
typedef std::shared_ptr<connection_impl> connection_ptr;
6768
connection_ptr get_connection(
6869
resolver_type& resolver, basic_request<Tag> const& request_,
6970
bool always_verify_peer,
70-
optional<string_type> /*unused*/const& certificate_filename =
71+
optional<string_type> const& certificate_filename =
7172
optional<string_type>(),
7273
optional<string_type> const& verify_path = optional<string_type>(),
7374
optional<string_type> const& certificate_file = optional<string_type>(),
@@ -76,14 +77,15 @@ struct async_connection_policy : resolver_policy<Tag>::type {
7677
long ssl_options = 0) {
7778
string_type protocol_ = protocol(request_);
7879
namespace ph = std::placeholders;
79-
connection_ptr connection_(new connection_impl(
80+
return std::make_shared<connection_impl>(
8081
follow_redirect_, always_verify_peer,
81-
std::bind(&async_connection_policy<Tag, version_major,
82-
version_minor>::resolve, this, ph::_1, ph::_2, ph::_3, ph::_4),
82+
[this](resolver_type& resolver, string_type const& host,
83+
std::uint16_t port, resolve_completion_function once_resolved) {
84+
this->resolve(resolver, host, port, once_resolved);
85+
},
8386
resolver, boost::iequals(protocol_, string_type("https")), timeout_,
8487
certificate_filename, verify_path, certificate_file, private_key_file,
85-
ciphers, ssl_options));
86-
return connection_;
88+
ciphers, ssl_options);
8789
}
8890

8991
void cleanup() {}

boost/network/protocol/http/policies/async_resolver.hpp

+12-16
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
3131
typedef typename string<Tag>::type string_type;
3232
typedef std::unordered_map<string_type, resolver_iterator_pair>
3333
endpoint_cache;
34-
typedef std::function<
35-
void(std::error_code const &, resolver_iterator_pair)>
34+
typedef std::function<void(std::error_code const &, resolver_iterator_pair)>
3635
resolve_completion_function;
3736
typedef std::function<void(resolver_type &, string_type, std::uint16_t,
3837
resolve_completion_function)> resolve_function;
@@ -47,8 +46,7 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
4746
: cache_resolved_(cache_resolved), endpoint_cache_() {}
4847

4948
void resolve(resolver_type &resolver_, string_type const &host,
50-
std::uint16_t port,
51-
resolve_completion_function once_resolved) {
49+
std::uint16_t port, resolve_completion_function once_resolved) {
5250
if (cache_resolved_) {
5351
typename endpoint_cache::iterator iter =
5452
endpoint_cache_.find(boost::to_lower_copy(host));
@@ -59,27 +57,25 @@ struct async_resolver : std::enable_shared_from_this<async_resolver<Tag> > {
5957
}
6058
}
6159

62-
typename resolver_type::query q(host,
63-
std::to_string(port));
60+
typename resolver_type::query q(host, std::to_string(port));
6461
auto self = this->shared_from_this();
65-
resolver_.async_resolve(q, resolver_strand_->wrap([=] (std::error_code const &ec,
66-
resolver_iterator endpoint_iterator) {
67-
self->handle_resolve(boost::to_lower_copy(host),
68-
once_resolved,
69-
ec, endpoint_iterator);
70-
}));
62+
resolver_.async_resolve(
63+
q, resolver_strand_->wrap([=](std::error_code const &ec,
64+
resolver_iterator endpoint_iterator) {
65+
self->handle_resolve(boost::to_lower_copy(host), once_resolved, ec,
66+
endpoint_iterator);
67+
}));
7168
}
7269

73-
void handle_resolve(string_type /*unused*/const &host,
70+
void handle_resolve(string_type /*unused*/ const &host,
7471
resolve_completion_function once_resolved,
7572
std::error_code const &ec,
7673
resolver_iterator endpoint_iterator) {
7774
typename endpoint_cache::iterator iter;
7875
bool inserted = false;
7976
if (!ec && cache_resolved_) {
80-
std::tie(iter, inserted) =
81-
endpoint_cache_.insert(std::make_pair(
82-
host, std::make_pair(endpoint_iterator, resolver_iterator())));
77+
std::tie(iter, inserted) = endpoint_cache_.insert(std::make_pair(
78+
host, std::make_pair(endpoint_iterator, resolver_iterator())));
8379
once_resolved(ec, iter->second);
8480
} else {
8581
once_resolved(ec, std::make_pair(endpoint_iterator, resolver_iterator()));

boost/network/protocol/http/policies/pooled_connection.hpp

+28-16
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
3535
resolver_type&, string_type const&, string_type const&)>
3636
resolver_function_type;
3737
typedef std::function<void(iterator_range<char const*> const&,
38-
std::error_code const&)> body_callback_function_type;
38+
std::error_code const&)>
39+
body_callback_function_type;
3940
typedef std::function<bool(string_type&)> body_generator_function_type;
4041

4142
void cleanup() { host_connection_map().swap(host_connections_); }
@@ -103,8 +104,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
103104
basic_response<Tag> response_;
104105
// check if the socket is open first
105106
if (!pimpl->is_open()) {
106-
pimpl->init_socket(request_.host(),
107-
std::to_string(request_.port()));
107+
pimpl->init_socket(request_.host(), std::to_string(request_.port()));
108108
}
109109
response_ = basic_response<Tag>();
110110
response_ << ::boost::network::source(request_.host());
@@ -132,7 +132,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
132132
pimpl->read_body(response_, response_buffer);
133133
}
134134

135-
typename headers_range<basic_response<Tag> >::type connection_range =
135+
typename headers_range<basic_response<Tag>>::type connection_range =
136136
headers(response_)["Connection"];
137137
if (version_major == 1 && version_minor == 1 &&
138138
!boost::empty(connection_range) &&
@@ -145,10 +145,10 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
145145
if (connection_follow_redirect_) {
146146
std::uint16_t status = response_.status();
147147
if (status >= 300 && status <= 307) {
148-
typename headers_range<basic_response<Tag> >::type location_range =
148+
typename headers_range<basic_response<Tag>>::type location_range =
149149
headers(response_)["Location"];
150150
typename range_iterator<
151-
typename headers_range<basic_request<Tag> >::type>::type
151+
typename headers_range<basic_request<Tag>>::type>::type
152152
location_header = std::begin(location_range);
153153
if (location_header != std::end(location_range)) {
154154
request_.uri(location_header->second);
@@ -168,7 +168,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
168168
}
169169

170170
std::shared_ptr<http::impl::sync_connection_base<Tag, version_major,
171-
version_minor> > pimpl;
171+
version_minor>> pimpl;
172172
resolver_type& resolver_;
173173
bool connection_follow_redirect_;
174174
get_connection_function get_connection_;
@@ -182,7 +182,8 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
182182

183183
typedef std::shared_ptr<connection_impl> connection_ptr;
184184

185-
typedef std::unordered_map<string_type, std::weak_ptr<connection_impl>> host_connection_map;
185+
typedef std::unordered_map<string_type, std::weak_ptr<connection_impl>>
186+
host_connection_map;
186187
std::mutex host_mutex_;
187188
host_connection_map host_connections_;
188189
bool follow_redirect_;
@@ -198,7 +199,7 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
198199
optional<string_type> const& private_key_file = optional<string_type>(),
199200
optional<string_type> const& ciphers = optional<string_type>()) {
200201
string_type index =
201-
(request_.host() + ':') + std::to_string(request_.port());
202+
(request_.host() + ':') + std::to_string(request_.port());
202203
std::unique_lock<std::mutex> lock(host_mutex_);
203204
auto it = host_connections_.find(index);
204205
if (it != host_connections_.end()) {
@@ -209,19 +210,30 @@ struct pooled_connection_policy : resolver_policy<Tag>::type {
209210
}
210211

211212
namespace ph = std::placeholders;
212-
connection_ptr connection(new connection_impl(
213+
auto connection = std::make_shared<connection_impl>(
213214
resolver, follow_redirect_, request_.host(),
214215
std::to_string(request_.port()),
215216
// resolver function
216-
std::bind(&pooled_connection_policy<Tag, version_major,
217-
version_minor>::resolve, this, ph::_1, ph::_2, ph::_3),
217+
[this](
218+
resolver_type& resolver, string_type const& host,
219+
std::uint16_t port,
220+
typename resolver_type::resolve_completion_function once_resolved) {
221+
this->resolve(resolver, host, port, once_resolved);
222+
},
218223
// connection factory
219-
std::bind(&pooled_connection_policy<Tag, version_major,
220-
version_minor>::get_connection,
221-
this, ph::_1, ph::_2, always_verify_peer, ph::_3, ph::_4, ph::_5, ph::_6, ph::_7),
224+
[this, always_verify_peer](
225+
resolver_type& resolver, basic_request<Tag> const& request, bool,
226+
optional<string_type> const& certificate_filename,
227+
optional<string_type> const& verify_path,
228+
optional<string_type> const& private_key_file,
229+
optional<string_type> const& ciphers) {
230+
return this->get_connection(resolver, request, always_verify_peer,
231+
certificate_filename, verify_path,
232+
private_key_file, ciphers);
233+
},
222234
boost::iequals(request_.protocol(), string_type("https")),
223235
always_verify_peer, timeout_, certificate_filename, verify_path,
224-
certificate_file, private_key_file, ciphers, 0));
236+
certificate_file, private_key_file, ciphers, 0);
225237
host_connections_.insert(std::make_pair(index, connection));
226238
return connection;
227239
}

boost/network/protocol/http/policies/simple_connection.hpp

+21-19
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
3333
typedef std::function<typename resolver_base::resolver_iterator_pair(
3434
resolver_type&, string_type const&, string_type const&)>
3535
resolver_function_type;
36+
typedef typename resolver_base::resolver_completion_function
37+
resolver_completion_function;
3638
typedef std::function<void(iterator_range<char const*> const&,
37-
std::error_code const&)> body_callback_function_type;
39+
std::error_code const&)>
40+
body_callback_function_type;
3841
typedef std::function<bool(string_type&)> body_generator_function_type;
3942

4043
struct connection_impl {
4144
connection_impl(
4245
resolver_type& resolver, bool follow_redirect, bool always_verify_peer,
43-
string_type /*unused*/const& hostname, string_type const& port,
46+
string_type /*unused*/ const& hostname, string_type const& port,
4447
resolver_function_type resolve, bool https, int timeout,
4548
optional<string_type> const& certificate_filename =
4649
optional<string_type>(),
@@ -54,16 +57,15 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
5457
(void)hostname;
5558
(void)port;
5659

57-
pimpl.reset(impl::sync_connection_base<
58-
Tag, version_major,
59-
version_minor>::new_connection(resolver, resolve, https,
60-
always_verify_peer, timeout,
61-
certificate_filename, verify_path,
62-
certificate_file, private_key_file,
63-
ciphers, ssl_options));
60+
pimpl.reset(
61+
impl::sync_connection_base<Tag, version_major, version_minor>::
62+
new_connection(resolver, resolve, https, always_verify_peer,
63+
timeout, certificate_filename, verify_path,
64+
certificate_file, private_key_file, ciphers,
65+
ssl_options));
6466
}
6567

66-
basic_response<Tag> send_request(string_type /*unused*/const& method,
68+
basic_response<Tag> send_request(string_type /*unused*/ const& method,
6769
basic_request<Tag> request_, bool get_body,
6870
body_callback_function_type callback,
6971
body_generator_function_type generator) {
@@ -72,8 +74,7 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
7274

7375
basic_response<Tag> response_;
7476
do {
75-
pimpl->init_socket(request_.host(),
76-
std::to_string(request_.port()));
77+
pimpl->init_socket(request_.host(), std::to_string(request_.port()));
7778
pimpl->send_request_impl(method, request_, generator);
7879

7980
response_ = basic_response<Tag>();
@@ -99,25 +100,25 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
99100
"Location header not defined in redirect response.");
100101
} else {
101102
break;
102-
}
103+
}
103104
} else {
104105
break;
105-
}
106+
}
106107
} while (true);
107108
return response_;
108109
}
109110

110111
private:
111112
std::shared_ptr<http::impl::sync_connection_base<Tag, version_major,
112-
version_minor> > pimpl;
113+
version_minor> > pimpl;
113114
bool follow_redirect_;
114115
};
115116

116117
typedef std::shared_ptr<connection_impl> connection_ptr;
117118
connection_ptr get_connection(
118119
resolver_type& resolver, basic_request<Tag> const& request_,
119120
bool always_verify_peer,
120-
optional<string_type> /*unused*/const& certificate_filename =
121+
optional<string_type> /*unused*/ const& certificate_filename =
121122
optional<string_type>(),
122123
optional<string_type> const& verify_path = optional<string_type>(),
123124
optional<string_type> const& certificate_file = optional<string_type>(),
@@ -128,9 +129,10 @@ struct simple_connection_policy : resolver_policy<Tag>::type {
128129
connection_ptr connection_(new connection_impl(
129130
resolver, follow_redirect_, always_verify_peer, request_.host(),
130131
std::to_string(request_.port()),
131-
std::bind(&simple_connection_policy<Tag, version_major,
132-
version_minor>::resolve,
133-
this, ph::_1, ph::_2, ph::_3),
132+
[this](resolver_type& resolver, string_type const& host,
133+
std::uint16_t port, resolve_completion_function once_resolved) {
134+
this->resolve(resolver, host, port, once_resolved);
135+
},
134136
boost::iequals(request_.protocol(), string_type("https")), timeout_,
135137
certificate_filename, verify_path, certificate_file, private_key_file,
136138
ciphers, ssl_options));

0 commit comments

Comments
 (0)