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<SkTConstType<char, SkTIsConst<D>::value>::type*>(ptr) + byteOffset); | |
64 } | |
65 | |
49 /** \class SkAutoTCallVProc | 66 /** \class SkAutoTCallVProc |
50 | 67 |
51 Call a function when this goes out of scope. The template uses two | 68 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. | 69 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 | 70 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 | 71 reference is null when the destructor is called, we do not call the |
55 function. | 72 function. |
56 */ | 73 */ |
57 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { | 74 template <typename T, void (*P)(T*)> class SkAutoTCallVProc : SkNoncopyable { |
58 public: | 75 public: |
(...skipping 14 matching lines...) Expand all Loading... | |
73 */ | 90 */ |
74 template <typename T, int (*P)(T*)> class SkAutoTCallIProc : SkNoncopyable { | 91 template <typename T, int (*P)(T*)> class SkAutoTCallIProc : SkNoncopyable { |
75 public: | 92 public: |
76 SkAutoTCallIProc(T* obj): fObj(obj) {} | 93 SkAutoTCallIProc(T* obj): fObj(obj) {} |
77 ~SkAutoTCallIProc() { if (fObj) P(fObj); } | 94 ~SkAutoTCallIProc() { if (fObj) P(fObj); } |
78 T* detach() { T* obj = fObj; fObj = NULL; return obj; } | 95 T* detach() { T* obj = fObj; fObj = NULL; return obj; } |
79 private: | 96 private: |
80 T* fObj; | 97 T* fObj; |
81 }; | 98 }; |
82 | 99 |
100 template <typename T> class SkAutoTFree : SkNoncopyable { | |
101 public: | |
102 explicit SkAutoTFree(T* ptr = NULL) : fPtr(ptr) { } | |
103 ~SkAutoTFree() { sk_free(fPtr); } | |
104 | |
105 /** Return the current buffer, or null. */ | |
106 T* get() const { return fPtr; } | |
107 | |
108 /** Assign a new ptr allocated with sk_malloc (or null), and return the | |
109 previous ptr. Note it is the caller's responsibility to sk_free the | |
110 returned ptr. | |
111 */ | |
112 T* set(T* ptr) { | |
reed1
2013/05/06 18:09:58
Do we have this pattern (where we return the old a
bungeman-skia
2013/05/06 20:48:45
Hmmm... not really. This is mostly copy-pasta from
| |
113 T* prev = fPtr; | |
114 fPtr = ptr; | |
115 return prev; | |
116 } | |
117 | |
118 /** Transfer ownership of the current ptr to the caller, setting the | |
119 internal reference to null. Note the caller is reponsible for calling | |
120 sk_free on the returned address. | |
121 */ | |
122 T* detach() { return this->set(NULL); } | |
123 | |
124 /** Free the current buffer, and set the internal reference to NULL. Same | |
125 as calling sk_free(detach()) | |
126 */ | |
127 void free() { | |
128 sk_free(fPtr); | |
129 fPtr = NULL; | |
130 } | |
131 | |
132 private: | |
133 T* fPtr; | |
134 }; | |
135 | |
83 template <typename T> class SkAutoTDelete : SkNoncopyable { | 136 template <typename T> class SkAutoTDelete : SkNoncopyable { |
84 public: | 137 public: |
85 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} | 138 SkAutoTDelete(T* obj = NULL) : fObj(obj) {} |
86 ~SkAutoTDelete() { delete fObj; } | 139 ~SkAutoTDelete() { delete fObj; } |
87 | 140 |
88 T* get() const { return fObj; } | 141 T* get() const { return fObj; } |
89 T& operator*() const { SkASSERT(fObj); return *fObj; } | 142 T& operator*() const { SkASSERT(fObj); return *fObj; } |
90 T* operator->() const { SkASSERT(fObj); return fObj; } | 143 T* operator->() const { SkASSERT(fObj); return fObj; } |
91 | 144 |
92 void reset(T* obj) { | 145 void reset(T* obj) { |
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
381 /** | 434 /** |
382 * Returns void* because this object does not initialize the | 435 * Returns void* because this object does not initialize the |
383 * memory. Use placement new for types that require a cons. | 436 * memory. Use placement new for types that require a cons. |
384 */ | 437 */ |
385 void* get() { return fStorage.get(); } | 438 void* get() { return fStorage.get(); } |
386 private: | 439 private: |
387 SkAlignedSStorage<sizeof(T)*N> fStorage; | 440 SkAlignedSStorage<sizeof(T)*N> fStorage; |
388 }; | 441 }; |
389 | 442 |
390 #endif | 443 #endif |
OLD | NEW |