OLD | NEW |
1 | |
2 /* | 1 /* |
3 * Copyright 2010 Google Inc. | 2 * Copyright 2010 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 #ifndef GrTemplates_DEFINED | 8 #ifndef GrTemplates_DEFINED |
11 #define GrTemplates_DEFINED | 9 #define GrTemplates_DEFINED |
12 | 10 |
13 #include "GrNoncopyable.h" | 11 #include "SkTypes.h" |
14 | 12 |
15 /** | 13 /** |
16 * Use to cast a ptr to a different type, and maintain strict-aliasing | 14 * Use to cast a ptr to a different type, and maintain strict-aliasing |
17 */ | 15 */ |
18 template <typename Dst, typename Src> Dst GrTCast(Src src) { | 16 template <typename Dst, typename Src> Dst GrTCast(Src src) { |
19 union { | 17 union { |
20 Src src; | 18 Src src; |
21 Dst dst; | 19 Dst dst; |
22 } data; | 20 } data; |
23 data.src = src; | 21 data.src = src; |
24 return data.dst; | 22 return data.dst; |
25 } | 23 } |
26 | 24 |
27 /** | 25 /** |
28 * takes a T*, saves the value it points to, in and restores the value in the | 26 * takes a T*, saves the value it points to, in and restores the value in the |
29 * destructor | 27 * destructor |
30 * e.g.: | 28 * e.g.: |
31 * { | 29 * { |
32 * GrAutoTRestore<int*> autoCountRestore; | 30 * GrAutoTRestore<int*> autoCountRestore; |
33 * if (useExtra) { | 31 * if (useExtra) { |
34 * autoCountRestore.reset(&fCount); | 32 * autoCountRestore.reset(&fCount); |
35 * fCount += fExtraCount; | 33 * fCount += fExtraCount; |
36 * } | 34 * } |
37 * ... | 35 * ... |
38 * } // fCount is restored | 36 * } // fCount is restored |
39 */ | 37 */ |
40 template <typename T> class GrAutoTRestore : public GrNoncopyable { | 38 template <typename T> class GrAutoTRestore : public SkNoncopyable { |
41 public: | 39 public: |
42 GrAutoTRestore() : fPtr(NULL), fVal() {} | 40 GrAutoTRestore() : fPtr(NULL), fVal() {} |
43 | 41 |
44 GrAutoTRestore(T* ptr) { | 42 GrAutoTRestore(T* ptr) { |
45 fPtr = ptr; | 43 fPtr = ptr; |
46 if (NULL != ptr) { | 44 if (NULL != ptr) { |
47 fVal = *ptr; | 45 fVal = *ptr; |
48 } | 46 } |
49 } | 47 } |
50 | 48 |
(...skipping 10 matching lines...) Expand all Loading... |
61 } | 59 } |
62 fPtr = ptr; | 60 fPtr = ptr; |
63 fVal = *ptr; | 61 fVal = *ptr; |
64 } | 62 } |
65 private: | 63 private: |
66 T* fPtr; | 64 T* fPtr; |
67 T fVal; | 65 T fVal; |
68 }; | 66 }; |
69 | 67 |
70 #endif | 68 #endif |
OLD | NEW |