OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #ifndef GrProgramElementRef_DEFINED |
| 9 #define GrProgramElementRef_DEFINED |
| 10 |
| 11 #include "SkRefCnt.h" |
| 12 #include "GrTypes.h" |
| 13 |
| 14 /** |
| 15 * Helper for owning a GrProgramElement subclass and being able to convert a ref
to pending |
| 16 * execution. It is like an SkAutoTUnref for program elements whose execution ca
n be deferred. Once |
| 17 * in the pending execution state it is illegal to change the object that is own
ed by the |
| 18 * GrProgramElementRef. Its destructor will either unref the GrProgramElement or
signal that |
| 19 * the pending execution has completed, depending on whether convertToPendingExe
c() was called. |
| 20 */ |
| 21 template <typename T> class GrProgramElementRef : SkNoncopyable { |
| 22 public: |
| 23 GrProgramElementRef() : fOwnPendingExec(false), fObj(NULL) {}; |
| 24 |
| 25 // Adopts a ref from the caller. |
| 26 explicit GrProgramElementRef(T* obj) : fOwnPendingExec(false), fObj(obj) {} |
| 27 |
| 28 // Adopts a ref from the caller. Do not call after convertToPendingExec. |
| 29 void reset(T* obj) { |
| 30 SkASSERT(!fOwnPendingExec); |
| 31 SkSafeUnref(fObj); |
| 32 fObj = obj; |
| 33 } |
| 34 |
| 35 void convertToPendingExec() { |
| 36 SkASSERT(!fOwnPendingExec); |
| 37 fObj->convertRefToPendingExecution(); |
| 38 fOwnPendingExec = true; |
| 39 } |
| 40 |
| 41 T* get() const { return fObj; } |
| 42 operator T*() { return fObj; } |
| 43 |
| 44 /** If T is const, the type returned from operator-> will also be const. */ |
| 45 typedef typename SkTConstType<typename SkAutoTUnref<T>::BlockRef<T>, |
| 46 SkTIsConst<T>::value>::type BlockRefType; |
| 47 |
| 48 /** |
| 49 * GrProgramElementRef assumes ownership of the ref and manages converting t
he ref to a |
| 50 * pending execution. As a result, it is an error for the user to ref or unr
ef through |
| 51 * GrProgramElementRef. Therefore operator-> returns BlockRef<T>*. |
| 52 */ |
| 53 BlockRefType *operator->() const { |
| 54 return static_cast<BlockRefType*>(fObj); |
| 55 } |
| 56 |
| 57 ~GrProgramElementRef() { |
| 58 if (NULL != fObj) { |
| 59 if (fOwnPendingExec) { |
| 60 fObj->completedExecution(); |
| 61 } else { |
| 62 fObj->unref(); |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 private: |
| 68 bool fOwnPendingExec; |
| 69 T* fObj; |
| 70 |
| 71 typedef SkNoncopyable INHERITED; |
| 72 }; |
| 73 #endif |
OLD | NEW |