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