Index: base/memory/ref_counted_memory.h |
diff --git a/base/memory/ref_counted_memory.h b/base/memory/ref_counted_memory.h |
index 1a0f51eec4aebf8784f7ed02e6b91c5964fb18c5..d917d54e7ad20c37cdc9c9085ad40894014d396b 100644 |
--- a/base/memory/ref_counted_memory.h |
+++ b/base/memory/ref_counted_memory.h |
@@ -6,6 +6,7 @@ |
#define BASE_MEMORY_REF_COUNTED_MEMORY_H_ |
#pragma once |
+#include <string> |
#include <vector> |
#include "base/base_api.h" |
@@ -40,9 +41,9 @@ class BASE_API RefCountedStaticMemory : public RefCountedMemory { |
RefCountedStaticMemory() |
: data_(NULL), length_(0) {} |
RefCountedStaticMemory(const unsigned char* data, size_t length) |
- : data_(data), length_(length) {} |
+ : data_(length ? data : NULL), length_(length) {} |
- // Overriden from RefCountedMemory: |
+ // Overridden from RefCountedMemory: |
virtual const unsigned char* front() const; |
virtual size_t size() const; |
@@ -67,18 +68,49 @@ class BASE_API RefCountedBytes : public RefCountedMemory { |
// vector.) |
static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); |
- // Overriden from RefCountedMemory: |
+ // Overridden from RefCountedMemory: |
virtual const unsigned char* front() const; |
virtual size_t size() const; |
- std::vector<unsigned char> data; |
+ std::vector<unsigned char>* data() { return &data_; } |
- protected: |
+ private: |
friend class base::RefCountedThreadSafe<RefCountedBytes>; |
virtual ~RefCountedBytes(); |
- private: |
+ std::vector<unsigned char> data_; |
+ |
DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
}; |
+namespace base { |
+ |
+// An implementation of RefCountedMemory, where the bytes are stored in an STL |
+// string. Use this if your data naturally arrives in that format. |
+class BASE_API RefCountedString : public RefCountedMemory { |
+ public: |
+ RefCountedString(); |
+ |
+ // Constructs a RefCountedString object by performing a swap. (To non |
+ // destructively build a RefCountedString, use the default constructor and |
+ // copy into object->data()). |
+ static RefCountedString* TakeString(std::string* to_destroy); |
+ |
+ // Overridden from RefCountedMemory: |
+ virtual const unsigned char* front() const; |
+ virtual size_t size() const; |
+ |
+ std::string* data() { return &data_; } |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<RefCountedString>; |
+ virtual ~RefCountedString(); |
+ |
+ std::string data_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RefCountedString); |
+}; |
+ |
+} // namespace |
brettw
2011/07/19 19:58:45
"namespace base"
joth
2011/07/20 12:53:03
Done.
|
+ |
#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ |