Skip to content

Commit 25a41e4

Browse files
committed
Re-writing directives to use BOOST_NETWORK_STRING_DIRECTIVE preprocessor macro.
1 parent 06b891e commit 25a41e4

File tree

10 files changed

+46
-372
lines changed

10 files changed

+46
-372
lines changed

boost/network/message/directives.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
/** Include all the various directive headers.
1111
*/
1212

13+
#include <boost/network/message/directives/detail/string_directive.hpp>
1314
#include <boost/network/message/directives/header.hpp>
14-
#include <boost/network/message/directives/body.hpp>
15-
#include <boost/network/message/directives/source.hpp>
16-
#include <boost/network/message/directives/destination.hpp>
1715
#include <boost/network/message/directives/remove_header.hpp>
1816

1917
namespace boost { namespace network {
@@ -25,6 +23,10 @@ namespace boost { namespace network {
2523
return message_;
2624
}
2725

26+
BOOST_NETWORK_STRING_DIRECTIVE(source, source_, message.source(source_));
27+
BOOST_NETWORK_STRING_DIRECTIVE(destination, destination_, message.destination(destination_));
28+
BOOST_NETWORK_STRING_DIRECTIVE(body, body_, message.body(body_));
29+
2830
} // namespace network
2931

3032
} // namespace boost

boost/network/message/directives/body.hpp

-56
This file was deleted.

boost/network/message/directives/destination.hpp

-49
This file was deleted.

boost/network/message/directives/detail/string_directive.hpp

+34-21
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <boost/variant/static_visitor.hpp>
1313
#include <boost/mpl/if.hpp>
1414
#include <boost/mpl/or.hpp>
15+
#include <boost/network/message/directives/detail/string_value.hpp>
1516

1617
namespace boost { namespace network { namespace detail {
1718

@@ -23,30 +24,17 @@ namespace boost { namespace network { namespace detail {
2324
*
2425
* A typical directive implementation of the visitor will take a reference
2526
* to a message as the constructor parameter and then performs operations
26-
* on that message. An example implementation of a directive base is given
27-
* below:
27+
* on that message.
2828
*
29-
* struct foo_directive_base {
30-
* template <class Message>
31-
* struct string_visitor : boost::static_visitor<> {
32-
* Message const & message_;
33-
* string_visitor(Message const & message)
34-
* : message_(message) {}
29+
* To create your own string directive, you can use the preprocessor macro
30+
* BOOST_NETWORK_STRING_DIRECTIVE which takes three parameters: the name of
31+
* the directive, a name for the variable to use in the directive visitor,
32+
* and the body to be implemented in the visitor. An example directive for
33+
* setting the source of a message would look something like this given the
34+
* BOOST_NETWORK_STRING_DIRECTIVE macro:
3535
*
36-
* void operator()(
37-
* typename detail::string_value<typename Message::tag>::type
38-
* const & foo)
39-
* const {
40-
* // do something to message_ here.
41-
* }
36+
* BOOST_NETWORK_STRING_DIRECTIVE(source, source_, message.source(source_));
4237
*
43-
* template <class T> void operator()(T const &) const {
44-
* // fail at runtime?
45-
* }
46-
* };
47-
* };
48-
*
49-
* TODO -- distill this pattern into a preprocessor macro.
5038
*/
5139
template <class Base>
5240
struct string_directive : public Base {
@@ -75,6 +63,31 @@ namespace boost { namespace network { namespace detail {
7563
}
7664
};
7765

66+
#define BOOST_NETWORK_STRING_DIRECTIVE(name, value, body) \
67+
struct name##_directive_base { \
68+
template <class Message> \
69+
struct string_visitor : boost::static_visitor<> { \
70+
Message const & message; \
71+
explicit string_visitor(Message const & message) : \
72+
message(message) {} \
73+
void operator()( \
74+
typename boost::network::detail::string_value< \
75+
typename Message::tag \
76+
>::type const & value \
77+
) const { \
78+
body; \
79+
} \
80+
template <class T> void operator()(T const &) const { \
81+
} \
82+
}; \
83+
}; \
84+
typedef boost::network::detail::string_directive<name##_directive_base> \
85+
name##_directive; \
86+
template <class T> inline name##_directive const \
87+
name (T const & input) { \
88+
return name##_directive(input); \
89+
}
90+
7891
} /* detail */
7992

8093
} /* network */

boost/network/message/directives/source.hpp

-51
This file was deleted.

boost/network/protocol/http/message/directives/status_message.hpp

+3-78
Original file line numberDiff line numberDiff line change
@@ -8,87 +8,12 @@
88
// http://www.boost.org/LICENSE_1_0.txt)
99

1010
#include <boost/network/tags.hpp>
11-
#include <boost/network/support/is_async.hpp>
12-
#include <boost/network/support/is_sync.hpp>
13-
#include <boost/variant/variant.hpp>
14-
#include <boost/variant/static_visitor.hpp>
15-
#include <boost/variant/apply_visitor.hpp>
11+
#include <boost/network/message/directives/detail/string_directive.hpp>
1612

1713
namespace boost { namespace network { namespace http {
1814

19-
template <class Tag>
20-
struct basic_response;
21-
22-
namespace impl {
23-
24-
struct status_message_directive {
25-
26-
boost::variant<
27-
string<tags::default_string>::type,
28-
string<tags::default_wstring>::type,
29-
boost::shared_future<string<tags::default_string>::type>,
30-
boost::shared_future<string<tags::default_wstring>::type>
31-
> status_message_;
32-
33-
status_message_directive(string<tags::default_string>::type const & status_message)
34-
: status_message_(status_message) {}
35-
36-
status_message_directive(string<tags::default_wstring>::type const & status_message)
37-
: status_message_(status_message) {}
38-
39-
status_message_directive(boost::shared_future<string<tags::default_string>::type> const & status_message)
40-
: status_message_(status_message) {}
41-
42-
status_message_directive(boost::shared_future<string<tags::default_wstring>::type> const & status_message)
43-
: status_message_(status_message) {}
44-
45-
status_message_directive(status_message_directive const & other)
46-
: status_message_(other.status_message_) {}
47-
48-
template <class Tag>
49-
struct unsupported_tag;
50-
51-
template <class Tag>
52-
struct value :
53-
mpl::if_<
54-
is_async<Tag>,
55-
boost::shared_future<typename string<Tag>::type>,
56-
typename mpl::if_<
57-
is_sync<Tag>,
58-
typename string<Tag>::type,
59-
unsupported_tag<Tag>
60-
>::type
61-
>
62-
{};
63-
64-
template <class Tag>
65-
struct status_message_visitor : boost::static_visitor<> {
66-
basic_response<Tag> const & response;
67-
status_message_visitor(basic_response<Tag> const & response)
68-
: response(response) {}
69-
void operator() (typename value<Tag>::type const & status_message) const {
70-
response.status_message(status_message);
71-
}
72-
template <class T>
73-
void operator() (T const &) const {
74-
// FIXME fail here!
75-
}
76-
};
77-
78-
template <class Tag>
79-
basic_response<Tag> const & operator() (basic_response<Tag> const & response) const {
80-
apply_visitor(status_message_visitor<Tag>(response), status_message_);
81-
return response;
82-
}
83-
84-
};
85-
86-
} // namespace impl
87-
88-
template <class T>
89-
inline impl::status_message_directive const status_message(T const & status_message_) {
90-
return impl::status_message_directive(status_message_);
91-
}
15+
BOOST_NETWORK_STRING_DIRECTIVE(status_message, status_message_,
16+
message.status_message(status_message_));
9217

9318
} // namespace http
9419

boost/network/protocol/http/message/directives/uri.hpp

+2-31
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,11 @@
88
// http://www.boost.org/LICENSE_1_0.txt)
99

1010
#include <boost/network/tags.hpp>
11-
#include <boost/network/traits/string.hpp>
11+
#include <boost/network/message/directives/detail/string_directive.hpp>
1212

1313
namespace boost { namespace network { namespace http {
1414

15-
template <class Tag>
16-
class basic_request;
17-
18-
namespace impl {
19-
20-
template <class Tag>
21-
struct uri_directive {
22-
23-
typedef typename string<Tag>::type string_type;
24-
25-
string_type uri_;
26-
27-
explicit uri_directive(string_type const & uri)
28-
: uri_(uri) {}
29-
30-
uri_directive(uri_directive const & other)
31-
: uri_(other.uri_) {}
32-
33-
template <class T> basic_request<T> const & operator() (basic_request<T> const & request) const {
34-
request.uri(uri_);
35-
return request;
36-
}
37-
38-
};
39-
40-
} // namespace impl
41-
42-
inline impl::uri_directive<tags::default_> const uri(string<tags::default_>::type const & uri_) {
43-
return impl::uri_directive<tags::default_>(uri_);
44-
}
15+
BOOST_NETWORK_STRING_DIRECTIVE(uri, uri_, message.uri(uri_));
4516

4617
} // namespace http
4718

0 commit comments

Comments
 (0)