OLD | NEW |
---|---|
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" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 | 14 |
14 // TODO(erg): The contents of this file should be in a namespace. This would | 15 // TODO(erg): The contents of this file should be in a namespace. This would |
15 // require touching >100 files in chrome/ though. | 16 // require touching >100 files in chrome/ though. |
16 | 17 |
17 // A generic interface to memory. This object is reference counted because one | 18 // 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 | 19 // of its two subclasses own the data they carry, and we need to have |
(...skipping 14 matching lines...) Expand all Loading... | |
33 virtual ~RefCountedMemory(); | 34 virtual ~RefCountedMemory(); |
34 }; | 35 }; |
35 | 36 |
36 // An implementation of RefCountedMemory, where the ref counting does not | 37 // An implementation of RefCountedMemory, where the ref counting does not |
37 // matter. | 38 // matter. |
38 class BASE_API RefCountedStaticMemory : public RefCountedMemory { | 39 class BASE_API RefCountedStaticMemory : public RefCountedMemory { |
39 public: | 40 public: |
40 RefCountedStaticMemory() | 41 RefCountedStaticMemory() |
41 : data_(NULL), length_(0) {} | 42 : data_(NULL), length_(0) {} |
42 RefCountedStaticMemory(const unsigned char* data, size_t length) | 43 RefCountedStaticMemory(const unsigned char* data, size_t length) |
43 : data_(data), length_(length) {} | 44 : data_(length ? data : NULL), length_(length) {} |
44 | 45 |
45 // Overriden from RefCountedMemory: | 46 // Overridden from RefCountedMemory: |
46 virtual const unsigned char* front() const; | 47 virtual const unsigned char* front() const; |
47 virtual size_t size() const; | 48 virtual size_t size() const; |
48 | 49 |
49 private: | 50 private: |
50 const unsigned char* data_; | 51 const unsigned char* data_; |
51 size_t length_; | 52 size_t length_; |
52 | 53 |
53 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); | 54 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); |
54 }; | 55 }; |
55 | 56 |
56 // An implementation of RefCountedMemory, where we own our the data in a | 57 // An implementation of RefCountedMemory, where we own our the data in a |
57 // vector. | 58 // vector. |
58 class BASE_API RefCountedBytes : public RefCountedMemory { | 59 class BASE_API RefCountedBytes : public RefCountedMemory { |
59 public: | 60 public: |
60 RefCountedBytes(); | 61 RefCountedBytes(); |
61 | 62 |
62 // Constructs a RefCountedBytes object by _copying_ from |initializer|. | 63 // Constructs a RefCountedBytes object by _copying_ from |initializer|. |
63 RefCountedBytes(const std::vector<unsigned char>& initializer); | 64 RefCountedBytes(const std::vector<unsigned char>& initializer); |
64 | 65 |
65 // Constructs a RefCountedBytes object by performing a swap. (To non | 66 // Constructs a RefCountedBytes object by performing a swap. (To non |
66 // destructively build a RefCountedBytes, use the constructor that takes a | 67 // destructively build a RefCountedBytes, use the constructor that takes a |
67 // vector.) | 68 // vector.) |
68 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); | 69 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); |
69 | 70 |
70 // Overriden from RefCountedMemory: | 71 // Overridden from RefCountedMemory: |
71 virtual const unsigned char* front() const; | 72 virtual const unsigned char* front() const; |
72 virtual size_t size() const; | 73 virtual size_t size() const; |
73 | 74 |
74 std::vector<unsigned char> data; | 75 std::vector<unsigned char>* data() { return &data_; } |
75 | 76 |
76 protected: | 77 private: |
77 friend class base::RefCountedThreadSafe<RefCountedBytes>; | 78 friend class base::RefCountedThreadSafe<RefCountedBytes>; |
78 virtual ~RefCountedBytes(); | 79 virtual ~RefCountedBytes(); |
79 | 80 |
80 private: | 81 std::vector<unsigned char> data_; |
82 | |
81 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); | 83 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
82 }; | 84 }; |
83 | 85 |
86 namespace base { | |
87 | |
88 // An implementation of RefCountedMemory, where the bytes are stored in an STL | |
89 // string. Use this if your data naturally arrives in that format. | |
90 class BASE_API RefCountedString : public RefCountedMemory { | |
91 public: | |
92 RefCountedString(); | |
93 | |
94 // Constructs a RefCountedString object by performing a swap. (To non | |
95 // destructively build a RefCountedString, use the default constructor and | |
96 // copy into object->data()). | |
97 static RefCountedString* TakeString(std::string* to_destroy); | |
98 | |
99 // Overridden from RefCountedMemory: | |
100 virtual const unsigned char* front() const; | |
101 virtual size_t size() const; | |
102 | |
103 std::string* data() { return &data_; } | |
brettw
2011/07/19 19:58:45
Is there any reason not to return this as a ref? I
joth
2011/07/20 12:53:03
I was overpowered by the "references must be const
| |
104 | |
105 private: | |
106 friend class base::RefCountedThreadSafe<RefCountedString>; | |
107 virtual ~RefCountedString(); | |
108 | |
109 std::string data_; | |
110 | |
111 DISALLOW_COPY_AND_ASSIGN(RefCountedString); | |
112 }; | |
113 | |
114 } // namespace | |
115 | |
84 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ | 116 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ |
OLD | NEW |