diff options
author | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-10-12 19:53:16 +0000 |
---|---|---|
committer | bsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2011-10-12 19:53:16 +0000 |
commit | d38f137e9b813f8193675ebd3dfbfe8bc42639e9 (patch) | |
tree | 1e670c378d7b31a4538fde3c2b3e4e29b72c05b5 /include/gpu/GrTemplates.h | |
parent | 4d5cb45f3e3e62633304b4911d131cdd02dfd541 (diff) |
Move gpu/include/* to include/gpu and gpu/src/* to src/gpu
Review URL: http://codereview.appspot.com/5250070/
git-svn-id: http://skia.googlecode.com/svn/trunk@2471 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'include/gpu/GrTemplates.h')
-rw-r--r-- | include/gpu/GrTemplates.h | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/include/gpu/GrTemplates.h b/include/gpu/GrTemplates.h new file mode 100644 index 0000000000..63e43eed01 --- /dev/null +++ b/include/gpu/GrTemplates.h @@ -0,0 +1,69 @@ + +/* + * 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; +} + +/** + * saves value of T* in and restores in destructor + * e.g.: + * { + * GrAutoTPtrValueRestore<int*> autoCountRestore; + * if (useExtra) { + * autoCountRestore.save(&fCount); + * fCount += fExtraCount; + * } + * ... + * } // fCount is restored + */ +template <typename T> class GrAutoTPtrValueRestore : public GrNoncopyable { +public: + GrAutoTPtrValueRestore() : fPtr(NULL), fVal() {} + + GrAutoTPtrValueRestore(T* ptr) { + fPtr = ptr; + if (NULL != ptr) { + fVal = *ptr; + } + } + + ~GrAutoTPtrValueRestore() { + if (NULL != fPtr) { + *fPtr = fVal; + } + } + + // restores previously saved value (if any) and saves value for passed T* + void save(T* ptr) { + if (NULL != fPtr) { + *fPtr = fVal; + } + fPtr = ptr; + fVal = *ptr; + } +private: + T* fPtr; + T fVal; +}; + +#endif |