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

Unified Diff: base/memory/ref_counted_memory.h

Issue 7397021: Re-land r93365 - add RefCountedString (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased onto r93582 Created 9 years, 5 months 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 side-by-side diff with in-line comments
Download patch
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..0d48f64e3e39593d95e8be6f9ac607b8881de771 100644
--- a/base/memory/ref_counted_memory.h
+++ b/base/memory/ref_counted_memory.h
@@ -6,9 +6,11 @@
#define BASE_MEMORY_REF_COUNTED_MEMORY_H_
#pragma once
+#include <string>
#include <vector>
#include "base/base_api.h"
+#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
// TODO(erg): The contents of this file should be in a namespace. This would
@@ -40,9 +42,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;
bulach 2011/07/22 11:16:54 nit: OVERRIDE here and below
virtual size_t size() const;
@@ -67,18 +69,51 @@ class BASE_API RefCountedBytes : public RefCountedMemory {
// vector.)
static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
- // Overriden from RefCountedMemory:
- virtual const unsigned char* front() const;
- virtual size_t size() const;
+ // Overridden from RefCountedMemory:
+ virtual const unsigned char* front() const OVERRIDE;
+ virtual size_t size() const OVERRIDE;
- std::vector<unsigned char> data;
+ const std::vector<unsigned char>& data() const { return 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 OVERRIDE;
+ virtual size_t size() const OVERRIDE;
+
+ const std::string& data() const { return data_; }
+ std::string& data() { return data_; }
+
+ private:
+ friend class base::RefCountedThreadSafe<RefCountedString>;
+ virtual ~RefCountedString();
+
+ std::string data_;
+
+ DISALLOW_COPY_AND_ASSIGN(RefCountedString);
+};
+
+} // namespace base
+
#endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_

Powered by Google App Engine
This is Rietveld 408576698