~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/basic_streambuf.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// basic_streambuf.hpp
3
 
// ~~~~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
 
//
7
 
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
 
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
 
//
10
 
 
11
 
#ifndef ASIO_BASIC_STREAMBUF_HPP
12
 
#define ASIO_BASIC_STREAMBUF_HPP
13
 
 
14
 
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
 
# pragma once
16
 
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
 
 
18
 
#include "asio/detail/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <algorithm>
22
 
#include <limits>
23
 
#include <memory>
24
 
#include <stdexcept>
25
 
#include <streambuf>
26
 
#include <vector>
27
 
#include "asio/detail/pop_options.hpp"
28
 
 
29
 
#include "asio/buffer.hpp"
30
 
#include "asio/detail/noncopyable.hpp"
31
 
 
32
 
namespace asio {
33
 
 
34
 
/// Automatically resizable buffer class based on std::streambuf.
35
 
template <typename Allocator = std::allocator<char> >
36
 
class basic_streambuf
37
 
  : public std::streambuf,
38
 
    private noncopyable
39
 
{
40
 
public:
41
 
#if defined(GENERATING_DOCUMENTATION)
42
 
  /// The type used to represent the get area as a list of buffers.
43
 
  typedef implementation_defined const_buffers_type;
44
 
 
45
 
  /// The type used to represent the put area as a list of buffers.
46
 
  typedef implementation_defined mutable_buffers_type;
47
 
#else
48
 
  typedef asio::const_buffers_1 const_buffers_type;
49
 
  typedef asio::mutable_buffers_1 mutable_buffers_type;
50
 
#endif
51
 
 
52
 
  /// Construct a buffer with a specified maximum size.
53
 
  explicit basic_streambuf(
54
 
      std::size_t max_size = (std::numeric_limits<std::size_t>::max)(),
55
 
      const Allocator& allocator = Allocator())
56
 
    : max_size_(max_size),
57
 
      buffer_(allocator)
58
 
  {
59
 
    std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);
60
 
    buffer_.resize((std::max<std::size_t>)(pend, 1));
61
 
    setg(&buffer_[0], &buffer_[0], &buffer_[0]);
62
 
    setp(&buffer_[0], &buffer_[0] + pend);
63
 
  }
64
 
 
65
 
  /// Return the size of the get area in characters.
66
 
  std::size_t size() const
67
 
  {
68
 
    return pptr() - gptr();
69
 
  }
70
 
 
71
 
  /// Return the maximum size of the buffer.
72
 
  std::size_t max_size() const
73
 
  {
74
 
    return max_size_;
75
 
  }
76
 
 
77
 
  /// Get a list of buffers that represents the get area.
78
 
  const_buffers_type data() const
79
 
  {
80
 
    return asio::buffer(asio::const_buffer(gptr(),
81
 
          (pptr() - gptr()) * sizeof(char_type)));
82
 
  }
83
 
 
84
 
  /// Get a list of buffers that represents the put area, with the given size.
85
 
  mutable_buffers_type prepare(std::size_t size)
86
 
  {
87
 
    reserve(size);
88
 
    return asio::buffer(asio::mutable_buffer(
89
 
          pptr(), size * sizeof(char_type)));
90
 
  }
91
 
 
92
 
  /// Move the start of the put area by the specified number of characters.
93
 
  void commit(std::size_t n)
94
 
  {
95
 
    if (pptr() + n > epptr())
96
 
      n = epptr() - pptr();
97
 
    pbump(static_cast<int>(n));
98
 
  }
99
 
 
100
 
  /// Move the start of the get area by the specified number of characters.
101
 
  void consume(std::size_t n)
102
 
  {
103
 
    if (gptr() + n > pptr())
104
 
      n = pptr() - gptr();
105
 
    gbump(static_cast<int>(n));
106
 
  }
107
 
 
108
 
protected:
109
 
  enum { buffer_delta = 128 };
110
 
 
111
 
  int_type underflow()
112
 
  {
113
 
    if (gptr() < pptr())
114
 
    {
115
 
      setg(&buffer_[0], gptr(), pptr());
116
 
      return traits_type::to_int_type(*gptr());
117
 
    }
118
 
    else
119
 
    {
120
 
      return traits_type::eof();
121
 
    }
122
 
  }
123
 
 
124
 
  int_type overflow(int_type c)
125
 
  {
126
 
    if (!traits_type::eq_int_type(c, traits_type::eof()))
127
 
    {
128
 
      if (pptr() == epptr())
129
 
      {
130
 
        std::size_t buffer_size = pptr() - gptr();
131
 
        if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
132
 
        {
133
 
          reserve(max_size_ - buffer_size);
134
 
        }
135
 
        else
136
 
        {
137
 
          reserve(buffer_delta);
138
 
        }
139
 
      }
140
 
 
141
 
      *pptr() = traits_type::to_char_type(c);
142
 
      pbump(1);
143
 
      return c;
144
 
    }
145
 
 
146
 
    return traits_type::not_eof(c);
147
 
  }
148
 
 
149
 
  void reserve(std::size_t n)
150
 
  {
151
 
    // Get current stream positions as offsets.
152
 
    std::size_t gnext = gptr() - &buffer_[0];
153
 
    std::size_t gend = egptr() - &buffer_[0];
154
 
    std::size_t pnext = pptr() - &buffer_[0];
155
 
    std::size_t pend = epptr() - &buffer_[0];
156
 
 
157
 
    // Check if there is already enough space in the put area.
158
 
    if (n <= pend - pnext)
159
 
    {
160
 
      return;
161
 
    }
162
 
 
163
 
    // Shift existing contents of get area to start of buffer.
164
 
    if (gnext > 0)
165
 
    {
166
 
      std::rotate(&buffer_[0], &buffer_[0] + gnext, &buffer_[0] + pend);
167
 
      gend -= gnext;
168
 
      pnext -= gnext;
169
 
    }
170
 
 
171
 
    // Ensure buffer is large enough to hold at least the specified size.
172
 
    if (n > pend - pnext)
173
 
    {
174
 
      if (n <= max_size_ && pnext <= max_size_ - n)
175
 
      {
176
 
        buffer_.resize((std::max<std::size_t>)(pnext + n, 1));
177
 
      }
178
 
      else
179
 
      {
180
 
        throw std::length_error("asio::streambuf too long");
181
 
      }
182
 
    }
183
 
 
184
 
    // Update stream positions.
185
 
    setg(&buffer_[0], &buffer_[0], &buffer_[0] + gend);
186
 
    setp(&buffer_[0] + pnext, &buffer_[0] + pnext + n);
187
 
  }
188
 
 
189
 
private:
190
 
  std::size_t max_size_;
191
 
  std::vector<char_type, Allocator> buffer_;
192
 
};
193
 
 
194
 
} // namespace asio
195
 
 
196
 
#include "asio/detail/pop_options.hpp"
197
 
 
198
 
#endif // ASIO_BASIC_STREAMBUF_HPP