Skip to content

Commit 144f21d

Browse files
committed
Fixes to transformer and thread pool libs.
1 parent 3a68cff commit 144f21d

File tree

6 files changed

+140
-186
lines changed

6 files changed

+140
-186
lines changed

boost/network/include/message.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//
99
// This is the modular include file for using the basic message type
1010

11-
#include <boost/network/tags.hpp>
1211
#include <boost/network/message.hpp>
1312

1413
#endif // BOOST_NETWORK_INCLUDE_MESSAGE_HPP_

boost/network/message/transformers/to_lower.hpp

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define __NETWORK_MESSAGE_TRANSFORMERS_TO_LOWER_HPP__
99

1010
#include <boost/algorithm/string.hpp>
11+
#include <boost/network/message/message_base.hpp>
1112

1213
/** to_lower.hpp
1314
*
@@ -18,61 +19,63 @@
1819
* This defines a type, to be applied using template
1920
* metaprogramming on the selected string target.
2021
*/
21-
namespace boost { namespace network {
22-
23-
namespace impl {
24-
25-
template <class Selector>
26-
struct to_lower_transformer { };
27-
28-
template <>
29-
struct to_lower_transformer<selectors::source_selector> {
30-
template <class Tag>
31-
void operator() (basic_message<Tag> & message_) const {
32-
boost::to_lower(message_.source());
33-
}
34-
35-
protected:
36-
~to_lower_transformer() { }
37-
};
38-
39-
template <>
40-
struct to_lower_transformer<selectors::destination_selector> {
41-
template <class Tag>
42-
void operator() (basic_message<Tag> & message_) const {
43-
boost::to_lower(message_.destination());
44-
}
45-
46-
protected:
47-
~to_lower_transformer() { };
48-
};
49-
50-
} // namespace impl
51-
52-
namespace detail {
53-
struct to_lower_placeholder_helper;
54-
}
55-
56-
detail::to_lower_placeholder_helper to_lower_(detail::to_lower_placeholder_helper);
57-
58-
namespace detail {
59-
60-
struct to_lower_placeholder_helper {
61-
template <class Selector>
62-
struct type : public impl::to_lower_transformer<Selector> { };
63-
private:
64-
to_lower_placeholder_helper() {}
65-
to_lower_placeholder_helper(to_lower_placeholder_helper const &) {}
66-
friend to_lower_placeholder_helper boost::network::to_lower_(to_lower_placeholder_helper);
67-
};
68-
69-
}
70-
71-
typedef detail::to_lower_placeholder_helper (*to_lower_placeholder)(detail::to_lower_placeholder_helper);
72-
73-
inline detail::to_lower_placeholder_helper to_lower_(detail::to_lower_placeholder_helper) {
74-
return detail::to_lower_placeholder_helper();
75-
}
22+
namespace boost { namespace network { namespace impl {
23+
24+
template <class Selector>
25+
struct to_lower_transformer { };
26+
27+
template <>
28+
struct to_lower_transformer<selectors::source_selector> {
29+
void operator() (message_base & message_) const {
30+
std::string source_;
31+
message_.get_source(source_);
32+
boost::to_lower(source_);
33+
message_.set_source(source_);
34+
}
35+
36+
protected:
37+
~to_lower_transformer() { }
38+
};
39+
40+
template <>
41+
struct to_lower_transformer<selectors::destination_selector> {
42+
void operator() (message_base & message_) const {
43+
std::string destination_;
44+
message_.get_destination(destination_);
45+
boost::to_lower(destination_);
46+
message_.set_destination(destination_);
47+
}
48+
49+
protected:
50+
~to_lower_transformer() { };
51+
};
52+
53+
} // namespace impl
54+
55+
namespace detail {
56+
struct to_lower_placeholder_helper;
57+
}
58+
59+
detail::to_lower_placeholder_helper to_lower_(detail::to_lower_placeholder_helper);
60+
61+
namespace detail {
62+
63+
struct to_lower_placeholder_helper {
64+
template <class Selector>
65+
struct type : public impl::to_lower_transformer<Selector> { };
66+
private:
67+
to_lower_placeholder_helper() {}
68+
to_lower_placeholder_helper(to_lower_placeholder_helper const &) {}
69+
friend to_lower_placeholder_helper boost::network::to_lower_(to_lower_placeholder_helper);
70+
};
71+
72+
}
73+
74+
typedef detail::to_lower_placeholder_helper (*to_lower_placeholder)(detail::to_lower_placeholder_helper);
75+
76+
inline detail::to_lower_placeholder_helper to_lower_(detail::to_lower_placeholder_helper) {
77+
return detail::to_lower_placeholder_helper();
78+
}
7679

7780
} // namespace network
7881

boost/network/message/transformers/to_upper.hpp

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define __NETWORK_MESSAGE_TRANSFORMERS_TO_UPPER_HPP__
99

1010
#include <boost/algorithm/string.hpp>
11+
#include <boost/network/message/message_base.hpp>
1112

1213
/** to_upper.hpp
1314
*
@@ -18,61 +19,64 @@
1819
* This defines a type, to be applied using template
1920
* metaprogramming on the selected string target.
2021
*/
21-
namespace boost { namespace network {
22+
namespace boost { namespace network { namespace impl {
2223

23-
namespace impl {
24+
template <class Selector>
25+
struct to_upper_transformer { };
2426

25-
template <class Selector>
26-
struct to_upper_transformer { };
27+
template <>
28+
struct to_upper_transformer<selectors::source_selector> {
29+
void operator() (message_base & message_) const {
30+
std::string source_;
31+
message_.get_source(source_);
32+
boost::to_upper(source_);
33+
message_.set_source(source_);
34+
}
2735

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-
}
36+
protected:
37+
~to_upper_transformer() { };
38+
};
3439

35-
protected:
36-
~to_upper_transformer() { };
37-
};
40+
template <>
41+
struct to_upper_transformer<selectors::destination_selector> {
42+
void operator() (message_base & message_) const {
43+
std::string destination_;
44+
message_.get_destination(destination_);
45+
boost::to_upper(destination_);
46+
message_.set_destination(destination_);
47+
}
3848

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-
}
49+
protected:
50+
~to_upper_transformer() { };
51+
};
4552

46-
protected:
47-
~to_upper_transformer() { };
48-
};
53+
} // namespace impl
4954

50-
} // namespace impl
55+
namespace detail {
56+
struct to_upper_placeholder_helper;
57+
}
5158

52-
namespace detail {
53-
struct to_upper_placeholder_helper;
54-
}
59+
detail::to_upper_placeholder_helper to_upper_(detail::to_upper_placeholder_helper);
5560

56-
detail::to_upper_placeholder_helper to_upper_(detail::to_upper_placeholder_helper);
61+
namespace detail {
5762

58-
namespace detail {
63+
struct to_upper_placeholder_helper {
64+
template <class Selector>
65+
struct type : public impl::to_upper_transformer<Selector> { };
5966

60-
struct to_upper_placeholder_helper {
61-
template <class Selector>
62-
struct type : public impl::to_upper_transformer<Selector> { };
63-
private:
64-
to_upper_placeholder_helper() {}
65-
to_upper_placeholder_helper(to_upper_placeholder_helper const &) {}
66-
friend to_upper_placeholder_helper boost::network::to_upper_(to_upper_placeholder_helper);
67-
};
67+
private:
68+
to_upper_placeholder_helper() {}
69+
to_upper_placeholder_helper(to_upper_placeholder_helper const &) {}
70+
friend to_upper_placeholder_helper boost::network::to_upper_(to_upper_placeholder_helper);
71+
};
6872

69-
}
73+
}
7074

71-
typedef detail::to_upper_placeholder_helper (*to_upper_placeholder)(detail::to_upper_placeholder_helper);
75+
typedef detail::to_upper_placeholder_helper (*to_upper_placeholder)(detail::to_upper_placeholder_helper);
7276

73-
inline detail::to_upper_placeholder_helper to_upper_(detail::to_upper_placeholder_helper) {
74-
return detail::to_upper_placeholder_helper();
75-
}
77+
inline detail::to_upper_placeholder_helper to_upper_(detail::to_upper_placeholder_helper) {
78+
return detail::to_upper_placeholder_helper();
79+
}
7680

7781
} // namespace network
7882

boost/network/utils/thread_pool.hpp

Lines changed: 21 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
// http://www.boost.org/LICENSE_1_0.txt)
88

99
#include <cstddef>
10-
#include <boost/network/tags.hpp>
1110
#include <boost/thread/thread.hpp>
1211
#include <boost/shared_ptr.hpp>
1312
#include <boost/function.hpp>
@@ -16,98 +15,32 @@
1615

1716
namespace boost { namespace network { namespace utils {
1817

19-
typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr;
20-
typedef boost::shared_ptr<boost::thread_group> worker_threads_ptr;
21-
typedef boost::shared_ptr<boost::asio::io_service::work> sentinel_ptr;
22-
23-
template <class Tag>
24-
struct basic_thread_pool {
25-
basic_thread_pool(
26-
std::size_t threads = 1,
27-
io_service_ptr io_service = io_service_ptr(),
28-
worker_threads_ptr worker_threads = worker_threads_ptr()
29-
)
30-
: threads_(threads)
31-
, io_service_(io_service)
32-
, worker_threads_(worker_threads)
33-
, sentinel_()
34-
{
35-
bool commit = false;
36-
BOOST_SCOPE_EXIT_TPL((&commit)(&io_service_)(&worker_threads_)(&sentinel_)) {
37-
if (!commit) {
38-
sentinel_.reset();
39-
io_service_.reset();
40-
if (worker_threads_.get()) {
41-
worker_threads_->interrupt_all();
42-
worker_threads_->join_all();
43-
}
44-
worker_threads_.reset();
45-
}
46-
} BOOST_SCOPE_EXIT_END
18+
typedef boost::shared_ptr<boost::asio::io_service> io_service_ptr;
19+
typedef boost::shared_ptr<boost::thread_group> worker_threads_ptr;
20+
typedef boost::shared_ptr<boost::asio::io_service::work> sentinel_ptr;
4721

48-
if (!io_service_.get()) {
49-
io_service_.reset(new boost::asio::io_service);
50-
}
22+
struct thread_pool_pimpl;
5123

52-
if (!worker_threads_.get()) {
53-
worker_threads_.reset(new boost::thread_group);
54-
}
24+
struct thread_pool {
25+
thread_pool(std::size_t threads = 1,
26+
io_service_ptr io_service = io_service_ptr(),
27+
worker_threads_ptr worker_threads = worker_threads_ptr());
28+
std::size_t const thread_count() const;
29+
void post(function<void()> f);
30+
~thread_pool();
31+
void swap(thread_pool & other);
32+
protected:
33+
thread_pool_pimpl * pimpl;
34+
};
5535

56-
if (!sentinel_.get()) {
57-
sentinel_.reset(new boost::asio::io_service::work(*io_service_));
58-
}
36+
inline void swap(thread_pool & l, thread_pool & r) {
37+
l.swap(r);
38+
}
5939

60-
for (std::size_t counter = 0; counter < threads_; ++counter)
61-
worker_threads_->create_thread(
62-
boost::bind(
63-
&boost::asio::io_service::run,
64-
io_service_
65-
)
66-
);
40+
} // utils
6741

68-
commit = true;
69-
}
42+
} // network
7043

71-
std::size_t const thread_count() const {
72-
return threads_;
73-
}
74-
75-
void post(boost::function<void()> f) {
76-
io_service_->post(f);
77-
}
78-
79-
~basic_thread_pool() throw () {
80-
sentinel_.reset();
81-
try {
82-
worker_threads_->join_all();
83-
} catch (...) {
84-
BOOST_ASSERT(false && "A handler was not supposed to throw, but one did.");
85-
}
86-
}
87-
88-
void swap(basic_thread_pool & other) {
89-
std::swap(other.threads_, threads_);
90-
std::swap(other.io_service_, io_service_);
91-
std::swap(other.worker_threads_, worker_threads_);
92-
std::swap(other.sentinel_, sentinel_);
93-
}
94-
protected:
95-
std::size_t threads_;
96-
io_service_ptr io_service_;
97-
worker_threads_ptr worker_threads_;
98-
sentinel_ptr sentinel_;
99-
100-
private:
101-
basic_thread_pool(basic_thread_pool const &); // no copies please
102-
basic_thread_pool & operator=(basic_thread_pool); // no assignment please
103-
};
104-
105-
typedef basic_thread_pool<tags::default_> thread_pool;
106-
107-
} /* utils */
108-
109-
} /* network */
110-
111-
} /* boost */
44+
} // boost
11245

11346
#endif /* BOOST_NETWORK_UTILS_THREAD_POOL_HPP_20101020 */

libs/network/src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ add_library(cppnetlib-message-wrappers ${CPP-NETLIB_MESSAGE_WRAPPERS_SRCS})
2727

2828
set(CPP-NETLIB_HTTP_MESSAGE_SRCS http/request.cpp http/response.cpp)
2929
add_library(cppnetlib-http-message ${CPP-NETLIB_HTTP_MESSAGE_SRCS})
30+
31+
set(CPP-NETLIB_UTILS_THREAD_POOL_SRCS utils/thread_pool.cpp)
32+
add_library(cppnetlib-utils-thread_pool ${CPP-NETLIB_UTILS_THREAD_POOL_SRCS})

0 commit comments

Comments
 (0)