~ubuntu-branches/ubuntu/trusty/miro/trusty

« back to all changes in this revision

Viewing changes to portable/libtorrent/include/libtorrent/asio/detail/const_buffers_iterator.hpp

  • Committer: Daniel Hahler
  • Date: 2010-04-13 18:51:35 UTC
  • mfrom: (1.2.10 upstream)
  • Revision ID: ubuntu-launchpad@thequod.de-20100413185135-xi24v1diqg8w406x
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// const_buffers_iterator.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_DETAIL_CONST_BUFFERS_ITERATOR_HPP
12
 
#define ASIO_DETAIL_CONST_BUFFERS_ITERATOR_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 <cstddef>
22
 
#include <boost/config.hpp>
23
 
#include <boost/iterator/iterator_facade.hpp>
24
 
#include "asio/detail/pop_options.hpp"
25
 
 
26
 
#include "asio/buffer.hpp"
27
 
 
28
 
namespace asio {
29
 
namespace detail {
30
 
 
31
 
// A proxy iterator for a sub-range in a list of buffers.
32
 
template <typename ConstBufferSequence>
33
 
class const_buffers_iterator
34
 
  : public boost::iterator_facade<const_buffers_iterator<ConstBufferSequence>,
35
 
        const char, boost::bidirectional_traversal_tag>
36
 
{
37
 
public:
38
 
  // Default constructor creates an iterator in an undefined state.
39
 
  const_buffers_iterator()
40
 
  {
41
 
  }
42
 
 
43
 
  // Create an iterator for the specified position.
44
 
  const_buffers_iterator(const ConstBufferSequence& buffers,
45
 
      std::size_t position)
46
 
    : begin_(buffers.begin()),
47
 
      current_(buffers.begin()),
48
 
      end_(buffers.end()),
49
 
      position_(0)
50
 
  {
51
 
    while (current_ != end_)
52
 
    {
53
 
      current_buffer_ = *current_;
54
 
      std::size_t buffer_size = asio::buffer_size(current_buffer_);
55
 
      if (position - position_ < buffer_size)
56
 
      {
57
 
        current_buffer_position_ = position - position_;
58
 
        position_ = position;
59
 
        return;
60
 
      }
61
 
      position_ += buffer_size;
62
 
      ++current_;
63
 
    }
64
 
    current_buffer_ = asio::const_buffer();
65
 
    current_buffer_position_ = 0;
66
 
  }
67
 
 
68
 
  std::size_t position() const
69
 
  {
70
 
    return position_;
71
 
  }
72
 
 
73
 
private:
74
 
  friend class boost::iterator_core_access;
75
 
 
76
 
  void increment()
77
 
  {
78
 
    if (current_ == end_)
79
 
      return;
80
 
 
81
 
    ++position_;
82
 
 
83
 
    ++current_buffer_position_;
84
 
    if (current_buffer_position_ != asio::buffer_size(current_buffer_))
85
 
      return;
86
 
 
87
 
    ++current_;
88
 
    current_buffer_position_ = 0;
89
 
    while (current_ != end_)
90
 
    {
91
 
      current_buffer_ = *current_;
92
 
      if (asio::buffer_size(current_buffer_) > 0)
93
 
        return;
94
 
      ++current_;
95
 
    }
96
 
  }
97
 
 
98
 
  void decrement()
99
 
  {
100
 
    if (position_ == 0)
101
 
      return;
102
 
 
103
 
    --position_;
104
 
 
105
 
    if (current_buffer_position_ != 0)
106
 
    {
107
 
      --current_buffer_position_;
108
 
      return;
109
 
    }
110
 
 
111
 
    typename ConstBufferSequence::const_iterator iter = current_;
112
 
    while (iter != begin_)
113
 
    {
114
 
      --iter;
115
 
      asio::const_buffer buffer = *iter;
116
 
      std::size_t buffer_size = asio::buffer_size(buffer);
117
 
      if (buffer_size > 0)
118
 
      {
119
 
        current_ = iter;
120
 
        current_buffer_ = buffer;
121
 
        current_buffer_position_ = buffer_size - 1;
122
 
        return;
123
 
      }
124
 
    }
125
 
  }
126
 
 
127
 
  bool equal(const const_buffers_iterator& other) const
128
 
  {
129
 
    return position_ == other.position_;
130
 
  }
131
 
 
132
 
  const char& dereference() const
133
 
  {
134
 
    return asio::buffer_cast<const char*>(
135
 
        current_buffer_)[current_buffer_position_];
136
 
  }
137
 
 
138
 
  asio::const_buffer current_buffer_;
139
 
  std::size_t current_buffer_position_;
140
 
  typename ConstBufferSequence::const_iterator begin_;
141
 
  typename ConstBufferSequence::const_iterator current_;
142
 
  typename ConstBufferSequence::const_iterator end_;
143
 
  std::size_t position_;
144
 
};
145
 
 
146
 
} // namespace detail
147
 
} // namespace asio
148
 
 
149
 
#include "asio/detail/pop_options.hpp"
150
 
 
151
 
#endif // ASIO_DETAIL_CONST_BUFFERS_ITERATOR_HPP