OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_BASE_IO_BUFFER_H_ | 5 #ifndef NET_BASE_IO_BUFFER_H_ |
6 #define NET_BASE_IO_BUFFER_H_ | 6 #define NET_BASE_IO_BUFFER_H_ |
7 | 7 |
8 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 char* data() { return data_; } | 22 char* data() { return data_; } |
23 | 23 |
24 protected: | 24 protected: |
25 // Only allow derived classes to specify data_. | 25 // Only allow derived classes to specify data_. |
26 // In all other cases, we own data_, and must delete it at destruction time. | 26 // In all other cases, we own data_, and must delete it at destruction time. |
27 explicit IOBuffer(char* data) : data_(data) {} | 27 explicit IOBuffer(char* data) : data_(data) {} |
28 char* data_; | 28 char* data_; |
29 }; | 29 }; |
30 | 30 |
| 31 // This version stores the size of the buffer so that the creator of the object |
| 32 // doesn't have to keep track of that value. |
| 33 // NOTE: This doesn't mean that we want to stop sending the size as an explictit |
| 34 // argument to IO functions. Please keep using IOBuffer* for API declarations. |
| 35 class IOBufferWithSize : public IOBuffer { |
| 36 public: |
| 37 explicit IOBufferWithSize(int size) : IOBuffer(size), size_(size) {} |
| 38 ~IOBufferWithSize() {} |
| 39 |
| 40 int size() const { return size_; } |
| 41 |
| 42 private: |
| 43 int size_; |
| 44 }; |
| 45 |
| 46 // This version allows the caller to do multiple IO operations reusing a given |
| 47 // IOBuffer. We don't own data_, we simply make it point to the buffer of the |
| 48 // passed in IOBuffer, plus the desired offset. |
| 49 class ReusedIOBuffer : public IOBuffer { |
| 50 public: |
| 51 ReusedIOBuffer(IOBuffer* base, int size) |
| 52 : IOBuffer(base->data()), base_(base), size_(size) {} |
| 53 ~ReusedIOBuffer() { |
| 54 // We don't really own a buffer. |
| 55 data_ = NULL; |
| 56 } |
| 57 |
| 58 int size() const { return size_; } |
| 59 void SetOffset(int offset); |
| 60 |
| 61 private: |
| 62 scoped_refptr<IOBuffer> base_; |
| 63 int size_; |
| 64 }; |
| 65 |
31 // This class allows the creation of a temporary IOBuffer that doesn't really | 66 // This class allows the creation of a temporary IOBuffer that doesn't really |
32 // own the underlying buffer. Please use this class only as a last resort. | 67 // own the underlying buffer. Please use this class only as a last resort. |
33 // A good example is the buffer for a synchronous operation, where we can be | 68 // A good example is the buffer for a synchronous operation, where we can be |
34 // sure that nobody is keeping an extra reference to this object so the lifetime | 69 // sure that nobody is keeping an extra reference to this object so the lifetime |
35 // of the buffer can be completely managed by its intended owner. | 70 // of the buffer can be completely managed by its intended owner. |
36 class WrappedIOBuffer : public net::IOBuffer { | 71 class WrappedIOBuffer : public IOBuffer { |
37 public: | 72 public: |
38 explicit WrappedIOBuffer(const char* data) | 73 explicit WrappedIOBuffer(const char* data) |
39 : net::IOBuffer(const_cast<char*>(data)) {} | 74 : IOBuffer(const_cast<char*>(data)) {} |
40 ~WrappedIOBuffer() { | 75 ~WrappedIOBuffer() { |
41 data_ = NULL; | 76 data_ = NULL; |
42 } | 77 } |
43 }; | 78 }; |
44 | 79 |
45 } // namespace net | 80 } // namespace net |
46 | 81 |
47 #endif // NET_BASE_IO_BUFFER_H_ | 82 #endif // NET_BASE_IO_BUFFER_H_ |
OLD | NEW |