Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3c6c0aa

Browse files
committed
Using Boost.Parameter for the client constructors instead of the adhoc method used earlier.
1 parent4169f81 commit3c6c0aa

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
namespaceboost {namespacenetwork {namespacehttp {
3030

3131
template<classTag,unsigned version_major,unsigned version_minor>
3232
structbasic_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-
typedefbasic_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+
typedefbasic_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
typedeftypename string<Tag>::type string_type;
4242
typedef Tag tag_type;
4343

44-
structcache_resolved_type { };
45-
46-
static cache_resolved_typecache_resolved() {
47-
returncache_resolved_type();
48-
};
49-
50-
structfollow_redirect_type { };
51-
52-
static follow_redirect_typefollow_redirects() {
53-
returnfollow_redirect_type();
54-
};
55-
56-
static follow_redirect_typefollow_redirect() {
57-
returnfollow_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-
explicitbasic_client(cache_resolved_type (*)())
67-
: base_facade_type(), pimpl(new pimpl_type(true,false))
68-
{}
69-
70-
explicitbasic_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-
explicitbasic_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-
friendstructbasic_client_facade<Tag,basic_client<Tag,version_major,version_minor> > ;
93-
94-
basic_response<Tag>constrequest_skeleton(requestconst & request_, string_type method,bool get_body) {
95-
return pimpl->request_skeleton(request_, method, get_body);
96-
}
97-
98-
voidclear_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
namespaceboost {namespacenetwork {namespacehttp {
1315

@@ -17,26 +19,39 @@ namespace boost { namespace network { namespace http {
1719
template<classTag>
1820
structbasic_response;
1921

20-
template<classTag,classDerived>
22+
template<classTag,unsigned version_major,unsigned version_minor>
2123
structbasic_client_facade {
2224

2325
typedeftypename 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<classArgPack>
31+
basic_client_facade(ArgPackconst & 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
responseconsthead (requestconst & request_) {
31-
returnstatic_cast<Derived*>(this)->request_skeleton(request_,"HEAD",false);
46+
returnpimpl->request_skeleton(request_,"HEAD",false);
3247
}
3348

3449
responseconstget (requestconst & request_) {
35-
returnstatic_cast<Derived*>(this)->request_skeleton(request_,"GET",true);
50+
returnpimpl->request_skeleton(request_,"GET",true);
3651
}
3752

3853
responseconstpost (requestconst & request_) {
39-
returnstatic_cast<Derived*>(this)->request_skeleton(request_,"POST",true);
54+
returnpimpl->request_skeleton(request_,"POST",true);
4055
}
4156

4257
responseconstpost (request request_, string_typeconst & content_type, string_typeconst & body_) {
@@ -51,7 +66,7 @@ namespace boost { namespace network { namespace http {
5166
}
5267

5368
responseconstput (requestconst & request_) {
54-
returnstatic_cast<Derived*>(this)->request_skeleton(request_,"PUT",true);
69+
returnpimpl->request_skeleton(request_,"PUT",true);
5570
}
5671

5772
responseconstput (requestconst & request_, string_typeconst & body_) {
@@ -66,11 +81,39 @@ namespace boost { namespace network { namespace http {
6681
}
6782

6883
responseconstdelete_ (requestconst & request_) {
69-
returnstatic_cast<Derived*>(this)->request_skeleton(request_,"DELETE",true);
84+
returnpimpl->request_skeleton(request_,"DELETE",true);
7085
}
7186

7287
voidclear_resolved_cache() {
73-
static_cast<Derived*>(this)->clear_resolved_cache();
88+
pimpl->clear_resolved_cache();
89+
}
90+
91+
protected:
92+
93+
structno_io_service {};
94+
structhas_io_service {};
95+
96+
boost::shared_ptr<pimpl_type> pimpl;
97+
98+
template<classArgPack>
99+
voidinit_pimpl(ArgPackconst & args, no_io_service) {
100+
pimpl.reset(
101+
newpimpl_type(
102+
args[_cache_resolved|false]
103+
, args[_follow_redirects|false]
104+
)
105+
);
106+
}
107+
108+
template<classArgPack>
109+
voidinit_pimpl(ArgPackconst & args, has_io_service) {
110+
pimpl.reset(
111+
newpimpl_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+
#defineBOOST_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+
namespaceboost {namespacenetwork {namespacehttp {
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_clientclient(http_client::follow_redirects);
75+
http_clientclient(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
clientinstance2(io_service);
1818
}
1919

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp