Skip to content

Commit 9dc07de

Browse files
author
mikhail_beris
committed
Implement to_upper transformer and basic transformation infrastructure.
1 parent 3bf7c60 commit 9dc07de

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

boost/network/message.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,8 @@ namespace boost { namespace network {
8080
#include <boost/network/message/wrappers.hpp>
8181
// pull in wrappers header file
8282

83+
#include <boost/network/message/transformers.hpp>
84+
// pull in transformer header file
85+
8386
#endif // __NETWORK_MESSAGE_HPP__
8487

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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_TRANSFORMERS_HPP__
8+
#define __NETWORK_MESSAGE_TRANSFORMERS_HPP__
9+
10+
/** transformers.hpp
11+
*
12+
* Pulls in all the transformers files.
13+
*/
14+
#include <boost/network/message/transformers/selectors.hpp>
15+
#include <boost/network/message/transformers/to_upper.hpp>
16+
17+
namespace boost { namespace network {
18+
namespace impl {
19+
template <class Algorithm, class Selector>
20+
struct get_real_algorithm {
21+
typedef typename Algorithm::type<Selector> type;
22+
};
23+
24+
template <class Algorithm, class Selector>
25+
struct transform_impl : public get_real_algorithm<Algorithm, Selector>::type { };
26+
}; // namspace impl
27+
28+
template <class Algorithm, class Selector>
29+
inline impl::transform_impl<Algorithm, Selector>
30+
transform(Algorithm, Selector) {
31+
return impl::transform_impl<Algorithm, Selector>();
32+
};
33+
34+
template <class Tag, class Algorithm, class Selector>
35+
inline basic_message<Tag> &
36+
operator<< (basic_message<Tag> & msg_,
37+
impl::transform_impl<Algorithm, Selector>
38+
const & transformer) {
39+
transformer(msg_);
40+
return msg_;
41+
};
42+
43+
}; // namespace network
44+
45+
}; // namespace boost
46+
47+
#endif // __NETWORK_MESSAGE_TRANSFORMERS_HPP__
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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_TRANSFORMERS_SELECTORS_HPP__
8+
#define __NETWORK_MESSAGE_TRANSFORMERS_SELECTORS_HPP__
9+
10+
namespace boost { namespace network {
11+
namespace selectors {
12+
struct source_selector { };
13+
struct destination_selector { };
14+
}; // namespace selectors
15+
16+
extern selectors::source_selector source_;
17+
extern selectors::destination_selector destination_;
18+
19+
}; // namespace network
20+
21+
}; // namespace boost
22+
23+
#endif // __NETWORK_MESSAGE_TRANSFORMERS_SELECTORS_HPP__
24+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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_TRANSFORMERS_TO_UPPER_HPP__
8+
#define __NETWORK_MESSAGE_TRANSFORMERS_TO_UPPER_HPP__
9+
10+
#include <boost/algorithm/string.hpp>
11+
12+
/** to_upper.hpp
13+
*
14+
* Implements the to_upper transformer. This applies
15+
* the to_upper string algorithm to a string, which
16+
* is selected by the appropriate selector.
17+
*
18+
* This defines a type, to be applied using template
19+
* metaprogramming on the selected string target.
20+
*/
21+
namespace boost { namespace network {
22+
23+
namespace impl {
24+
25+
template <class Selector>
26+
struct to_upper_transformer { };
27+
28+
template <>
29+
struct to_upper_transformer<selectors::source_selector> {
30+
template <class Tag>
31+
void operator() (basic_message<Tag> & message_) const {
32+
boost::to_upper(message_.source());
33+
};
34+
35+
protected:
36+
~to_upper_transformer() { };
37+
};
38+
39+
template <>
40+
struct to_upper_transformer<selectors::destination_selector> {
41+
template <class Tag>
42+
void operator() (basic_message<Tag> & message_) const {
43+
boost::to_upper(message_.destination());
44+
};
45+
46+
protected:
47+
~to_upper_transformer() { };
48+
};
49+
50+
}; // namespace impl
51+
52+
struct to_upper_placeholder {
53+
template <class Selector>
54+
struct type : public impl::to_upper_transformer<Selector> { };
55+
} ;
56+
57+
extern to_upper_placeholder to_upper_;
58+
59+
}; // namespace network
60+
61+
}; // namespace boost
62+
63+
#endif // __NETWORK_MESSAGE_TRANSFORMERS_TO_UPPER_HPP__
64+

libs/network/test/Jamfile.v2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ project network_test :
1212
;
1313

1414
unit-test message_test : message_test.cpp ;
15+
16+
unit-test message_transform_test : message_transform_test.cpp ;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
#define BOOST_TEST_MODULE message test
8+
#include <boost/test/unit_test.hpp>
9+
#include <boost/network.hpp>
10+
#include <algorithm>
11+
12+
BOOST_AUTO_TEST_CASE ( message_transform_tolower ) {
13+
using namespace boost::network;
14+
15+
message msg;
16+
msg << source("me");
17+
BOOST_CHECK_EQUAL ( source(msg), "me" );
18+
msg << transform(to_upper_, source_);
19+
BOOST_CHECK_EQUAL ( source(msg), "ME" );
20+
};

0 commit comments

Comments
 (0)