summaryrefslogtreecommitdiff
path: root/source/core/slang-com-ptr.h
blob: 9f6651306ae199ed2f01053f32a76d9a272b9643 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#ifndef SLANG_COM_PTR_H
#define SLANG_COM_PTR_H

#include "slang-defines.h"
#include "slang-result.h"

#include <assert.h>

namespace Slang {

/*! \brief ComPtr is a simple smart pointer that manages types which implement COM based interfaces. 
\details A class that implements a COM, must derive from the IUnknown interface or a type that matches
it's layout exactly (such as IForwardUnknown). Trying to use this template with a class that doesn't follow
these rules, will lead to undefined behavior. 
This is a 'strong' pointer type, and will AddRef when a non null pointer is set and Release when the pointer 
leaves scope. 
Using 'detach' allows a pointer to be removed from the management of the ComPtr.
To set the smart pointer to null, there is the method setNull, or alternatively just assign SLANG_NULL/nullptr.

One edge case using the template is that sometimes you want access as a pointer to a pointer. Sometimes this
is to write into the smart pointer, other times to pass as an array. To handle these different behaviors 
there are the methods readRef and writeRef, which are used instead of the & (ref) operator. For example

\code

Void doSomething(ID3D12Resource** resources, IndexT numResources);

// ...
ComPtr<ID3D12Resource> resources[3];

doSomething(resources[0].readRef(), SLANG_COUNT_OF(resource));
\endcode

A more common scenario writing to the pointer 

\code
IUnknown* unk = ...;

ComPtr<ID3D12Resource> resource;
Result res = unk->QueryInterface(resource.writeRef());
\endcode
*/
 
struct Guid
{
    uint32_t data1;     ///< Low field of the timestamp
    uint16_t data2;     ///< Middle field of the timestamp
    uint16_t data3;     ///< High field of the timestamp with multiplexed version number
    uint8_t  data4[8];  ///< 0, 1 = clock_seq_hi_and_reserved, clock_seq_low, followed by 'spatially unique node' (48 bits) 
};

SLANG_FORCE_INLINE bool operator==(const Guid& aIn, const Guid& bIn)
{
	// Use the largest type the honors the alignment of Guid
	typedef uint32_t CmpType;
    struct GuidCompare
    {
        Guid guid;
		CmpType data[sizeof(Guid) / sizeof(CmpType)];
    };
	// Type pun - so compiler can 'see' the pun and not break aliasing rules 
    const CmpType* a = reinterpret_cast<const GuidCompare&>(aIn).data;
    const CmpType* b = reinterpret_cast<const GuidCompare&>(bIn).data;
	// Make the guid comparison a single branch, by not using short circuit
	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2]) | (a[3] ^ b[3])) == 0;
} 

SLANG_FORCE_INLINE bool operator!=(const Guid& a, const Guid& b)
{
    return !(a == b);
} 

// Allows for defining of a GUID that works in C++ and C which defines in a format similar to microsofts INTERFACE style
// MIDL_INTERFACE("00000000-0000-0000-C000-00 00 00 00 00 46")

#define SLANG_GUID_BYTE(x, index) ((uint8_t)(SLANG_UINT64(0x##x) >> (8 * index)))

