diff options
author | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-22 12:34:22 +0000 |
---|---|---|
committer | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-22 12:34:22 +0000 |
commit | 5e2412983183f9c4075a795278f3ca4ffee0ed79 (patch) | |
tree | 0cbd270ba4a640bb1f021a55f7508d0699379bd5 /src/gpu | |
parent | 9e553c6e7cff5fd699ed4dee1d52317362ea30d0 (diff) |
Move GrTemplates.h to src
Review URL: http://codereview.appspot.com/6333053/
git-svn-id: http://skia.googlecode.com/svn/trunk@4300 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/gpu')
-rw-r--r-- | src/gpu/GrTemplates.h | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/gpu/GrTemplates.h b/src/gpu/GrTemplates.h new file mode 100644 index 0000000000..4378d70949 --- /dev/null +++ b/src/gpu/GrTemplates.h @@ -0,0 +1,70 @@ + +/* + * 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 "GrNoncopyable.h" + +/** + * Use to cast a ptr to a different type, and maintain strict-aliasing + */ +template <typename Dst, typename Src> 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<int*> autoCountRestore; + * if (useExtra) { + * autoCountRestore.reset(&fCount); + * fCount += fExtraCount; + * } + * ... + * } // fCount is restored + */ +template <typename T> class GrAutoTRestore : public GrNoncopyable { +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 |