OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 RefCountedStaticMemory() | 44 RefCountedStaticMemory() |
45 : data_(NULL), length_(0) {} | 45 : data_(NULL), length_(0) {} |
46 RefCountedStaticMemory(const unsigned char* data, size_t length) | 46 RefCountedStaticMemory(const unsigned char* data, size_t length) |
47 : data_(length ? data : NULL), length_(length) {} | 47 : data_(length ? data : NULL), length_(length) {} |
48 | 48 |
49 // Overridden from RefCountedMemory: | 49 // Overridden from RefCountedMemory: |
50 virtual const unsigned char* front() const OVERRIDE; | 50 virtual const unsigned char* front() const OVERRIDE; |
51 virtual size_t size() const OVERRIDE; | 51 virtual size_t size() const OVERRIDE; |
52 | 52 |
53 private: | 53 private: |
| 54 virtual ~RefCountedStaticMemory(); |
| 55 |
54 const unsigned char* data_; | 56 const unsigned char* data_; |
55 size_t length_; | 57 size_t length_; |
56 | 58 |
57 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); | 59 DISALLOW_COPY_AND_ASSIGN(RefCountedStaticMemory); |
58 }; | 60 }; |
59 | 61 |
60 // An implementation of RefCountedMemory, where we own our the data in a | 62 // An implementation of RefCountedMemory, where we own our the data in a |
61 // vector. | 63 // vector. |
62 class BASE_EXPORT RefCountedBytes : public RefCountedMemory { | 64 class BASE_EXPORT RefCountedBytes : public RefCountedMemory { |
63 public: | 65 public: |
64 RefCountedBytes(); | 66 RefCountedBytes(); |
65 | 67 |
66 // Constructs a RefCountedBytes object by _copying_ from |initializer|. | 68 // Constructs a RefCountedBytes object by _copying_ from |initializer|. |
67 RefCountedBytes(const std::vector<unsigned char>& initializer); | 69 RefCountedBytes(const std::vector<unsigned char>& initializer); |
68 | 70 |
69 // Constructs a RefCountedBytes object by performing a swap. (To non | 71 // Constructs a RefCountedBytes object by performing a swap. (To non |
70 // destructively build a RefCountedBytes, use the constructor that takes a | 72 // destructively build a RefCountedBytes, use the constructor that takes a |
71 // vector.) | 73 // vector.) |
72 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); | 74 static RefCountedBytes* TakeVector(std::vector<unsigned char>* to_destroy); |
73 | 75 |
74 // Overridden from RefCountedMemory: | 76 // Overridden from RefCountedMemory: |
75 virtual const unsigned char* front() const OVERRIDE; | 77 virtual const unsigned char* front() const OVERRIDE; |
76 virtual size_t size() const OVERRIDE; | 78 virtual size_t size() const OVERRIDE; |
77 | 79 |
78 const std::vector<unsigned char>& data() const { return data_; } | 80 const std::vector<unsigned char>& data() const { return data_; } |
79 std::vector<unsigned char>& data() { return data_; } | 81 std::vector<unsigned char>& data() { return data_; } |
80 | 82 |
81 private: | 83 private: |
82 friend class base::RefCountedThreadSafe<RefCountedBytes>; | |
83 virtual ~RefCountedBytes(); | 84 virtual ~RefCountedBytes(); |
84 | 85 |
85 std::vector<unsigned char> data_; | 86 std::vector<unsigned char> data_; |
86 | 87 |
87 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); | 88 DISALLOW_COPY_AND_ASSIGN(RefCountedBytes); |
88 }; | 89 }; |
89 | 90 |
90 // An implementation of RefCountedMemory, where the bytes are stored in an STL | 91 // 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 // string. Use this if your data naturally arrives in that format. |
92 class BASE_EXPORT RefCountedString : public RefCountedMemory { | 93 class BASE_EXPORT RefCountedString : public RefCountedMemory { |
93 public: | 94 public: |
94 RefCountedString(); | 95 RefCountedString(); |
95 | 96 |
96 // Constructs a RefCountedString object by performing a swap. (To non | 97 // Constructs a RefCountedString object by performing a swap. (To non |
97 // destructively build a RefCountedString, use the default constructor and | 98 // destructively build a RefCountedString, use the default constructor and |
98 // copy into object->data()). | 99 // copy into object->data()). |
99 static RefCountedString* TakeString(std::string* to_destroy); | 100 static RefCountedString* TakeString(std::string* to_destroy); |
100 | 101 |
101 // Overridden from RefCountedMemory: | 102 // Overridden from RefCountedMemory: |
102 virtual const unsigned char* front() const OVERRIDE; | 103 virtual const unsigned char* front() const OVERRIDE; |
103 virtual size_t size() const OVERRIDE; | 104 virtual size_t size() const OVERRIDE; |
104 | 105 |
105 const std::string& data() const { return data_; } | 106 const std::string& data() const { return data_; } |
106 std::string& data() { return data_; } | 107 std::string& data() { return data_; } |
107 | 108 |
108 private: | 109 private: |
109 friend class base::RefCountedThreadSafe<RefCountedString>; | |
110 virtual ~RefCountedString(); | 110 virtual ~RefCountedString(); |
111 | 111 |
112 std::string data_; | 112 std::string data_; |
113 | 113 |
114 DISALLOW_COPY_AND_ASSIGN(RefCountedString); | 114 DISALLOW_COPY_AND_ASSIGN(RefCountedString); |
115 }; | 115 }; |
116 | 116 |
117 } // namespace base | 117 } // namespace base |
118 | 118 |
119 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ | 119 #endif // BASE_MEMORY_REF_COUNTED_MEMORY_H_ |
OLD | NEW |