Realistic 3D camera system
3D camera system components
buffered_stream_storage.hpp
Go to the documentation of this file.
1 //
2 // detail/buffered_stream_storage.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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_BUFFERED_STREAM_STORAGE_HPP
12 #define ASIO_DETAIL_BUFFERED_STREAM_STORAGE_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/config.hpp"
19 #include "asio/buffer.hpp"
20 #include "asio/detail/assert.hpp"
21 #include <cstddef>
22 #include <cstring>
23 #include <vector>
24 
26 
27 namespace asio {
28 namespace detail {
29 
31 {
32 public:
33  // The type of the bytes stored in the buffer.
34  typedef unsigned char byte_type;
35 
36  // The type used for offsets into the buffer.
37  typedef std::size_t size_type;
38 
39  // Constructor.
40  explicit buffered_stream_storage(std::size_t buffer_capacity)
41  : begin_offset_(0),
42  end_offset_(0),
43  buffer_(buffer_capacity)
44  {
45  }
46 
48  void clear()
49  {
50  begin_offset_ = 0;
51  end_offset_ = 0;
52  }
53 
54  // Return a pointer to the beginning of the unread data.
56  {
57  return asio::buffer(buffer_) + begin_offset_;
58  }
59 
60  // Return a pointer to the beginning of the unread data.
62  {
63  return asio::buffer(buffer_) + begin_offset_;
64  }
65 
66  // Is there no unread data in the buffer.
67  bool empty() const
68  {
69  return begin_offset_ == end_offset_;
70  }
71 
72  // Return the amount of unread data the is in the buffer.
73  size_type size() const
74  {
75  return end_offset_ - begin_offset_;
76  }
77 
78  // Resize the buffer to the specified length.
79  void resize(size_type length)
80  {
81  ASIO_ASSERT(length <= capacity());
82  if (begin_offset_ + length <= capacity())
83  {
84  end_offset_ = begin_offset_ + length;
85  }
86  else
87  {
88  using namespace std; // For memmove.
89  memmove(&buffer_[0], &buffer_[0] + begin_offset_, size());
90  end_offset_ = length;
91  begin_offset_ = 0;
92  }
93  }
94 
95  // Return the maximum size for data in the buffer.
96  size_type capacity() const
97  {
98  return buffer_.size();
99  }
100 
101  // Consume multiple bytes from the beginning of the buffer.
102  void consume(size_type count)
103  {
104  ASIO_ASSERT(begin_offset_ + count <= end_offset_);
105  begin_offset_ += count;
106  if (empty())
107  clear();
108  }
109 
110 private:
111  // The offset to the beginning of the unread data.
112  size_type begin_offset_;
113 
114  // The offset to the end of the unread data.
115  size_type end_offset_;
116 
117  // The data in the buffer.
118  std::vector<byte_type> buffer_;
119 };
120 
121 } // namespace detail
122 } // namespace asio
123 
125 
126 #endif // ASIO_DETAIL_BUFFERED_STREAM_STORAGE_HPP
#define ASIO_ASSERT(expr)
Definition: assert.hpp:29
Holds a buffer that cannot be modified.
Definition: buffer.hpp:211
buffered_stream_storage(std::size_t buffer_capacity)
STL namespace.
mutable_buffers_1 buffer(const mutable_buffer &b)
Create a new modifiable buffer from an existing buffer.
Definition: buffer.hpp:706
Holds a buffer that can be modified.
Definition: buffer.hpp:91