slot 0.0.1
A real time UI render framework
载入中...
搜索中...
未找到
slang-com-ptr.h
浏览该文件的文档.
1#ifndef SLANG_COM_PTR_H
2#define SLANG_COM_PTR_H
3
4#include "slang-com-helper.h"
5
6#include <assert.h>
7#include <cstddef>
8
9namespace Slang
10{
11
42// Enum to force initializing as an attach (without adding a reference)
47
48template<class T>
49class ComPtr
50{
51public:
52 typedef T Type;
55
59 : m_ptr(nullptr)
60 {
61 }
62 SLANG_FORCE_INLINE ComPtr(std::nullptr_t)
63 : m_ptr(nullptr)
64 {
65 }
67 SLANG_FORCE_INLINE explicit ComPtr(T* ptr)
68 : m_ptr(ptr)
69 {
70 if (ptr)
71 ((Ptr)ptr)->addRef();
72 }
75 : m_ptr(rhs.m_ptr)
76 {
77 if (m_ptr)
78 ((Ptr)m_ptr)->addRef();
79 }
80
83 : m_ptr(ptr)
84 {
85 }
88 : m_ptr(rhs.m_ptr)
89 {
90 }
91
92#ifdef SLANG_HAS_MOVE_SEMANTICS
95 : m_ptr(rhs.m_ptr)
96 {
97 rhs.m_ptr = nullptr;
98 }
101 {
102 T* swap = m_ptr;
103 m_ptr = rhs.m_ptr;
104 rhs.m_ptr = swap;
105 return *this;
106 }
107#endif
108
111 {
112 if (m_ptr)
113 ((Ptr)m_ptr)->release();
114 }
115
116 // !!! Operators !!!
117
119 SLANG_FORCE_INLINE operator T*() const { return m_ptr; }
120
123 SLANG_FORCE_INLINE T* operator->() const { return m_ptr; }
124
129
131 SLANG_FORCE_INLINE T* get() const { return m_ptr; }
134
137 {
138 T* ptr = m_ptr;
139 m_ptr = nullptr;
140 return ptr;
141 }
143 SLANG_FORCE_INLINE void attach(T* in) { m_ptr = in; }
144
147 {
148 setNull();
149 return &m_ptr;
150 }
152 SLANG_FORCE_INLINE T* const* readRef() const { return &m_ptr; }
153
155 void swap(ThisType& rhs);
156
157protected:
159 // Disabled: use writeRef and readRef to get a reference based on usage.
160#ifndef SLANG_COM_PTR_ENABLE_REF_OPERATOR
162#endif
163
165};
166
167//----------------------------------------------------------------------------
168template<typename T>
170{
171 if (m_ptr)
172 {
173 ((Ptr)m_ptr)->release();
174 m_ptr = nullptr;
175 }
176}
177//----------------------------------------------------------------------------
178template<typename T>
180{
181 if (rhs.m_ptr)
182 ((Ptr)rhs.m_ptr)->addRef();
183 if (m_ptr)
184 ((Ptr)m_ptr)->release();
185 m_ptr = rhs.m_ptr;
186 return *this;
187}
188//----------------------------------------------------------------------------
189template<typename T>
191{
192 if (ptr)
193 ((Ptr)ptr)->addRef();
194 if (m_ptr)
195 ((Ptr)m_ptr)->release();
196 m_ptr = ptr;
197 return m_ptr;
198}
199//----------------------------------------------------------------------------
200template<typename T>
202{
203 T* tmp = m_ptr;
204 m_ptr = rhs.m_ptr;
205 rhs.m_ptr = tmp;
206}
207
208} // namespace Slang
209
210#endif // SLANG_COM_PTR_H
Definition slang-com-ptr.h:50
SLANG_FORCE_INLINE T * get() const
Get the pointer and don't ref
Definition slang-com-ptr.h:131
SLANG_FORCE_INLINE ComPtr(InitAttach, T *ptr)
Ctor without adding to ref count.
Definition slang-com-ptr.h:82
SLANG_FORCE_INLINE const ThisType & operator=(const ThisType &rhs)
Assign
Definition slang-com-ptr.h:179
SLANG_FORCE_INLINE T * detach()
Detach
Definition slang-com-ptr.h:136
SLANG_FORCE_INLINE ComPtr(InitAttach, const ThisType &rhs)
Ctor without adding to ref count
Definition slang-com-ptr.h:87
T * m_ptr
Definition slang-com-ptr.h:164
void swap(ThisType &rhs)
Swap
Definition slang-com-ptr.h:201
SLANG_FORCE_INLINE ComPtr(std::nullptr_t)
Definition slang-com-ptr.h:62
SLANG_FORCE_INLINE T ** writeRef()
Get ready for writing (nulls contents)
Definition slang-com-ptr.h:146
SLANG_FORCE_INLINE void attach(T *in)
Set to a pointer without changing the ref count
Definition slang-com-ptr.h:143
SLANG_FORCE_INLINE void setNull()
Release a contained nullptr pointer if set
Definition slang-com-ptr.h:169
ComPtr ThisType
Definition slang-com-ptr.h:53
SLANG_FORCE_INLINE T * operator->() const
For making method invocations through the smart pointer work through the dumb pointer
Definition slang-com-ptr.h:123
SLANG_FORCE_INLINE T * operator=(T *in)
Assign from dumb ptr
Definition slang-com-ptr.h:190
SLANG_FORCE_INLINE T ** operator&()=delete
Gets the address of the dumb pointer.
ISlangUnknown * Ptr
Definition slang-com-ptr.h:54
SLANG_FORCE_INLINE ComPtr(T *ptr)
Sets, and ref counts.
Definition slang-com-ptr.h:67
SLANG_FORCE_INLINE ComPtr(const ThisType &rhs)
The copy ctor
Definition slang-com-ptr.h:74
SLANG_FORCE_INLINE T & operator*()
Definition slang-com-ptr.h:121
SLANG_FORCE_INLINE ~ComPtr()
Destructor releases the pointer, assuming it is set
Definition slang-com-ptr.h:110
SLANG_FORCE_INLINE T *const * readRef() const
Get for read access
Definition slang-com-ptr.h:152
T Type
Definition slang-com-ptr.h:52
SLANG_FORCE_INLINE ComPtr()
Definition slang-com-ptr.h:58
Definition slang-com-ptr.h:10
InitAttach
ComPtr is a simple smart pointer that manages types which implement COM based interfaces.
Definition slang-com-ptr.h:44
@ INIT_ATTACH
Definition slang-com-ptr.h:45
#define SLANG_FORCE_INLINE
Definition slang-cpp-prelude.h:286
Definition slang-cpp-prelude.h:303