Skip to content

Commit 3c6c0aa

Browse files
committed
Using Boost.Parameter for the client constructors instead of the adhoc method used earlier.
1 parent 4169f81 commit 3c6c0aa

File tree

5 files changed

+114
-68
lines changed

5 files changed

+114
-68
lines changed

boost/network/protocol/http/client.hpp

Lines changed: 24 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,81 +24,48 @@
2424
#include <map>
2525

2626
#include <boost/network/protocol/http/client/facade.hpp>
27-
#include <boost/network/protocol/http/client/pimpl.hpp>
27+
#include <boost/network/protocol/http/client/parameters.hpp>
2828

2929
namespace boost { namespace network { namespace http {
3030

3131
template <class Tag, unsigned version_major, unsigned version_minor>
3232
struct basic_client
33-
: basic_client_facade<Tag, basic_client<Tag,version_major,version_minor> >
33+
: basic_client_facade<Tag, version_major, version_minor>
3434
{
3535
private:
36-
typedef basic_client_impl<Tag,version_major,version_minor> pimpl_type;
37-
typedef basic_client_facade<Tag, basic_client<Tag,version_major,version_minor> > base_facade_type;
36+
typedef basic_client_facade<Tag, version_major, version_minor>
37+
base_facade_type;
3838
public:
3939
typedef basic_request<Tag> request;
4040
typedef basic_response<Tag> response;
4141
typedef typename string<Tag>::type string_type;
4242
typedef Tag tag_type;
4343

44-
struct cache_resolved_type { };
45-
46-
static cache_resolved_type cache_resolved() {
47-
return cache_resolved_type();
48-
};
49-
50-
struct follow_redirect_type { };
51-
52-
static follow_redirect_type follow_redirects() {
53-
return follow_redirect_type();
54-
};
55-
56-
static follow_redirect_type follow_redirect() {
57-
return follow_redirect_type();
58-
};
59-
60-
// Constructors
44+
// Constructor
6145
// =================================================================
62-
basic_client()
63-
: base_facade_type(), pimpl(new pimpl_type(false, false))
64-
{}
65-
66-
explicit basic_client(cache_resolved_type (*)())
67-
: base_facade_type(), pimpl(new pimpl_type(true, false))
68-
{}
69-
70-
explicit basic_client(follow_redirect_type (*)())
71-
: base_facade_type(), pimpl(new pimpl_type(false, true))
72-
{}
73-
74-
basic_client(cache_resolved_type (*)(), follow_redirect_type (*)())
75-
: base_facade_type(), pimpl(new pimpl_type(true, true))
76-
{}
77-
78-
explicit basic_client(boost::asio::io_service & io_service)
79-
: base_facade_type(), pimpl(new pimpl_type(false, false, io_service))
80-
{}
46+
// This is a Boost.Parameter-based constructor forwarder, whose
47+
// implementation is actually forwarded to the base type.
48+
//
49+
// The supported parameters are:
50+
// _follow_redirects : bool -- whether to follow HTTP redirect
51+
// responses (default: false)
52+
// _cache_resolved : bool -- whether to cache the resolved
53+
// endpoints (default: false)
54+
// _io_service : boost::asio::io_service &
55+
// -- supply an io_service to the
56+
// client
57+
58+
BOOST_PARAMETER_CONSTRUCTOR(
59+
basic_client, (base_facade_type), tag,
60+
(optional
61+
(in_out(io_service), (boost::asio::io_service))
62+
(follow_redirects, (bool))
63+
(cache_resolved, (bool))
64+
))
8165

8266
//
8367
// =================================================================
8468

85-
~basic_client()
86-
{}
87-
88-
private:
89-
90-
boost::shared_ptr<pimpl_type> pimpl;
91-
92-
friend struct basic_client_facade<Tag,basic_client<Tag,version_major,version_minor> > ;
93-
94-
basic_response<Tag> const request_skeleton(request const & request_, string_type method, bool get_body) {
95-
return pimpl->request_skeleton(request_, method, get_body);
96-
}
97-
98-
void clear_resolved_cache() {
99-
pimpl->clear_resolved_cache();
100-
}
101-
10269
};
10370

10471
typedef basic_client<tags::http_default_8bit_udp_resolve, 1, 0> client;

boost/network/protocol/http/client/facade.hpp

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
#include <boost/network/protocol/http/request.hpp>
1010
#include <boost/network/protocol/http/response.hpp>
11+
#include <boost/network/protocol/http/client/pimpl.hpp>
12+
#include <boost/network/protocol/http/client/parameters.hpp>
1113

1214
namespace boost { namespace network { namespace http {
1315

@@ -17,26 +19,39 @@ namespace boost { namespace network { namespace http {
1719
template <class Tag>
1820
struct basic_response;
1921

20-
template <class Tag, class Derived>
22+
template <class Tag, unsigned version_major, unsigned version_minor>
2123
struct basic_client_facade {
2224

2325
typedef typename string<Tag>::type string_type;
2426
typedef basic_request<Tag> request;
2527
typedef basic_response<Tag> response;
26-
27-
basic_client_facade()
28-
{}
28+
typedef basic_client_impl<Tag,version_major,version_minor> pimpl_type;
29+
30+
template <class ArgPack>
31+
basic_client_facade(ArgPack const & args)
32+
{
33+
init_pimpl(args,
34+
typename mpl::if_<
35+
is_same<
36+
typename parameter::value_type<ArgPack, tag::io_service, void>::type,
37+
void
38+
>,
39+
no_io_service,
40+
has_io_service
41+
>::type());
42+
43+
}
2944

3045
response const head (request const & request_) {
31-
return static_cast<Derived*>(this)->request_skeleton(request_, "HEAD", false);
46+
return pimpl->request_skeleton(request_, "HEAD", false);
3247
}
3348

3449
response const get (request const & request_) {
35-
return static_cast<Derived*>(this)->request_skeleton(request_, "GET", true);
50+
return pimpl->request_skeleton(request_, "GET", true);
3651
}
3752

3853
response const post (request const & request_) {
39-
return static_cast<Derived*>(this)->request_skeleton(request_, "POST", true);
54+
return pimpl->request_skeleton(request_, "POST", true);
4055
}
4156

4257
response const post (request request_, string_type const & content_type, string_type const & body_) {
@@ -51,7 +66,7 @@ namespace boost { namespace network { namespace http {
5166
}
5267

5368
response const put (request const & request_) {
54-
return static_cast<Derived*>(this)->request_skeleton(request_, "PUT", true);
69+
return pimpl->request_skeleton(request_, "PUT", true);
5570
}
5671

5772
response const put (request const & request_, string_type const & body_) {
@@ -66,11 +81,39 @@ namespace boost { namespace network { namespace http {
6681
}
6782

6883
response const delete_ (request const & request_) {
69-
return static_cast<Derived*>(this)->request_skeleton(request_, "DELETE", true);
84+
return pimpl->request_skeleton(request_, "DELETE", true);
7085
}
7186

7287
void clear_resolved_cache() {
73-
static_cast<Derived*>(this)->clear_resolved_cache();
88+
pimpl->clear_resolved_cache();
89+
}
90+
91+
protected:
92+
93+
struct no_io_service {};
94+
struct has_io_service {};
95+
96+
boost::shared_ptr<pimpl_type> pimpl;
97+
98+
template <class ArgPack>
99+
void init_pimpl(ArgPack const & args, no_io_service) {
100+
pimpl.reset(
101+
new pimpl_type(
102+
args[_cache_resolved|false]
103+
, args[_follow_redirects|false]
104+
)
105+
);
106+
}
107+
108+
template <class ArgPack>
109+
void init_pimpl(ArgPack const & args, has_io_service) {
110+
pimpl.reset(
111+
new pimpl_type(
112+
args[_cache_resolved|false]
113+
, args[_follow_redirects|false]
114+
, args[_io_service]
115+
)
116+
);
74117
}
75118

76119
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_PARAMETERS_HPP_2010127
2+
#define BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_PARAMETERS_HPP_2010127
3+
4+
// Copyright 2010 Dean Michael Berris.
5+
// Distributed under the Boost Software License, Version 1.0.
6+
// (See accompanying file LICENSE_1_0.txt or copy at
7+
// http://www.boost.org/LICENSE_1_0.txt)
8+
9+
#include <boost/parameter.hpp>
10+
11+
namespace boost { namespace network { namespace http {
12+
13+
BOOST_PARAMETER_NAME(follow_redirects)
14+
BOOST_PARAMETER_NAME(io_service)
15+
BOOST_PARAMETER_NAME(cache_resolved)
16+
17+
} /* http */
18+
19+
} /* network */
20+
21+
} /* boost */
22+
23+
#endif /* BOOST_NETWORK_PROTOCOL_HTTP_CLIENT_PARAMETERS_HPP_2010127 */

libs/network/example/http_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(int argc, char * argv[]) {
7272
http_client::string_type destination_ = host(request);
7373

7474
request << ::boost::network::header("Connection", "close");
75-
http_client client(http_client::follow_redirects);
75+
http_client client(http::_follow_redirects=true);
7676
http_client::response response = client.get(request);
7777

7878
if (show_headers) {

libs/network/test/http/client_constructor_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,16 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(http_client_constructor_test, client, client_types
1717
client instance2(io_service);
1818
}
1919

20+
BOOST_AUTO_TEST_CASE_TEMPLATE(http_cient_constructor_params_test, client, client_types) {
21+
client instance(
22+
http::_follow_redirects=true,
23+
http::_cache_resolved=true
24+
);
25+
boost::asio::io_service io_service;
26+
client instance2(
27+
http::_follow_redirects=true,
28+
http::_io_service=io_service,
29+
http::_cache_resolved=true
30+
);
31+
}
32+

0 commit comments

Comments
 (0)