Skip to content

Commit 0c1b508

Browse files
author
mikhail_beris
committed
Add support for body(...) directives and wrapper.
1 parent 43e2285 commit 0c1b508

File tree

7 files changed

+106
-0
lines changed

7 files changed

+106
-0
lines changed

boost/network/.message.hpp.swp

-12 KB
Binary file not shown.

boost/network/message.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,17 @@ namespace boost { namespace network {
4646
return _headers;
4747
};
4848

49+
std::string & body() const {
50+
return _body;
51+
};
52+
4953
private:
5054

5155
friend struct detail::directive_base<tag> ;
5256
friend struct detail::wrapper_base<tag> ;
5357

5458
mutable headers_container_type _headers;
59+
mutable std::string _body;
5560
};
5661

5762
typedef basic_message<> message; // default message type

boost/network/message/directives.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
#include <boost/network/message/directives/header.hpp>
14+
#include <boost/network/message/directives/body.hpp>
1415

1516
namespace boost { namespace network {
1617

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
// Copyright Dean Michael Berris 2007.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifndef __NETWORK_MESSAGE_DIRECTIVES_BODY_HPP__
8+
#define __NETWORK_MESSAGE_DIRECTIVES_BODY_HPP__
9+
10+
/** body.hpp
11+
*
12+
* Defines the types involved and the semantics of adding
13+
* body contents into message objects.
14+
*
15+
* WARNING: DO NOT INCLUDE THIS HEADER DIRECTLY. THIS REQUIRES
16+
* TYPES TO BE DEFINED FROM EARLIER FILES THAT INCLUDE THIS
17+
* HEADER.
18+
*/
19+
namespace boost { namespace network {
20+
21+
namespace impl {
22+
template <class Tag>
23+
struct body_directive : public detail::directive_base<Tag> {
24+
typedef Tag tag;
25+
26+
explicit body_directive(
27+
std::string const & body
28+
) :
29+
_body(body)
30+
{ };
31+
32+
void operator() (basic_message<tag> & msg) const {
33+
msg.body() = _body;
34+
};
35+
36+
private:
37+
38+
std::string _body;
39+
};
40+
}; // namespace impl
41+
42+
inline impl::body_directive<tags::default_>
43+
body(std::string const & body_) {
44+
return impl::body_directive<tags::default_>(body_);
45+
};
46+
47+
}; // namespace network
48+
49+
}; // namespace boost
50+
51+
#endif // __NETWORK_MESSAGE_DIRECTIVES_BODY_HPP__
52+

boost/network/message/wrappers.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@
1212
* Pulls in all the wrapper header files.
1313
*/
1414
#include <boost/network/message/wrappers/headers.hpp>
15+
#include <boost/network/message/wrappers/body.hpp>
1516

1617
#endif // __NETWORK_MESSAGE_WRAPPERS_HPP__
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
// Copyright Dean Michael Berris 2007.
3+
// Distributed under the Boost Software License, Version 1.0.
4+
// (See accompanying file LICENSE_1_0.txt or copy at
5+
// http://www.boost.org/LICENSE_1_0.txt)
6+
7+
#ifndef __NETWORK_MESSAGE_WRAPPERS_BODY_HPP__
8+
#define __NETWORK_MESSAGE_WRAPPERS_BODY_HPP__
9+
10+
namespace boost { namespace network {
11+
12+
namespace impl {
13+
template <class Tag>
14+
struct body_wrapper : public detail::wrapper_base<Tag> {
15+
typedef Tag tag;
16+
typedef basic_message<tag> message_type;
17+
18+
explicit body_wrapper(basic_message<tag> & message_)
19+
: detail::wrapper_base<tag>(message_)
20+
{ };
21+
22+
operator std::string () const {
23+
return std::string(_message.body());
24+
};
25+
};
26+
}; // namespace impl
27+
28+
template <class Tag>
29+
inline std::string
30+
body(basic_message<Tag> & message_) {
31+
return impl::body_wrapper<Tag>(message_);
32+
};
33+
34+
}; // namespace network
35+
36+
}; // namespace boost
37+
38+
#endif // __NETWORK_MESSAGE_WRAPPERS_BODY_HPP__
39+

libs/network/test/message_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,11 @@ BOOST_AUTO_TEST_CASE(header_directives_test) {
3030
headers_range<message>::type range = headers(msg)["SOME_HEADER"];
3131
BOOST_CHECK( range.first != range.second );
3232
}
33+
34+
BOOST_AUTO_TEST_CASE(body_directive_test) {
35+
using namespace boost::network;
36+
message msg;
37+
msg << body("The quick brown fox jumps over the lazy dog.") ;
38+
39+
BOOST_CHECK_EQUAL( body(msg), "The quick brown fox jumps over the lazy dog.");
40+
}

0 commit comments

Comments
 (0)