blob: 920ab78a48a42be9ed41b5cef8ac1dff0fc0e7e2 [file] [log] [blame]
bsalomon95740982014-09-04 13:12:37 -07001/*
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 can be deferred. Once
17 * in the pending execution state it is illegal to change the object that is owned by the
18 * GrProgramElementRef. Its destructor will either unref the GrProgramElement or signal that
19 * the pending execution has completed, depending on whether convertToPendingExec() was called.
20 */
21template <typename T> class GrProgramElementRef : SkNoncopyable {
22public:
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. */
bsalomon2a9ca782014-09-05 14:27:43 -070045 typedef typename SkTConstType<typename SkAutoTUnref<T>::template BlockRef<T>,
bsalomon95740982014-09-04 13:12:37 -070046 SkTIsConst<T>::value>::type BlockRefType;
47
48 /**
49 * GrProgramElementRef assumes ownership of the ref and manages converting the ref to a
50 * pending execution. As a result, it is an error for the user to ref or unref through
51 * GrProgramElementRef. Therefore operator-> returns BlockRef<T>*.
52 */
53 BlockRefType *operator->() const {
54 return static_cast<BlockRefType*>(fObj);
55 }
56
57 ~GrProgramElementRef() {
bsalomon49f085d2014-09-05 13:34:00 -070058 if (fObj) {
bsalomon95740982014-09-04 13:12:37 -070059 if (fOwnPendingExec) {
60 fObj->completedExecution();
61 } else {
62 fObj->unref();
63 }
64 }
65 }
66
67private:
68 bool fOwnPendingExec;
69 T* fObj;
70
71 typedef SkNoncopyable INHERITED;
72};
73#endif