Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2010 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef GrTemplates_DEFINED |
michael@0 | 9 | #define GrTemplates_DEFINED |
michael@0 | 10 | |
michael@0 | 11 | #include "SkTypes.h" |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * Use to cast a ptr to a different type, and maintain strict-aliasing |
michael@0 | 15 | */ |
michael@0 | 16 | template <typename Dst, typename Src> Dst GrTCast(Src src) { |
michael@0 | 17 | union { |
michael@0 | 18 | Src src; |
michael@0 | 19 | Dst dst; |
michael@0 | 20 | } data; |
michael@0 | 21 | data.src = src; |
michael@0 | 22 | return data.dst; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | /** |
michael@0 | 26 | * takes a T*, saves the value it points to, in and restores the value in the |
michael@0 | 27 | * destructor |
michael@0 | 28 | * e.g.: |
michael@0 | 29 | * { |
michael@0 | 30 | * GrAutoTRestore<int*> autoCountRestore; |
michael@0 | 31 | * if (useExtra) { |
michael@0 | 32 | * autoCountRestore.reset(&fCount); |
michael@0 | 33 | * fCount += fExtraCount; |
michael@0 | 34 | * } |
michael@0 | 35 | * ... |
michael@0 | 36 | * } // fCount is restored |
michael@0 | 37 | */ |
michael@0 | 38 | template <typename T> class GrAutoTRestore : public SkNoncopyable { |
michael@0 | 39 | public: |
michael@0 | 40 | GrAutoTRestore() : fPtr(NULL), fVal() {} |
michael@0 | 41 | |
michael@0 | 42 | GrAutoTRestore(T* ptr) { |
michael@0 | 43 | fPtr = ptr; |
michael@0 | 44 | if (NULL != ptr) { |
michael@0 | 45 | fVal = *ptr; |
michael@0 | 46 | } |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | ~GrAutoTRestore() { |
michael@0 | 50 | if (NULL != fPtr) { |
michael@0 | 51 | *fPtr = fVal; |
michael@0 | 52 | } |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | // restores previously saved value (if any) and saves value for passed T* |
michael@0 | 56 | void reset(T* ptr) { |
michael@0 | 57 | if (NULL != fPtr) { |
michael@0 | 58 | *fPtr = fVal; |
michael@0 | 59 | } |
michael@0 | 60 | fPtr = ptr; |
michael@0 | 61 | fVal = *ptr; |
michael@0 | 62 | } |
michael@0 | 63 | private: |
michael@0 | 64 | T* fPtr; |
michael@0 | 65 | T fVal; |
michael@0 | 66 | }; |
michael@0 | 67 | |
michael@0 | 68 | #endif |