blob: d8cba3cb7afc6d0f8f1eeeff72edaf56781d8bfe [file] [log] [blame]
bungeman@google.com9df621d2011-06-23 21:43:52 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
bungeman@google.com9df621d2011-06-23 21:43:52 +00006 */
7
commit-bot@chromium.orge0294402013-08-29 22:14:04 +00008#ifndef SkTScopedComPtr_DEFINED
9#define SkTScopedComPtr_DEFINED
bungeman@google.com9df621d2011-06-23 21:43:52 +000010
Ben Wagnerab6eefe2019-05-20 11:02:49 -040011#include "src/core/SkLeanWindows.h"
Ben Wagner6cb6a072019-08-12 18:30:27 -040012#include "src/utils/win/SkObjBase.h"
bungeman@google.com9df621d2011-06-23 21:43:52 +000013
halcanary0cbe7ee2015-12-01 09:02:49 -080014#ifdef SK_BUILD_FOR_WIN
15
bungeman@google.com71033442013-05-01 14:21:20 +000016template<typename T> T* SkRefComPtr(T* ptr) {
17 ptr->AddRef();
18 return ptr;
19}
20
bungeman@google.com4bbc5242013-05-02 14:01:36 +000021template<typename T> T* SkSafeRefComPtr(T* ptr) {
22 if (ptr) {
23 ptr->AddRef();
24 }
25 return ptr;
26}
27
bungeman@google.comb29c8832011-10-10 13:19:10 +000028template<typename T>
Hal Canaryb2cd1d72017-01-24 11:11:31 -050029class SkTScopedComPtr {
bungeman@google.com9df621d2011-06-23 21:43:52 +000030private:
31 T *fPtr;
32
33public:
Hal Canary1b2b3fb2017-01-27 08:20:17 -050034 constexpr SkTScopedComPtr() : fPtr(nullptr) {}
35 constexpr SkTScopedComPtr(std::nullptr_t) : fPtr(nullptr) {}
36 explicit SkTScopedComPtr(T *ptr) : fPtr(ptr) {}
37 SkTScopedComPtr(SkTScopedComPtr&& that) : fPtr(that.release()) {}
38 SkTScopedComPtr(const SkTScopedComPtr&) = delete;
bungeman2853f002016-06-16 15:31:42 -070039
40 ~SkTScopedComPtr() { this->reset();}
41
Hal Canaryb2cd1d72017-01-24 11:11:31 -050042 SkTScopedComPtr& operator=(SkTScopedComPtr&& that) {
43 this->reset(that.release());
44 return *this;
45 }
46 SkTScopedComPtr& operator=(const SkTScopedComPtr&) = delete;
Hal Canary1b2b3fb2017-01-27 08:20:17 -050047 SkTScopedComPtr& operator=(std::nullptr_t) { this->reset(); return *this; }
Hal Canaryb2cd1d72017-01-24 11:11:31 -050048
bungeman2853f002016-06-16 15:31:42 -070049 T &operator*() const { SkASSERT(fPtr != nullptr); return *fPtr; }
50
51 explicit operator bool() const { return fPtr != nullptr; }
52
Ben Wagner6cb6a072019-08-12 18:30:27 -040053 T *operator->() const { return fPtr; }
bungeman2853f002016-06-16 15:31:42 -070054
bungeman@google.com9df621d2011-06-23 21:43:52 +000055 /**
56 * Returns the address of the underlying pointer.
57 * This is dangerous -- it breaks encapsulation and the reference escapes.
58 * Must only be used on instances currently pointing to NULL,
59 * and only to initialize the instance.
60 */
bungeman2853f002016-06-16 15:31:42 -070061 T **operator&() { SkASSERT(fPtr == nullptr); return &fPtr; }
62
bungeman@google.com9df621d2011-06-23 21:43:52 +000063 T *get() const { return fPtr; }
bungeman2853f002016-06-16 15:31:42 -070064
Hal Canarybd81a322016-12-21 08:18:51 -050065 void reset(T* ptr = nullptr) {
66 if (fPtr) {
67 fPtr->Release();
bungeman@google.coma9e586a2011-08-09 19:25:05 +000068 }
Hal Canarybd81a322016-12-21 08:18:51 -050069 fPtr = ptr;
bungeman@google.coma9e586a2011-08-09 19:25:05 +000070 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000071
bungeman@google.comb29c8832011-10-10 13:19:10 +000072 void swap(SkTScopedComPtr<T>& that) {
73 T* temp = this->fPtr;
74 this->fPtr = that.fPtr;
75 that.fPtr = temp;
76 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000077
bungeman@google.comb29c8832011-10-10 13:19:10 +000078 T* release() {
79 T* temp = this->fPtr;
bungeman2853f002016-06-16 15:31:42 -070080 this->fPtr = nullptr;
bungeman@google.comb29c8832011-10-10 13:19:10 +000081 return temp;
82 }
bungeman@google.com9df621d2011-06-23 21:43:52 +000083};
84
halcanary0cbe7ee2015-12-01 09:02:49 -080085#endif // SK_BUILD_FOR_WIN
86#endif // SkTScopedComPtr_DEFINED