OLD | NEW |
---|---|
1 | |
2 /* | 1 /* |
3 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
4 * | 3 * |
5 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 5 * found in the LICENSE file. |
7 */ | 6 */ |
8 | 7 |
9 | |
10 | |
11 #ifndef SkData_DEFINED | 8 #ifndef SkData_DEFINED |
12 #define SkData_DEFINED | 9 #define SkData_DEFINED |
13 | 10 |
14 #include "SkFlattenable.h" | 11 #include "SkFlattenable.h" |
15 | 12 |
16 struct SkFILE; | 13 struct SkFILE; |
17 | 14 |
18 /** | 15 /** |
19 * SkData holds an immutable data buffer. Not only is the data immutable, | 16 * SkData holds an immutable data buffer. Not only is the data immutable, |
20 * but the actual ptr that is returned (by data() or bytes()) is guaranteed | 17 * but the actual ptr that is returned (by data() or bytes()) is guaranteed |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 */ | 49 */ |
53 size_t copyRange(size_t offset, size_t length, void* buffer) const; | 50 size_t copyRange(size_t offset, size_t length, void* buffer) const; |
54 | 51 |
55 /** | 52 /** |
56 * Returns true if these two objects have the same length and contents, | 53 * Returns true if these two objects have the same length and contents, |
57 * effectively returning 0 == memcmp(...) | 54 * effectively returning 0 == memcmp(...) |
58 */ | 55 */ |
59 bool equals(const SkData* other) const; | 56 bool equals(const SkData* other) const; |
60 | 57 |
61 /** | 58 /** |
62 * Function that, if provided, will be called when the SkData goes out | |
63 * of scope, allowing for custom allocation/freeing of the data. | |
64 */ | |
65 typedef void (*ReleaseProc)(const void* ptr, size_t length, void* context); | |
66 | |
67 /** | |
68 * Create a new dataref by copying the specified data | 59 * Create a new dataref by copying the specified data |
69 */ | 60 */ |
70 static SkData* NewWithCopy(const void* data, size_t length); | 61 static SkData* NewWithCopy(const void* data, size_t length); |
71 | 62 |
72 /** | 63 /** |
73 * Create a new dataref by copying the specified c-string | 64 * Create a new dataref by copying the specified c-string |
74 * (a null-terminated array of bytes). The returned SkData will have size() | 65 * (a null-terminated array of bytes). The returned SkData will have size() |
75 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same | 66 * equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same |
76 * as "". | 67 * as "". |
77 */ | 68 */ |
78 static SkData* NewWithCString(const char cstr[]); | 69 static SkData* NewWithCString(const char cstr[]); |
79 | 70 |
80 /** | 71 /** |
81 * Create a new dataref, taking the data ptr as is, and using the | 72 * Creates a new SkData which takes ownership of the data, using the |
82 * releaseproc to free it. The proc may be NULL. | 73 * releaseProc to free it. |
74 * | |
75 * The releaseProc must be of a type where | |
76 * releaseProc(const void* ptr, size_t length); | |
77 * is valid. | |
83 */ | 78 */ |
84 static SkData* NewWithProc(const void* data, size_t length, | 79 template <typename T> |
85 ReleaseProc proc, void* context); | 80 static SkData* NewWithProc(const void* data, size_t length, T releaseProc); |
86 | 81 |
87 /** | 82 /** |
88 * Create a new dataref from a pointer allocated by malloc. The Data object | 83 * Create a new dataref from a pointer allocated by malloc. The Data object |
89 * takes ownership of that allocation, and will handling calling sk_free. | 84 * takes ownership of that allocation, and will handling calling sk_free. |
90 */ | 85 */ |
91 static SkData* NewFromMalloc(const void* data, size_t length); | 86 static SkData* NewFromMalloc(const void* data, size_t length); |
92 | 87 |
93 /** | 88 /** |
94 * Create a new dataref the file with the specified path. | 89 * Create a new dataref the file with the specified path. |
95 * If the file cannot be opened, this returns NULL. | 90 * If the file cannot be opened, this returns NULL. |
(...skipping 23 matching lines...) Expand all Loading... | |
119 * src dataref. | 114 * src dataref. |
120 */ | 115 */ |
121 static SkData* NewSubset(const SkData* src, size_t offset, size_t length); | 116 static SkData* NewSubset(const SkData* src, size_t offset, size_t length); |
122 | 117 |
123 /** | 118 /** |
124 * Returns a new empty dataref (or a reference to a shared empty dataref). | 119 * Returns a new empty dataref (or a reference to a shared empty dataref). |
125 * New or shared, the caller must see that unref() is eventually called. | 120 * New or shared, the caller must see that unref() is eventually called. |
126 */ | 121 */ |
127 static SkData* NewEmpty(); | 122 static SkData* NewEmpty(); |
128 | 123 |
129 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData) | 124 /** |
125 * Creates an SkData which does not delete the data. | |
126 * Such an SkData is just a pointer and length. | |
reed1
2013/06/21 19:00:36
Not sure what this 2nd sentence means.
"The calle
| |
127 */ | |
128 static SkData* NewUnowned(const void* data, size_t length); | |
reed1
2013/06/21 19:00:36
NewUnmanaged() ?
maybe unowned is better.
I presu
| |
129 | |
130 //SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(SkData) | |
131 virtual Factory getFactory() SK_OVERRIDE { return CreateProc; } | |
132 static SkFlattenable* CreateProc(SkFlattenableReadBuffer& buffer); | |
130 | 133 |
131 protected: | 134 protected: |
132 SkData(SkFlattenableReadBuffer&); | 135 SkData(SkFlattenableReadBuffer&); |
133 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; | 136 virtual void flatten(SkFlattenableWriteBuffer&) const SK_OVERRIDE; |
134 | 137 |
135 private: | 138 private: |
136 ReleaseProc fReleaseProc; | 139 size_t const fSize; |
137 void* fReleaseProcContext; | 140 void const * const fPtr; |
138 | 141 |
139 const void* fPtr; | 142 SkData(const void* ptr, size_t size); |
140 size_t fSize; | |
141 | 143 |
142 SkData(const void* ptr, size_t size, ReleaseProc, void* context); | 144 template <typename Release, typename Deleter> friend class SkManagedData; |
143 virtual ~SkData(); | |
144 | |
145 typedef SkFlattenable INHERITED; | 145 typedef SkFlattenable INHERITED; |
146 }; | 146 }; |
147 | 147 |
148 namespace SkManagedDataProcs { | |
149 typedef void (*ReleaseProc)(const void* ptr, size_t length); | |
150 typedef void (*DeleterProc)(const void* obj); | |
151 } | |
152 | |
153 /** | |
154 * The Release and Deleter types must allow | |
155 * | |
156 * release(const void* data, size_t length); | |
157 * deleter(const void* obj); | |
158 * | |
159 * to be valid, respectively. If these are function pointers, | |
160 * Release and Deleter should be specified to have one of the types in | |
161 * SkManagedDataProcs. | |
162 */ | |
163 template <typename Release, typename Deleter> | |
164 class SkManagedData : public SkData { | |
165 public: | |
166 /** | |
167 * Constructs an SkManagedData at the given memory. | |
168 * When destroyed, release(ptr, size) will be called. | |
169 * When the reference count goes to zero, deleter(memory) will be called. | |
170 */ | |
171 static SkData* Place(void* memory, const void* data, size_t length, | |
172 Release release, Deleter deleter) { | |
173 return SkNEW_PLACEMENT_ARGS(memory, SkManagedData, ( | |
174 data, length, release, deleter)); | |
175 } | |
176 | |
177 private: | |
178 SkManagedData(const void* p, size_t size, Release release, Deleter deleter) | |
179 : SkData(p, size), fRelease(release), fDeleter(deleter) | |
180 { } | |
181 | |
182 virtual ~SkManagedData() { | |
183 fRelease(fPtr, fSize); | |
184 } | |
185 | |
186 /** Called when the reference count goes to zero. */ | |
187 virtual void internal_dispose() const SK_OVERRIDE { | |
188 Deleter deleter = fDeleter; | |
189 this->internal_dispose_restore_refcnt_to_1(); | |
190 this->~SkData(); | |
191 deleter(this); | |
192 return; | |
193 } | |
194 | |
195 Release const fRelease; | |
196 Deleter const fDeleter; | |
197 | |
198 typedef SkData INHERITED; | |
199 }; | |
200 | |
201 template <typename Deleter> class SkManagedData<void, Deleter> : public SkData { | |
202 public: | |
203 /** | |
204 * Constructs an SkManagedData at the given memory. | |
205 * When the reference count goes to zero, deleter(memory) will be called. | |
206 */ | |
207 static SkData* Place(void* memory, const void* data, size_t length, | |
208 Deleter deleter) | |
209 { | |
210 return SkNEW_PLACEMENT_ARGS(memory, SkManagedData, ( | |
211 data, length, deleter)); | |
212 } | |
213 | |
214 private: | |
215 SkManagedData(const void* ptr, size_t size, Deleter deleter) | |
216 : SkData(ptr, size), fDeleter(deleter) | |
217 { } | |
218 | |
219 /** Called when the reference count goes to zero. */ | |
220 virtual void internal_dispose() const SK_OVERRIDE { | |
221 Deleter deleter = fDeleter; | |
222 this->internal_dispose_restore_refcnt_to_1(); | |
223 this->~SkData(); | |
224 deleter(this); | |
225 return; | |
226 } | |
227 | |
228 Deleter const fDeleter; | |
229 | |
230 typedef SkData INHERITED; | |
231 }; | |
232 | |
233 template <typename Release> class SkManagedData<Release, void> : public SkData { | |
234 /** When destroyed, release(ptr, size) will be called. */ | |
235 SkManagedData(const void* ptr, size_t size, Release release) | |
236 : SkData(ptr, size), fRelease(release) | |
237 { } | |
238 | |
239 virtual ~SkManagedData() { | |
240 fRelease(fPtr, fSize); | |
241 } | |
242 | |
243 Release const fRelease; | |
244 | |
245 typedef SkData INHERITED; | |
246 friend class SkData; | |
247 }; | |
248 | |
249 template <typename T> | |
250 SkData* SkData::NewWithProc(const void* data, size_t length, T releaseProc) { | |
251 return new SkManagedData<T, void>(data, length, releaseProc); | |
252 } | |
253 | |
148 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ | 254 /** Typedef of SkAutoTUnref<SkData> for automatically unref-ing a SkData. */ |
149 typedef SkAutoTUnref<SkData> SkAutoDataUnref; | 255 typedef SkAutoTUnref<SkData> SkAutoDataUnref; |
150 | 256 |
151 #endif | 257 #endif |
OLD | NEW |