|
| 1 | +// Copyright 2011 Dean Michael Berris <dberris@google.com>. |
| 2 | +// Copyright 2011 Google, Inc. |
| 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_BASIC_MESSAGE_IPP_20110911 |
| 8 | +#defineNETWORK_MESSAGE_BASIC_MESSAGE_IPP_20110911 |
| 9 | + |
| 10 | +namespacenetwork { |
| 11 | + |
| 12 | +structbasic_storage_pimpl { |
| 13 | +basic_storage_pimpl(); |
| 14 | +basic_storage_pimpl(basic_storage_pimplconst &); |
| 15 | + |
| 16 | +virtual basic_storage_pimpl*clone(); |
| 17 | +protected: |
| 18 | +friendstructbasic_storage_base; |
| 19 | + std::string source_, destination_; |
| 20 | +typedef std::multimap<std::string, std::string> headers_container_type; |
| 21 | + headers_container_type headers_; |
| 22 | + std::string body_; |
| 23 | +}; |
| 24 | + |
| 25 | +basic_storage_base::basic_storage_base() |
| 26 | +: pimpl(new (std::nothrow) basic_storage_pimpl()) |
| 27 | +{} |
| 28 | + |
| 29 | +basic_storage_base::basic_storage_base(basic_storage_baseconst & other) |
| 30 | +: pimpl(other.clone()) |
| 31 | +{} |
| 32 | + |
| 33 | +voidbasic_storage_base::set_destination(std::stringconst & destination) { |
| 34 | + pimpl->destination_ = destination; |
| 35 | +} |
| 36 | + |
| 37 | +voidbasic_storage_base::set_source(std::stringconst & source) { |
| 38 | + pimpl->source_ = source; |
| 39 | +} |
| 40 | + |
| 41 | +voidbasic_storage_base::append_header(std::stringconst & name, |
| 42 | + std::stringconst & value) { |
| 43 | + pimpl->headers_.insert(std::make_pair(name, value)); |
| 44 | +} |
| 45 | + |
| 46 | +voidbasic_storage_base::remove_headers(std::stringconst & name) { |
| 47 | + pimpl->headers_.erase(name); |
| 48 | +} |
| 49 | + |
| 50 | +voidbasic_storage_base::remove_headers() { |
| 51 | +basic_storage_pimpl::headers_container_type().swap(pimpl->headers_); |
| 52 | +} |
| 53 | + |
| 54 | +voidbasic_storage_base::set_body(std::stringconst & body) { |
| 55 | + pimpl->body = body; |
| 56 | +} |
| 57 | + |
| 58 | +voidbasic_storage_base::append_body(std::stringconst & data) { |
| 59 | + pimpl->body.append(data); |
| 60 | +} |
| 61 | + |
| 62 | +voidbasic_storage_base::get_destination(std::string & destination) { |
| 63 | + destination = pimpl->destination; |
| 64 | +} |
| 65 | + |
| 66 | +voidbasic_storage_base::get_source(std::string & source) { |
| 67 | + source = pimpl->source; |
| 68 | +} |
| 69 | + |
| 70 | +voidbasic_storage_base::get_headers(function<void(std::stringconst &, std::stringconst &)> inserter) { |
| 71 | +copy(pimpl->headers_, inserter); |
| 72 | +} |
| 73 | + |
| 74 | +voidbasic_storage_base::get_headers(std::stringconst & name, function<void(std::stringconst &, std::stringconst &)> inserter) { |
| 75 | + basic_storage_pimpl::headers_container_type::const_iterator |
| 76 | + it = pimpl->headers_.find(name), |
| 77 | + pe = pimpl->headers_.end(); |
| 78 | +for (; it != pe; ++it) |
| 79 | +inserter(it->first, it->second); |
| 80 | +} |
| 81 | + |
| 82 | +voidbasic_storage_base::get_body(std::string & body) { |
| 83 | +// TODO use iostreams! |
| 84 | + body = pimpl_->body; |
| 85 | +} |
| 86 | + |
| 87 | +voidbasic_storage_base::get_body(function<void(iterator_range<charconst *>)> chunk_reader, size_t size) { |
| 88 | +// TODO use iostreams! |
| 89 | + std::string::const_iterator it = pimpl->body.begin(), |
| 90 | + pe = pimpl->body.end(); |
| 91 | +std::advance(it, size); |
| 92 | +chunk_reader(make_iterator_range(it, pe)); |
| 93 | + pimpl->body.assign(it, pe); |
| 94 | +} |
| 95 | + |
| 96 | +basic_storage_base::~basic_storage_base() { |
| 97 | + pimpl->reset(); |
| 98 | +} |
| 99 | + |
| 100 | +voidbasic_storage_base::swap(basic_storage_base & other) { |
| 101 | +std::swap(pimpl, other.pimpl); |
| 102 | +} |
| 103 | + |
| 104 | +voidswap(basic_storage_base & l, basic_storage_base & r) { |
| 105 | + l.swap(r); |
| 106 | +} |
| 107 | + |
| 108 | +}/* network*/ |
| 109 | + |
| 110 | +#endif/* NETWORK_MESSAGE_BASIC_MESSAGE_IPP_20110911*/ |