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

Side by Side 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: rebase again 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/debug/trace_event.cc ('k') | base/memory/ref_counted_memory.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 BASE_MEMORY_REF_COUNTED_MEMORY_H_ 5 #ifndef BASE_MEMORY_REF_COUNTED_MEMORY_H_
6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_ 6 #define BASE_MEMORY_REF_COUNTED_MEMORY_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/base_api.h" 12 #include "base/base_api.h"
13 #include "base/compiler_specific.h"
12 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
13 15
14 // TODO(erg): The contents of this file should be in a namespace. This would 16 // TODO(erg): The contents of this file should be in a namespace. This would
15 // require touching >100 files in chrome/ though. 17 // require touching >100 files in chrome/ though.
16 18
17 // A generic interface to memory. This object is reference counted because one 19 // A generic interface to memory. This object is reference counted because one
18 // of its two subclasses own the data they carry, and we need to have 20 // of its two subclasses own the data they carry, and we need to have
19 // heterogeneous containers of these two types of memory. 21 // heterogeneous containers of these two types of memory.
20 class BASE_API RefCountedMemory 22 class BASE_API RefCountedMemory
21 : public base::RefCountedThreadSafe<RefCountedMemory> { 23 : public base::RefCountedThreadSafe<RefCountedMemory> {
(...skipping 11 matching lines...) Expand all
33 virtual ~RefCountedMemory(); 35 virtual ~RefCountedMemory();
34 }; 36 };
35 37
36 // An implementation of RefCountedMemory, where the ref counting does not 38 // An implementation of RefCountedMemory, where the ref counting does not
37 // matter. 39 // matter.
38 class BASE_API RefCountedStaticMemory : public RefCountedMemory { 40 class BASE_API RefCountedStaticMemory : public RefCountedMemory {
39 public: 41 public:
40 RefCountedStaticMemory() 42 RefCountedStaticMemory()
41 : data_(NULL), length_(0) {} 43 : data_(NULL), length_(0) {}
42 RefCountedStaticMemory(const unsigned char* data, size_t length) 44 RefCountedStaticMemory(const unsigned char* data, size_t length)
43 : data_(data), length_(length) {} 45 : data_(length ? data : NULL), length_(length) {}
44 46
45 // Overriden from RefCountedMemory: 47 // Overridden from RefCountedMemory:
46 virtual const unsigned char* front() const; 48 virtual const unsigned char* front() const OVERRIDE;
47 virtual size_t size() const; 49 virtual size_t size() const OVERRIDE;
48 50
49 private: 51 private:
50 const unsigned char* data_; 52 const unsigned char* data_;
51 size_t length_; 53 size_t length_;
52 54
53 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); 55 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory);
54 }; 56 };
55 57
56 // An implementation of RefCountedMemory, where we own our the data in a 58 // An implementation of RefCountedMemory, where we own our the data in a
57 // vector. 59 // vector.
58 class BASE_API RefCountedBytes : public RefCountedMemory { 60 class BASE_API RefCountedBytes : public RefCountedMemory {
59 public: 61 public:
60 RefCountedBytes(); 62 RefCountedBytes();
61 63
62 // Constructs a RefCountedBytes object by _copying_ from |initializer|. 64 // Constructs a RefCountedBytes object by _copying_ from |initializer|.
63 RefCountedBytes(const std::vector<unsigned char>& initializer); 65 RefCountedBytes(const std::vector<unsigned char>& initializer);
64 66
65 // Constructs a RefCountedBytes object by performing a swap. (To non 67 // Constructs a RefCountedBytes object by performing a swap. (To non
66 // destructively build a RefCountedBytes, use the constructor that takes a 68 // destructively build a RefCountedBytes, use the constructor that takes a
67 // vector.) 69 // vector.)
68 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); 70 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy);
69 71
70 // Overriden from RefCountedMemory: 72 // Overridden from RefCountedMemory:
71 virtual const unsigned char* front() const; 73 virtual const unsigned char* front() const OVERRIDE;
72 virtual size_t size() const; 74 virtual size_t size() const OVERRIDE;
73 75
74 std::vector<unsigned char> data; 76 const std::vector<unsigned char>& data() const { return data_; }
77 std::vector<unsigned char>& data() { return data_; }
75 78
76 protected: 79 private:
77 friend class base::RefCountedThreadSafe<RefCountedBytes>; 80 friend class base::RefCountedThreadSafe<RefCountedBytes>;
78 virtual ~RefCountedBytes(); 81 virtual ~RefCountedBytes();
79 82
80 private: 83 std::vector<unsigned char> data_;
84
81 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); 85 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes);
82 }; 86 };
83 87
88 namespace base {
89
90 // An implementation of RefCountedMemory, where the bytes are stored in an STL
91 // string. Use this if your data naturally arrives in that format.
92 class BASE_API RefCountedString : public RefCountedMemory {
93 public:
94 RefCountedString();
95
96 // Constructs a RefCountedString object by performing a swap. (To non
97 // destructively build a RefCountedString, use the default constructor and
98 // copy into object->data()).
99 static RefCountedString* TakeString(std::string* to_destroy);
100
101 // Overridden from RefCountedMemory:
102 virtual const unsigned char* front() const OVERRIDE;
103 virtual size_t size() const OVERRIDE;
104
105 const std::string& data() const { return data_; }
106 std::string& data() { return data_; }
107
108 private:
109 friend class base::RefCountedThreadSafe<RefCountedString>;
110 virtual ~RefCountedString();
111
112 std::string data_;
113
114 DISALLOW_COPY_AND_ASSIGN(RefCountedString);
115 };
116
117 } // namespace base
118
84 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ 119 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_
OLDNEW
« no previous file with comments | « base/debug/trace_event.cc ('k') | base/memory/ref_counted_memory.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698