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/logging.h" | |
9 #include "base/ref_counted.h" | 8 #include "base/ref_counted.h" |
10 | 9 |
11 namespace net { | 10 namespace net { |
12 | 11 |
13 // This is a simple wrapper around a buffer that provides ref counting for | 12 // This is a simple wrapper around a buffer that provides ref counting for |
14 // easier asynchronous IO handling. | 13 // easier asynchronous IO handling. |
15 class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> { | 14 class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> { |
16 public: | 15 public: |
17 IOBuffer() : data_(NULL) {} | 16 IOBuffer() : data_(NULL) {} |
18 explicit IOBuffer(int buffer_size) { | 17 explicit IOBuffer(int buffer_size); |
19 DCHECK(buffer_size); | |
20 data_ = new char[buffer_size]; | |
21 } | |
22 explicit IOBuffer(char* data) : data_(data) {} | 18 explicit IOBuffer(char* data) : data_(data) {} |
23 virtual ~IOBuffer() { | 19 virtual ~IOBuffer() { |
24 delete[] data_; | 20 delete[] data_; |
25 } | 21 } |
26 | 22 |
27 char* data() { return data_; } | 23 char* data() { return data_; } |
28 | 24 |
29 protected: | 25 protected: |
30 char* data_; | 26 char* data_; |
31 }; | 27 }; |
32 | 28 |
33 // This class allows the creation of a temporary IOBuffer that doesn't really | 29 // This class allows the creation of a temporary IOBuffer that doesn't really |
34 // own the underlying buffer. Please use this class only as a last resort. | 30 // own the underlying buffer. Please use this class only as a last resort. |
35 // A good example is the buffer for a synchronous operation, where we can be | 31 // A good example is the buffer for a synchronous operation, where we can be |
36 // sure that nobody is keeping an extra reference to this object so the lifetime | 32 // sure that nobody is keeping an extra reference to this object so the lifetime |
37 // of the buffer can be completely managed by its intended owner. | 33 // of the buffer can be completely managed by its intended owner. |
38 class WrappedIOBuffer : public net::IOBuffer { | 34 class WrappedIOBuffer : public net::IOBuffer { |
39 public: | 35 public: |
40 explicit WrappedIOBuffer(const char* data) | 36 explicit WrappedIOBuffer(const char* data) |
41 : net::IOBuffer(const_cast<char*>(data)) {} | 37 : net::IOBuffer(const_cast<char*>(data)) {} |
42 ~WrappedIOBuffer() { | 38 ~WrappedIOBuffer() { |
43 data_ = NULL; | 39 data_ = NULL; |
44 } | 40 } |
45 }; | 41 }; |
46 | 42 |
47 } // namespace net | 43 } // namespace net |
48 | 44 |
49 #endif // NET_BASE_IO_BUFFER_H_ | 45 #endif // NET_BASE_IO_BUFFER_H_ |
OLD | NEW |