3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #ifndef SkTemplates_DEFINED
11 #define SkTemplates_DEFINED
16 /** \file SkTemplates.h
18 This file contains light-weight template classes for type-safe and exception-safe
23 * Marks a local variable as known to be unused (to avoid warnings).
24 * Note that this does *not* prevent the local variable from being optimized away.
26 template<typename T> inline void sk_ignore_unused_variable(const T&) { }
29 * SkTIsConst<T>::value is true if the type T is const.
30 * The type T is constrained not to be an array or reference type.
32 template <typename T> struct SkTIsConst {
34 static uint16_t test(const volatile void*);
35 static uint32_t test(volatile void *);
36 static const bool value = (sizeof(uint16_t) == sizeof(test(t)));
40 /** SkTConstType<T, CONST>::type will be 'const T' if CONST is true, 'T' otherwise. */
41 template <typename T, bool CONST> struct SkTConstType {
44 template <typename T> struct SkTConstType<T, true> {
50 * Returns a pointer to a D which comes immediately after S[count].
52 template <typename D, typename S> static D* SkTAfter(S* ptr, size_t count = 1) {
53 return reinterpret_cast<D*>(ptr + count);
57 * Returns a pointer to a D which comes byteOffset bytes after S.
59 template <typename D, typename S> static D* SkTAddOffset(S* ptr, size_t byteOffset) {
60 // The intermediate char* has the same const-ness as D as this produces better error messages.
61 // This relies on the fact that reinterpret_cast can add constness, but cannot remove it.
62 return reinterpret_cast<D*>(
63 reinterpret_cast<typename SkTConstType<char, SkTIsConst<D>::value>::type*>(ptr) + byteOffset
67 /** \class SkAutoTCallVProc
69 Call a function when this goes out of scope. The template uses two
70 parameters, the object, and a function that is to be called in the destructor.
71 If detach() is called, the object reference is set to null. If the object
72 reference is null when the destructor is called, we do not call the
75 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable {
77 SkAutoTCallVProc(T* obj): fObj(obj) {}
78 ~SkAutoTCallVProc() { if (fObj) P(fObj); }
79 T* detach() { T* obj = fObj; fObj = NULL; return obj; }
84 /** \class SkAutoTCallIProc
86 Call a function when this goes out of scope. The template uses two
87 parameters, the object, and a function that is to be called in the destructor.
88 If detach() is called, the object reference is set to null. If the object
89 reference is null when the destructor is called, we do not call the
92 template <typename T, int (*P)(T*)> class SkAutoTCallIProc : SkNoncopyable {
94 SkAutoTCallIProc(T* obj): fObj(obj) {}
95 ~SkAutoTCallIProc() { if (fObj) P(fObj); }
96 T* detach() { T* obj = fObj; fObj = NULL; return obj; }
101 /** \class SkAutoTDelete
102 An SkAutoTDelete<T> is like a T*, except that the destructor of SkAutoTDelete<T>
103 automatically deletes the pointer it holds (if any). That is, SkAutoTDelete<T>
104 owns the T object that it points to. Like a T*, an SkAutoTDelete<T> may hold
105 either NULL or a pointer to a T object. Also like T*, SkAutoTDelete<T> is
106 thread-compatible, and once you dereference it, you get the threadsafety
109 The size of a SkAutoTDelete is small: sizeof(SkAutoTDelete<T>) == sizeof(T*)
111 template <typename T> class SkAutoTDelete : SkNoncopyable {
113 SkAutoTDelete(T* obj = NULL) : fObj(obj) {}
114 ~SkAutoTDelete() { SkDELETE(fObj); }
116 T* get() const { return fObj; }
117 T& operator*() const { SkASSERT(fObj); return *fObj; }
118 T* operator->() const { SkASSERT(fObj); return fObj; }
128 * Delete the owned object, setting the internal pointer to NULL.
136 * Transfer ownership of the object to the caller, setting the internal
137 * pointer to NULL. Note that this differs from get(), which also returns
138 * the pointer, but it does not transfer ownership.
146 void swap(SkAutoTDelete* that) {
147 SkTSwap(fObj, that->fObj);
154 // Calls ~T() in the destructor.
155 template <typename T> class SkAutoTDestroy : SkNoncopyable {
157 SkAutoTDestroy(T* obj = NULL) : fObj(obj) {}
164 T* get() const { return fObj; }
165 T& operator*() const { SkASSERT(fObj); return *fObj; }
166 T* operator->() const { SkASSERT(fObj); return fObj; }
172 template <typename T> class SkAutoTDeleteArray : SkNoncopyable {
174 SkAutoTDeleteArray(T array[]) : fArray(array) {}
175 ~SkAutoTDeleteArray() { SkDELETE_ARRAY(fArray); }
177 T* get() const { return fArray; }
178 void free() { SkDELETE_ARRAY(fArray); fArray = NULL; }
179 T* detach() { T* array = fArray; fArray = NULL; return array; }
181 void reset(T array[]) {
182 if (fArray != array) {
183 SkDELETE_ARRAY(fArray);
192 /** Allocate an array of T elements, and free the array in the destructor
194 template <typename T> class SkAutoTArray : SkNoncopyable {
198 SkDEBUGCODE(fCount = 0;)
200 /** Allocate count number of T elements
202 explicit SkAutoTArray(int count) {
203 SkASSERT(count >= 0);
206 fArray = SkNEW_ARRAY(T, count);
208 SkDEBUGCODE(fCount = count;)
211 /** Reallocates given a new count. Reallocation occurs even if new count equals old count.
213 void reset(int count) {
214 SkDELETE_ARRAY(fArray);
215 SkASSERT(count >= 0);
218 fArray = SkNEW_ARRAY(T, count);
220 SkDEBUGCODE(fCount = count;)
224 SkDELETE_ARRAY(fArray);
227 /** Return the array of T elements. Will be NULL if count == 0
229 T* get() const { return fArray; }
231 /** Return the nth element in the array
233 T& operator[](int index) const {
234 SkASSERT((unsigned)index < (unsigned)fCount);
235 return fArray[index];
240 SkDEBUGCODE(int fCount;)
243 /** Wraps SkAutoTArray, with room for up to N elements preallocated
245 template <int N, typename T> class SkAutoSTArray : SkNoncopyable {
247 /** Initialize with no objects */
253 /** Allocate count number of T elements
255 SkAutoSTArray(int count) {
265 /** Destroys previous objects in the array and default constructs count number of objects */
266 void reset(int count) {
268 T* iter = start + fCount;
269 while (iter > start) {
273 if (fCount != count) {
275 // 'fArray' was allocated last time so free it now
276 SkASSERT((T*) fStorage != fArray);
281 fArray = (T*) sk_malloc_throw(count * sizeof(T));
282 } else if (count > 0) {
283 fArray = (T*) fStorage;
292 T* stop = fArray + count;
293 while (iter < stop) {
294 SkNEW_PLACEMENT(iter++, T);
298 /** Return the number of T elements in the array
300 int count() const { return fCount; }
302 /** Return the array of T elements. Will be NULL if count == 0
304 T* get() const { return fArray; }
306 /** Return the nth element in the array
308 T& operator[](int index) const {
309 SkASSERT(index < fCount);
310 return fArray[index];
316 // since we come right after fArray, fStorage should be properly aligned
317 char fStorage[N * sizeof(T)];
320 /** Manages an array of T elements, freeing the array in the destructor.
321 * Does NOT call any constructors/destructors on T (T must be POD).
323 template <typename T> class SkAutoTMalloc : SkNoncopyable {
325 /** Takes ownership of the ptr. The ptr must be a value which can be passed to sk_free. */
326 explicit SkAutoTMalloc(T* ptr = NULL) {
330 /** Allocates space for 'count' Ts. */
331 explicit SkAutoTMalloc(size_t count) {
332 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
339 /** Resize the memory area pointed to by the current ptr preserving contents. */
340 void realloc(size_t count) {
341 fPtr = reinterpret_cast<T*>(sk_realloc_throw(fPtr, count * sizeof(T)));
344 /** Resize the memory area pointed to by the current ptr without preserving contents. */
345 void reset(size_t count) {
347 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
350 T* get() const { return fPtr; }
356 operator const T*() const {
360 T& operator[](int index) {
364 const T& operator[](int index) const {
369 * Transfer ownership of the ptr to the caller, setting the internal
370 * pointer to NULL. Note that this differs from get(), which also returns
371 * the pointer, but it does not transfer ownership.
383 template <size_t N, typename T> class SkAutoSTMalloc : SkNoncopyable {
389 SkAutoSTMalloc(size_t count) {
391 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
400 if (fPtr != fTStorage) {
405 // doesn't preserve contents
406 T* reset(size_t count) {
407 if (fPtr != fTStorage) {
411 fPtr = (T*)sk_malloc_flags(count * sizeof(T), SK_MALLOC_THROW | SK_MALLOC_TEMP);
420 T* get() const { return fPtr; }
426 operator const T*() const {
430 T& operator[](int index) {
434 const T& operator[](int index) const {
441 uint32_t fStorage32[(N*sizeof(T) + 3) >> 2];
442 T fTStorage[1]; // do NOT want to invoke T::T()
447 * Reserves memory that is aligned on double and pointer boundaries.
448 * Hopefully this is sufficient for all practical purposes.
450 template <size_t N> class SkAlignedSStorage : SkNoncopyable {
452 void* get() { return fData; }
462 * Reserves memory that is aligned on double and pointer boundaries.
463 * Hopefully this is sufficient for all practical purposes. Otherwise,
464 * we have to do some arcane trickery to determine alignment of non-POD
465 * types. Lifetime of the memory is the lifetime of the object.
467 template <int N, typename T> class SkAlignedSTStorage : SkNoncopyable {
470 * Returns void* because this object does not initialize the
471 * memory. Use placement new for types that require a cons.
473 void* get() { return fStorage.get(); }
475 SkAlignedSStorage<sizeof(T)*N> fStorage;