1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/gpu/GrTemplates.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,68 @@ 1.4 +/* 1.5 + * Copyright 2010 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef GrTemplates_DEFINED 1.12 +#define GrTemplates_DEFINED 1.13 + 1.14 +#include "SkTypes.h" 1.15 + 1.16 +/** 1.17 + * Use to cast a ptr to a different type, and maintain strict-aliasing 1.18 + */ 1.19 +template <typename Dst, typename Src> Dst GrTCast(Src src) { 1.20 + union { 1.21 + Src src; 1.22 + Dst dst; 1.23 + } data; 1.24 + data.src = src; 1.25 + return data.dst; 1.26 +} 1.27 + 1.28 +/** 1.29 + * takes a T*, saves the value it points to, in and restores the value in the 1.30 + * destructor 1.31 + * e.g.: 1.32 + * { 1.33 + * GrAutoTRestore<int*> autoCountRestore; 1.34 + * if (useExtra) { 1.35 + * autoCountRestore.reset(&fCount); 1.36 + * fCount += fExtraCount; 1.37 + * } 1.38 + * ... 1.39 + * } // fCount is restored 1.40 + */ 1.41 +template <typename T> class GrAutoTRestore : public SkNoncopyable { 1.42 +public: 1.43 + GrAutoTRestore() : fPtr(NULL), fVal() {} 1.44 + 1.45 + GrAutoTRestore(T* ptr) { 1.46 + fPtr = ptr; 1.47 + if (NULL != ptr) { 1.48 + fVal = *ptr; 1.49 + } 1.50 + } 1.51 + 1.52 + ~GrAutoTRestore() { 1.53 + if (NULL != fPtr) { 1.54 + *fPtr = fVal; 1.55 + } 1.56 + } 1.57 + 1.58 + // restores previously saved value (if any) and saves value for passed T* 1.59 + void reset(T* ptr) { 1.60 + if (NULL != fPtr) { 1.61 + *fPtr = fVal; 1.62 + } 1.63 + fPtr = ptr; 1.64 + fVal = *ptr; 1.65 + } 1.66 +private: 1.67 + T* fPtr; 1.68 + T fVal; 1.69 +}; 1.70 + 1.71 +#endif