#define SLANG_MAKE_GUID(data0, data1, data2, shortTail, tail) \
	{ (uint32_t)(0x##data0), (uint16_t)(0x##data1), (uint16_t)(0x##data2), \
		{ (uint8_t)(0x##shortTail >> 8), (uint8_t)(0x##shortTail & 0xff), \
		SLANG_GUID_BYTE(tail,5), SLANG_GUID_BYTE(tail,4), SLANG_GUID_BYTE(tail,3), SLANG_GUID_BYTE(tail,2), SLANG_GUID_BYTE(tail,1), SLANG_GUID_BYTE(tail,0) \
	}}

// Compatible with Microsoft IUnknown
static const Guid IID_IComUnknown = SLANG_MAKE_GUID(00000000, 0000, 0000, C000, 000000000046);

/// ! Must be kept in sync with IUnknown
class IComUnknown
{
public:
    virtual SLANG_NO_THROW Result SLANG_MCALL queryInterface(const Guid& iid, void* objOut) = 0;
    virtual SLANG_NO_THROW uint32_t SLANG_MCALL addRef() = 0;
    virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() = 0;
};

// Enum to force initializing as an attach (without adding a reference) 
enum InitAttach
{
    INIT_ATTACH
};

template <class T>
class ComPtr
{
public:
	typedef T Type;
	typedef ComPtr ThisType;
	typedef IComUnknown* Ptr;

		/// Constructors
		/// Default Ctor. Sets to nullptr
	SLANG_FORCE_INLINE ComPtr() :m_ptr(nullptr) {}
		/// Sets, and ref counts.
	SLANG_FORCE_INLINE explicit ComPtr(T* ptr) :m_ptr(ptr) { if (ptr) ((Ptr)ptr)->addRef(); }
		/// The copy ctor
	SLANG_FORCE_INLINE ComPtr(const ThisType& rhs) : m_ptr(rhs.m_ptr) { if (m_ptr) ((Ptr)m_ptr)->addRef(); }

        /// Ctor without adding to ref count.
    SLANG_FORCE_INLINE explicit ComPtr(InitAttach, T* ptr) :m_ptr(ptr) { }
        /// Ctor without adding to ref count
    SLANG_FORCE_INLINE ComPtr(InitAttach, const ThisType& rhs) : m_ptr(rhs.m_ptr) { }

#ifdef SLANG_HAS_MOVE_SEMANTICS
		/// Move Ctor
	SLANG_FORCE_INLINE ComPtr(ThisType&& rhs) : m_ptr(rhs.m_ptr) { rhs.m_ptr = nullptr; }
		/// Move assign
	SLANG_FORCE_INLINE ComPtr& operator=(ThisType&& rhs) { T* swap = m_ptr; m_ptr = rhs.m_ptr; rhs.m_ptr = swap; return *this; }
#endif

	/// Destructor releases the pointer, assuming it is set
	SLANG_FORCE_INLINE ~ComPtr() { if (m_ptr) ((Ptr)m_ptr)->release(); }

	// !!! Operators !!!

	  /// Returns the dumb pointer
	SLANG_FORCE_INLINE operator T *() const { return m_ptr; }

	SLANG_FORCE_INLINE T& operator*() { return *m_ptr; }
		/// For making method invocations through the smart pointer work through the dumb pointer
	SLANG_FORCE_INLINE T* operator->() const { return m_ptr; }

		/// Assign 
	SLANG_FORCE_INLINE const ThisType &operator=(const ThisType& rhs);
		/// Assign from dumb ptr
	SLANG_FORCE_INLINE T* operator=(T* in);

		/// Get the pointer and don't ref
	SLANG_FORCE_INLINE T* get() const { return m_ptr; }
		/// Release a contained nullptr pointer if set
	SLANG_FORCE_INLINE void setNull();

		/// Detach
	SLANG_FORCE_INLINE T* detach() { T* ptr = m_ptr; m_ptr = nullptr; return ptr; }
		/// Set to a pointer without changing the ref count
	SLANG_FORCE_INLINE void attach(T* in) { m_ptr = in; }

		/// Get ready for writing (nulls contents)
	SLANG_FORCE_INLINE T** writeRef() { setNull(); return &m_ptr; }
		/// Get for read access
	SLANG_FORCE_INLINE T*const* readRef() const { return &m_ptr; }

		/// Swap
	void swap(ThisType& rhs);

protected:
	/// Gets the address of the dumb pointer.
    // Disabled: use writeRef and readRef to get a reference based on usage.
	SLANG_FORCE_INLINE T** operator&();

	T* m_ptr;
};

//----------------------------------------------------------------------------
template <typename T>
void ComPtr<T>::setNull()
{
	if (m_ptr)
	{
		((Ptr)m_ptr)->release();
		m_ptr = nullptr;
	}
}
//----------------------------------------------------------------------------
/* template <typename T>
T** ComPtr<T>::operator&()
{
	assert(m_ptr == nullptr);
	return &m_ptr;
} */
//----------------------------------------------------------------------------
template <typename T>
const ComPtr<T>& ComPtr<T>::operator=(const ThisType& rhs)
{
	if (rhs.m_ptr) ((Ptr)rhs.m_ptr)->addRef();
	if (m_ptr) ((Ptr)m_ptr)->release();
	m_ptr = rhs.m_ptr;
	return *this;
}
//----------------------------------------------------------------------------
template <typename T>
T* ComPtr<T>::operator=(T* ptr)
{
	if (ptr) ((Ptr)ptr)->addRef();
	if (m_ptr) ((Ptr)m_ptr)->release();
	m_ptr = ptr;           
	return m_ptr;
}
//----------------------------------------------------------------------------
template <typename T>
void ComPtr<T>::swap(ThisType& rhs)
{
	T* tmp = m_ptr;
	m_ptr = rhs.m_ptr;
	rhs.m_ptr = tmp;
}

} // namespace Slang 

#endif // SLANG_COM_PTR_H