OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkTemplates_DEFINED | 10 #ifndef SkTemplates_DEFINED |
(...skipping 28 matching lines...) Expand all Loading... |
39 ///@{ | 39 ///@{ |
40 /** SkTConstType<T, CONST>::type will be 'const T' if CONST is true, 'T' otherwi
se. */ | 40 /** SkTConstType<T, CONST>::type will be 'const T' if CONST is true, 'T' otherwi
se. */ |
41 template <typename T, bool CONST> struct SkTConstType { | 41 template <typename T, bool CONST> struct SkTConstType { |
42 typedef T type; | 42 typedef T type; |
43 }; | 43 }; |
44 template <typename T> struct SkTConstType<T, true> { | 44 template <typename T> struct SkTConstType<T, true> { |
45 typedef const T type; | 45 typedef const T type; |
46 }; | 46 }; |
47 ///@} | 47 ///@} |
48 | 48 |
| 49 /** |
| 50 * Returns a pointer to a D which comes immediately after S[count]. |
| 51 */ |
| 52 template <typename D, typename S> static D* SkTAfter(S* ptr, size_t count = 1) { |
| 53 return reinterpret_cast<D*>(ptr + count); |
| 54 } |
| 55 |
| 56 /** |
| 57 * Returns a pointer to a D which comes byteOffset bytes after S. |
| 58 */ |
| 59 template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffs
et) { |
| 60 // The intermediate char* has the same const-ness as D as this produces bett
er error messages. |
| 61 // This relies on the fact that reinterpret_cast can add constness, but cann
ot remove it. |
| 62 return reinterpret_cast<D*>( |
| 63 reinterpret_cast<typename SkTConstType<char, SkTIsConst<D>::value>::type
*>(ptr) + byteOffset |
| 64 ); |
| 65 } |
| 66 |
49 /** \class SkAutoTCallVProc | 67 /** \class SkAutoTCallVProc |
50 | 68 |
51 Call a function when this goes out of scope. The template uses two | 69 Call a function when this goes out of scope. The template uses two |
52 parameters, the object, and a function that is to be called in the destructo
r. | 70 parameters, the object, and a function that is to be called in the destructo
r. |
53 If detach() is called, the object reference is set to null. If the object | 71 If detach() is called, the object reference is set to null. If the object |
54 reference is null when the destructor is called, we do not call the | 72 reference is null when the destructor is called, we do not call the |
55 function. | 73 function. |
56 */ | 74 */ |
57 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { | 75 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { |
58 public: | 76 public: |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 return fArray[index]; | 263 return fArray[index]; |
246 } | 264 } |
247 | 265 |
248 private: | 266 private: |
249 size_t fCount; | 267 size_t fCount; |
250 T* fArray; | 268 T* fArray; |
251 // since we come right after fArray, fStorage should be properly aligned | 269 // since we come right after fArray, fStorage should be properly aligned |
252 char fStorage[N * sizeof(T)]; | 270 char fStorage[N * sizeof(T)]; |
253 }; | 271 }; |
254 | 272 |
255 /** Allocate a temp array on the stack/heap. | 273 /** Manages an array of T elements, freeing the array in the destructor. |
256 Does NOT call any constructors/destructors on T (i.e. T must be POD) | 274 * Does NOT call any constructors/destructors on T (T must be POD). |
257 */ | 275 */ |
258 template <typename T> class SkAutoTMalloc : SkNoncopyable { | 276 template <typename T> class SkAutoTMalloc : SkNoncopyable { |
259 public: | 277 public: |
260 SkAutoTMalloc(size_t count) { | 278 /** Takes ownership of the ptr. The ptr must be a value which can be passed
to sk_free. */ |
| 279 explicit SkAutoTMalloc(T* ptr = NULL) { |
| 280 fPtr = ptr; |
| 281 } |
| 282 |
| 283 /** Allocates space for 'count' Ts. */ |
| 284 explicit SkAutoTMalloc(size_t count) { |
261 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLO
C_TEMP); | 285 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLO
C_TEMP); |
262 } | 286 } |
263 | 287 |
264 ~SkAutoTMalloc() { | 288 ~SkAutoTMalloc() { |
265 sk_free(fPtr); | 289 sk_free(fPtr); |
266 } | 290 } |
267 | 291 |
268 // doesn't preserve contents | 292 /** Resize the memory area pointed to by the current ptr preserving contents
. */ |
269 void reset (size_t count) { | 293 void realloc(size_t count) { |
| 294 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T))); |
| 295 } |
| 296 |
| 297 /** Resize the memory area pointed to by the current ptr without preserving
contents. */ |
| 298 void reset(size_t count) { |
270 sk_free(fPtr); | 299 sk_free(fPtr); |
271 fPtr = fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | S
K_MALLOC_TEMP); | 300 fPtr = fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | S
K_MALLOC_TEMP); |
272 } | 301 } |
273 | 302 |
274 T* get() const { return fPtr; } | 303 T* get() const { return fPtr; } |
275 | 304 |
276 operator T*() { | 305 operator T*() { |
277 return fPtr; | 306 return fPtr; |
278 } | 307 } |
279 | 308 |
280 operator const T*() const { | 309 operator const T*() const { |
281 return fPtr; | 310 return fPtr; |
282 } | 311 } |
283 | 312 |
284 T& operator[](int index) { | 313 T& operator[](int index) { |
285 return fPtr[index]; | 314 return fPtr[index]; |
286 } | 315 } |
287 | 316 |
288 const T& operator[](int index) const { | 317 const T& operator[](int index) const { |
289 return fPtr[index]; | 318 return fPtr[index]; |
290 } | 319 } |
291 | 320 |
| 321 /** |
| 322 * Transfer ownership of the ptr to the caller, setting the internal |
| 323 * pointer to NULL. Note that this differs from get(), which also returns |
| 324 * the pointer, but it does not transfer ownership. |
| 325 */ |
| 326 T* detach() { |
| 327 T* ptr = fPtr; |
| 328 fPtr = NULL; |
| 329 return ptr; |
| 330 } |
| 331 |
292 private: | 332 private: |
293 T* fPtr; | 333 T* fPtr; |
294 }; | 334 }; |
295 | 335 |
296 template <size_t N, typename T> class SK_API SkAutoSTMalloc : SkNoncopyable { | 336 template <size_t N, typename T> class SK_API SkAutoSTMalloc : SkNoncopyable { |
297 public: | 337 public: |
298 SkAutoSTMalloc() { | 338 SkAutoSTMalloc() { |
299 fPtr = NULL; | 339 fPtr = NULL; |
300 } | 340 } |
301 | 341 |
302 SkAutoSTMalloc(size_t count) { | 342 SkAutoSTMalloc(size_t count) { |
303 if (count > N) { | 343 if (count > N) { |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 /** | 421 /** |
382 * Returns void* because this object does not initialize the | 422 * Returns void* because this object does not initialize the |
383 * memory. Use placement new for types that require a cons. | 423 * memory. Use placement new for types that require a cons. |
384 */ | 424 */ |
385 void* get() { return fStorage.get(); } | 425 void* get() { return fStorage.get(); } |
386 private: | 426 private: |
387 SkAlignedSStorage<sizeof(T)*N> fStorage; | 427 SkAlignedSStorage<sizeof(T)*N> fStorage; |
388 }; | 428 }; |
389 | 429 |
390 #endif | 430 #endif |
OLD | NEW |