Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: net/base/io_buffer.h

Issue 368001: Second patch in making destructors of refcounted objects private. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 8 #include <string>
9 9
10 #include "base/ref_counted.h" 10 #include "base/ref_counted.h"
11 #include "base/scoped_ptr.h" 11 #include "base/scoped_ptr.h"
12 12
13 namespace net { 13 namespace net {
14 14
15 // This is a simple wrapper around a buffer that provides ref counting for 15 // This is a simple wrapper around a buffer that provides ref counting for
16 // easier asynchronous IO handling. 16 // easier asynchronous IO handling.
17 class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> { 17 class IOBuffer : public base::RefCountedThreadSafe<IOBuffer> {
18 public: 18 public:
19 IOBuffer() : data_(NULL) {} 19 IOBuffer() : data_(NULL) {}
20 explicit IOBuffer(int buffer_size); 20 explicit IOBuffer(int buffer_size);
21
22 char* data() { return data_; }
23
24 protected:
25 friend class base::RefCountedThreadSafe<IOBuffer>;
26
27 // Only allow derived classes to specify data_.
28 // In all other cases, we own data_, and must delete it at destruction time.
29 explicit IOBuffer(char* data) : data_(data) {}
30
21 virtual ~IOBuffer() { 31 virtual ~IOBuffer() {
22 delete[] data_; 32 delete[] data_;
23 } 33 }
24 34
25 char* data() { return data_; }
26
27 protected:
28 // Only allow derived classes to specify data_.
29 // In all other cases, we own data_, and must delete it at destruction time.
30 explicit IOBuffer(char* data) : data_(data) {}
31 char* data_; 35 char* data_;
32 }; 36 };
33 37
34 // This version stores the size of the buffer so that the creator of the object 38 // This version stores the size of the buffer so that the creator of the object
35 // doesn't have to keep track of that value. 39 // doesn't have to keep track of that value.
36 // NOTE: This doesn't mean that we want to stop sending the size as an explicit 40 // NOTE: This doesn't mean that we want to stop sending the size as an explicit
37 // argument to IO functions. Please keep using IOBuffer* for API declarations. 41 // argument to IO functions. Please keep using IOBuffer* for API declarations.
38 class IOBufferWithSize : public IOBuffer { 42 class IOBufferWithSize : public IOBuffer {
39 public: 43 public:
40 explicit IOBufferWithSize(int size) : IOBuffer(size), size_(size) {} 44 explicit IOBufferWithSize(int size) : IOBuffer(size), size_(size) {}
41 ~IOBufferWithSize() {}
42 45
43 int size() const { return size_; } 46 int size() const { return size_; }
44 47
45 private: 48 private:
49 ~IOBufferWithSize() {}
50
46 int size_; 51 int size_;
47 }; 52 };
48 53
49 // This is a read only IOBuffer. The data is stored in a string and 54 // This is a read only IOBuffer. The data is stored in a string and
50 // the IOBuffer interface does not provide a proper way to modify it. 55 // the IOBuffer interface does not provide a proper way to modify it.
51 class StringIOBuffer : public IOBuffer { 56 class StringIOBuffer : public IOBuffer {
52 public: 57 public:
53 explicit StringIOBuffer(const std::string& s) 58 explicit StringIOBuffer(const std::string& s)
54 : IOBuffer(static_cast<char*>(NULL)), 59 : IOBuffer(static_cast<char*>(NULL)),
55 string_data_(s) { 60 string_data_(s) {
56 data_ = const_cast<char*>(string_data_.data()); 61 data_ = const_cast<char*>(string_data_.data());
57 } 62 }
63
64 int size() const { return string_data_.size(); }
65
66 private:
58 ~StringIOBuffer() { 67 ~StringIOBuffer() {
59 // We haven't allocated the buffer, so remove it before the base class 68 // We haven't allocated the buffer, so remove it before the base class
60 // destructor tries to delete[] it. 69 // destructor tries to delete[] it.
61 data_ = NULL; 70 data_ = NULL;
62 } 71 }
63 72
64 int size() const { return string_data_.size(); }
65
66 private:
67 std::string string_data_; 73 std::string string_data_;
68 }; 74 };
69 75
70 // This version wraps an existing IOBuffer and provides convenient functions 76 // This version wraps an existing IOBuffer and provides convenient functions
71 // to progressively read all the data. 77 // to progressively read all the data.
72 class DrainableIOBuffer : public IOBuffer { 78 class DrainableIOBuffer : public IOBuffer {
73 public: 79 public:
74 DrainableIOBuffer(IOBuffer* base, int size) 80 DrainableIOBuffer(IOBuffer* base, int size)
75 : IOBuffer(base->data()), base_(base), size_(size), used_(0) {} 81 : IOBuffer(base->data()), base_(base), size_(size), used_(0) {}
76 ~DrainableIOBuffer() {
77 // The buffer is owned by the |base_| instance.
78 data_ = NULL;
79 }
80 82
81 // DidConsume() changes the |data_| pointer so that |data_| always points 83 // DidConsume() changes the |data_| pointer so that |data_| always points
82 // to the first unconsumed byte. 84 // to the first unconsumed byte.
83 void DidConsume(int bytes) { SetOffset(used_ + bytes); } 85 void DidConsume(int bytes) { SetOffset(used_ + bytes); }
84 86
85 // Returns the number of unconsumed bytes. 87 // Returns the number of unconsumed bytes.
86 int BytesRemaining() const { return size_ - used_; } 88 int BytesRemaining() const { return size_ - used_; }
87 89
88 // Returns the number of consumed bytes. 90 // Returns the number of consumed bytes.
89 int BytesConsumed() const { return used_; } 91 int BytesConsumed() const { return used_; }
90 92
91 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed 93 // Seeks to an arbitrary point in the buffer. The notion of bytes consumed
92 // and remaining are updated appropriately. 94 // and remaining are updated appropriately.
93 void SetOffset(int bytes); 95 void SetOffset(int bytes);
94 96
95 int size() const { return size_; } 97 int size() const { return size_; }
96 98
97 private: 99 private:
100 ~DrainableIOBuffer() {
101 // The buffer is owned by the |base_| instance.
102 data_ = NULL;
103 }
104
98 scoped_refptr<IOBuffer> base_; 105 scoped_refptr<IOBuffer> base_;
99 int size_; 106 int size_;
100 int used_; 107 int used_;
101 }; 108 };
102 109
103 // This version provides a resizable buffer and a changeable offset. 110 // This version provides a resizable buffer and a changeable offset.
104 class GrowableIOBuffer : public IOBuffer { 111 class GrowableIOBuffer : public IOBuffer {
105 public: 112 public:
106 GrowableIOBuffer() : IOBuffer(), capacity_(0), offset_(0) {} 113 GrowableIOBuffer() : IOBuffer(), capacity_(0), offset_(0) {}
107 ~GrowableIOBuffer() { data_ = NULL; }
108 114
109 // realloc memory to the specified capacity. Returns true on success. 115 // realloc memory to the specified capacity. Returns true on success.
110 // On failure, the capacity and buffer are unchanged. 116 // On failure, the capacity and buffer are unchanged.
111 bool SetCapacity(int capacity); 117 bool SetCapacity(int capacity);
112 int capacity() { return capacity_; } 118 int capacity() { return capacity_; }
113 119
114 // |offset| moves the |data_| pointer, allowing "seeking" in the data. 120 // |offset| moves the |data_| pointer, allowing "seeking" in the data.
115 void set_offset(int offset); 121 void set_offset(int offset);
116 int offset() { return offset_; } 122 int offset() { return offset_; }
117 123
118 int RemainingCapacity() { return capacity_ - offset_; } 124 int RemainingCapacity() { return capacity_ - offset_; }
119 char* StartOfBuffer() { return real_data_.get(); } 125 char* StartOfBuffer() { return real_data_.get(); }
120 126
121 private: 127 private:
128 ~GrowableIOBuffer() { data_ = NULL; }
129
122 scoped_ptr_malloc<char> real_data_; 130 scoped_ptr_malloc<char> real_data_;
123 int capacity_; 131 int capacity_;
124 int offset_; 132 int offset_;
125 }; 133 };
126 134
127 // This class allows the creation of a temporary IOBuffer that doesn't really 135 // This class allows the creation of a temporary IOBuffer that doesn't really
128 // own the underlying buffer. Please use this class only as a last resort. 136 // own the underlying buffer. Please use this class only as a last resort.
129 // A good example is the buffer for a synchronous operation, where we can be 137 // A good example is the buffer for a synchronous operation, where we can be
130 // sure that nobody is keeping an extra reference to this object so the lifetime 138 // sure that nobody is keeping an extra reference to this object so the lifetime
131 // of the buffer can be completely managed by its intended owner. 139 // of the buffer can be completely managed by its intended owner.
132 class WrappedIOBuffer : public IOBuffer { 140 class WrappedIOBuffer : public IOBuffer {
133 public: 141 public:
134 explicit WrappedIOBuffer(const char* data) 142 explicit WrappedIOBuffer(const char* data)
135 : IOBuffer(const_cast<char*>(data)) {} 143 : IOBuffer(const_cast<char*>(data)) {}
144
145 protected:
136 ~WrappedIOBuffer() { 146 ~WrappedIOBuffer() {
137 data_ = NULL; 147 data_ = NULL;
138 } 148 }
139 }; 149 };
140 150
141 } // namespace net 151 } // namespace net
142 152
143 #endif // NET_BASE_IO_BUFFER_H_ 153 #endif // NET_BASE_IO_BUFFER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698