bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef GrTemplates_DEFINED |
| 9 | #define GrTemplates_DEFINED |
| 10 | |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 11 | #include "SkTypes.h" |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * Use to cast a ptr to a different type, and maintain strict-aliasing |
| 15 | */ |
| 16 | template <typename Dst, typename Src> Dst GrTCast(Src src) { |
| 17 | union { |
| 18 | Src src; |
| 19 | Dst dst; |
| 20 | } data; |
| 21 | data.src = src; |
| 22 | return data.dst; |
| 23 | } |
| 24 | |
| 25 | /** |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 26 | * takes a T*, saves the value it points to, in and restores the value in the |
| 27 | * destructor |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 28 | * e.g.: |
| 29 | * { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 30 | * GrAutoTRestore<int*> autoCountRestore; |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 31 | * if (useExtra) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 32 | * autoCountRestore.reset(&fCount); |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 33 | * fCount += fExtraCount; |
| 34 | * } |
| 35 | * ... |
| 36 | * } // fCount is restored |
| 37 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 38 | template <typename T> class GrAutoTRestore : SkNoncopyable { |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 39 | public: |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 40 | GrAutoTRestore() : fPtr(NULL), fVal() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 42 | GrAutoTRestore(T* ptr) { |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 43 | fPtr = ptr; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 44 | if (ptr) { |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 45 | fVal = *ptr; |
| 46 | } |
| 47 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 48 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 49 | ~GrAutoTRestore() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 50 | if (fPtr) { |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 51 | *fPtr = fVal; |
| 52 | } |
| 53 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 55 | // restores previously saved value (if any) and saves value for passed T* |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 56 | void reset(T* ptr) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 57 | if (fPtr) { |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 58 | *fPtr = fVal; |
| 59 | } |
| 60 | fPtr = ptr; |
| 61 | fVal = *ptr; |
| 62 | } |
| 63 | private: |
| 64 | T* fPtr; |
| 65 | T fVal; |
| 66 | }; |
| 67 | |
agl@chromium.org | 8374725 | 2011-04-27 23:11:21 +0000 | [diff] [blame] | 68 | #endif |