|
10 | 10 | #include<boost/variant/variant.hpp> |
11 | 11 | #include<boost/variant/apply_visitor.hpp> |
12 | 12 | #include<boost/variant/static_visitor.hpp> |
| 13 | +#include<boost/network/support/is_pod.hpp> |
| 14 | +#include<boost/utility/enable_if.hpp> |
13 | 15 | #include<boost/mpl/if.hpp> |
14 | 16 | #include<boost/mpl/or.hpp> |
15 | | -#include<boost/network/message/directives/detail/string_value.hpp> |
16 | 17 |
|
17 | | -namespaceboost {namespacenetwork {namespacedetail { |
18 | | - |
19 | | -/** This directive template technically implements a decorator pattern |
20 | | - * which takes a concrete implementation and uses that as a base. This |
21 | | - * particular directive template requires the Base type to just include |
22 | | - * a nested template named `string_visitor` that takes a single template |
23 | | - * class parameter which is supposed to be a Message class/struct. |
24 | | - * |
25 | | - * A typical directive implementation of the visitor will take a reference |
26 | | - * to a message as the constructor parameter and then performs operations |
27 | | - * on that message. |
28 | | - * |
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: |
35 | | - * |
36 | | - * BOOST_NETWORK_STRING_DIRECTIVE(source, source_, |
37 | | - * message.source(source_) |
38 | | - * , message.source=source_); |
39 | | - * |
40 | | -*/ |
41 | | -template<classBase> |
42 | | -structstring_directive :publicBase { |
43 | | - boost::variant< |
44 | | - string<tags::default_string>::type, |
45 | | - string<tags::default_wstring>::type, |
46 | | - boost::shared_future<string<tags::default_string>::type>, |
47 | | - boost::shared_future<string<tags::default_wstring>::type> |
48 | | - > string_; |
49 | | - |
50 | | -explicitstring_directive(string<tags::default_string>::typeconst & input) |
51 | | - : string_(input) {} |
52 | | -explicitstring_directive(string<tags::default_wstring>::typeconst & input) |
53 | | - : string_(input) {} |
54 | | -explicitstring_directive(boost::shared_future<string<tags::default_string>::type>const & input) |
55 | | - : string_(input) {} |
56 | | -explicitstring_directive(boost::shared_future<string<tags::default_wstring>::type>const & input) |
57 | | - : string_(input) {} |
58 | | - |
59 | | -string_directive(string_directiveconst & other) |
60 | | - : string_(other.string_) {} |
61 | | - |
62 | | -template<classTag,template<class>classMessage> |
63 | | -voidoperator()(Message<Tag>const & message)const { |
64 | | -apply_visitor(typename Base::template string_visitor<Message<Tag> >(message), string_); |
65 | | - } |
66 | | - }; |
| 18 | +/** |
| 19 | + * |
| 20 | + * To create your own string directive, you can use the preprocessor macro |
| 21 | + * BOOST_NETWORK_STRING_DIRECTIVE which takes three parameters: the name of |
| 22 | + * the directive, a name for the variable to use in the directive visitor, |
| 23 | + * and the body to be implemented in the visitor. An example directive for |
| 24 | + * setting the source of a message would look something like this given the |
| 25 | + * BOOST_NETWORK_STRING_DIRECTIVE macro: |
| 26 | + * |
| 27 | + * BOOST_NETWORK_STRING_DIRECTIVE(source, source_, |
| 28 | + * message.source(source_) |
| 29 | + * , message.source=source_); |
| 30 | + * |
| 31 | +*/ |
67 | 32 |
|
| 33 | +#ifndef BOOST_NETWORK_STRING_DIRECTIVE |
68 | 34 | #defineBOOST_NETWORK_STRING_DIRECTIVE(name, value, body, pod_body) \ |
69 | | -structname##_directive_base { \ |
70 | | - \ |
71 | | -template<classMessage> \ |
72 | | -structnormal_visitor : boost::static_visitor<> { \ |
73 | | - Messageconst & message; \ |
74 | | -explicitnormal_visitor(Messageconst & message) : \ |
75 | | - message(message) {} \ |
76 | | -voidoperator()( \ |
77 | | -typename boost::network::detail::string_value< \ |
78 | | -typename Message::tag \ |
79 | | - >::typeconst & value \ |
80 | | - )const { \ |
81 | | - body; \ |
82 | | - } \ |
83 | | -template<classT>voidoperator()(Tconst &)const { \ |
84 | | - } \ |
85 | | - }; \ |
86 | | - \ |
87 | | -template<classMessage> \ |
88 | | -structpod_visitor : boost::static_visitor<> { \ |
89 | | - Messageconst & message; \ |
90 | | -explicitpod_visitor(Messageconst & message) : \ |
91 | | - message(message) {} \ |
92 | | -voidoperator()( \ |
93 | | -typename boost::network::detail::string_value< \ |
94 | | -typename Message::tag \ |
95 | | - >::typeconst & value \ |
96 | | - )const { \ |
97 | | - pod_body; \ |
98 | | - } \ |
99 | | -template<classT>voidoperator()(Tconst &)const { \ |
100 | | - } \ |
101 | | - }; \ |
102 | | - \ |
103 | | -template<classMessage> \ |
104 | | -structstring_visitor : \ |
105 | | - mpl::if_< \ |
106 | | - is_base_of< \ |
107 | | - tags::pod, \ |
108 | | -typename Message::tag \ |
109 | | - >, \ |
110 | | - pod_visitor<Message>, \ |
111 | | - normal_visitor<Message> \ |
112 | | - >::type \ |
113 | | - { \ |
114 | | -typedeftypename mpl::if_< \ |
115 | | - is_base_of< \ |
116 | | - tags::pod, \ |
117 | | -typename Message::tag \ |
118 | | - >, \ |
119 | | - pod_visitor<Message>, \ |
120 | | - normal_visitor<Message> \ |
121 | | - >::type base; \ |
122 | | -explicitstring_visitor(Messageconst & message): \ |
123 | | - base(message) {} \ |
124 | | -string_visitor(string_visitorconst & other): \ |
125 | | -base(other) {} \ |
126 | | -usingbase::operator(); \ |
127 | | - }; \ |
| 35 | +template<classValueType> \ |
| 36 | +structname##_directive { \ |
| 37 | + ValueTypeconst & value; \ |
| 38 | +explicit name##_directive(ValueTypeconst & value_) \ |
| 39 | + : value(value_) {} \ |
| 40 | + name##_directive(name##_directiveconst & other) \ |
| 41 | + : value(other.value) {} \ |
| 42 | +template<classTag,template<class>classMessage> \ |
| 43 | +typename enable_if<is_pod<Tag>,void>::type \ |
| 44 | +operator()(Message<Tag> & message)const { \ |
| 45 | + pod_body; \ |
| 46 | + } \ |
| 47 | +template<classTag,template<class>classMessage> \ |
| 48 | +typename enable_if<mpl::not_<is_pod<Tag> >,void>::type \ |
| 49 | +operator()(Message<Tag> & message)const { \ |
| 50 | + body; \ |
| 51 | + } \ |
128 | 52 | }; \ |
129 | 53 | \ |
130 | | -typedef boost::network::detail::string_directive<name##_directive_base> \ |
131 | | - name##_directive; \ |
132 | | -template<classT>inline name##_directiveconst \ |
| 54 | +template<classT>inline name##_directive<T> \ |
133 | 55 | name (Tconst & input) { \ |
134 | | -return name##_directive(input); \ |
| 56 | +return name##_directive<T>(input); \ |
135 | 57 | } |
136 | | - |
137 | | -}/* detail*/ |
138 | | - |
139 | | -}/* network*/ |
140 | | - |
141 | | -}/* boost*/ |
| 58 | +#endif/* BOOST_NETWORK_STRING_DIRECTIVE*/ |
142 | 59 |
|
143 | 60 | #endif/* BOOST_NETWORK_MESSAGE_DIRECTIVES_DETAIL_STRING_DIRECTIVE_HPP_20100915*/ |