|
| 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_HEADERS_HPP__ |
| 8 | +#define __NETWORK_MESSAGE_WRAPPERS_HEADERS_HPP__ |
| 9 | + |
| 10 | +namespace boost { namespace network { |
| 11 | + |
| 12 | + // Forward declaration |
| 13 | + template <class Message> |
| 14 | + struct headers_range ; |
| 15 | + |
| 16 | + /** headers wrapper for messages. |
| 17 | + * |
| 18 | + * This exposes an interface similar to a map, indexable |
| 19 | + * using operator[] taking a string as the index and returns |
| 20 | + * a range of iterators (std::pair<iterator, iterator>) |
| 21 | + * whose keys are all equal to the index string. |
| 22 | + */ |
| 23 | + namespace impl { |
| 24 | + template <class Tag> |
| 25 | + struct headers_wrapper : public detail::wrapper_base<Tag> { |
| 26 | + typedef Tag tag; |
| 27 | + typedef basic_message<tag> message_type; |
| 28 | + typedef typename headers_range<message_type>::type range_type; |
| 29 | + |
| 30 | + explicit headers_wrapper(basic_message<tag> & message_) |
| 31 | + : detail::wrapper_base<tag>(message_) |
| 32 | + { }; |
| 33 | + |
| 34 | + range_type operator[] (std::string const & key) const { |
| 35 | + return _message.headers().equal_range(key); |
| 36 | + }; |
| 37 | + |
| 38 | + typename message_type::headers_container_type::size_type count(std::string const & key) const { |
| 39 | + return _message.headers().count(key); |
| 40 | + }; |
| 41 | + |
| 42 | + }; |
| 43 | + }; // namespace impl |
| 44 | + |
| 45 | + /// Factory method to create the right wrapper object |
| 46 | + template <class Tag, template <class> class Message> |
| 47 | + inline impl::headers_wrapper<Tag> |
| 48 | + headers(Message<Tag> & message_) { |
| 49 | + return impl::headers_wrapper<Tag>(message_); |
| 50 | + }; |
| 51 | + |
| 52 | + /// Template metaprogram to get the range type for a message |
| 53 | + template <class Message> |
| 54 | + struct headers_range { |
| 55 | + typedef typename |
| 56 | + std::pair< |
| 57 | + typename Message::headers_container_type::const_iterator, |
| 58 | + typename Message::headers_container_type::const_iterator> |
| 59 | + type; |
| 60 | + }; |
| 61 | + |
| 62 | +}; // namespace network |
| 63 | + |
| 64 | +}; // namespace boost |
| 65 | + |
| 66 | +#endif // __NETWORK_MESSAGE_WRAPPERS_HEADERS_HPP__ |
| 67 | + |
0 commit comments