diff -r 000000000000 -r 6474c204b198 gfx/skia/trunk/src/gpu/GrTemplates.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gfx/skia/trunk/src/gpu/GrTemplates.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,68 @@ +/* + * Copyright 2010 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef GrTemplates_DEFINED +#define GrTemplates_DEFINED + +#include "SkTypes.h" + +/** + * Use to cast a ptr to a different type, and maintain strict-aliasing + */ +template Dst GrTCast(Src src) { + union { + Src src; + Dst dst; + } data; + data.src = src; + return data.dst; +} + +/** + * takes a T*, saves the value it points to, in and restores the value in the + * destructor + * e.g.: + * { + * GrAutoTRestore autoCountRestore; + * if (useExtra) { + * autoCountRestore.reset(&fCount); + * fCount += fExtraCount; + * } + * ... + * } // fCount is restored + */ +template class GrAutoTRestore : public SkNoncopyable { +public: + GrAutoTRestore() : fPtr(NULL), fVal() {} + + GrAutoTRestore(T* ptr) { + fPtr = ptr; + if (NULL != ptr) { + fVal = *ptr; + } + } + + ~GrAutoTRestore() { + if (NULL != fPtr) { + *fPtr = fVal; + } + } + + // restores previously saved value (if any) and saves value for passed T* + void reset(T* ptr) { + if (NULL != fPtr) { + *fPtr = fVal; + } + fPtr = ptr; + fVal = *ptr; + } +private: + T* fPtr; + T fVal; +}; + +#endif