openshot-audio  0.1.4
juce_win32_ComSmartPtr.h
Go to the documentation of this file.
1 /*
2  ==============================================================================
3 
4  This file is part of the juce_core module of the JUCE library.
5  Copyright (c) 2015 - ROLI Ltd.
6 
7  Permission to use, copy, modify, and/or distribute this software for any purpose with
8  or without fee is hereby granted, provided that the above copyright notice and this
9  permission notice appear in all copies.
10 
11  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
12  TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
13  NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
15  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 
18  ------------------------------------------------------------------------------
19 
20  NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
21  All other JUCE modules are covered by a dual GPL/commercial license, so if you are
22  using any other modules, be sure to check that you also comply with their license.
23 
24  For more details, visit www.juce.com
25 
26  ==============================================================================
27 */
28 
29 #ifndef JUCE_WIN32_COMSMARTPTR_H_INCLUDED
30 #define JUCE_WIN32_COMSMARTPTR_H_INCLUDED
31 
32 #if JUCE_MINGW && defined(__uuidof)
33  #undef __uuidof
34 #endif
35 
36 #if ! (defined (_MSC_VER) || defined (__uuidof))
37 template<typename Type> struct UUIDGetter { static CLSID get() { jassertfalse; return CLSID(); } };
38 #define __uuidof(x) UUIDGetter<x>::get()
39 #endif
40 
41 inline GUID uuidFromString (const char* const s) noexcept
42 {
43  unsigned long p0;
44  unsigned int p1, p2, p3, p4, p5, p6, p7, p8, p9, p10;
45 
46  #ifndef _MSC_VER
47  sscanf
48  #else
49  sscanf_s
50  #endif
51  (s, "%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",
52  &p0, &p1, &p2, &p3, &p4, &p5, &p6, &p7, &p8, &p9, &p10);
53 
54  GUID g = { p0, (uint16) p1, (uint16) p2, { (uint8) p3, (uint8) p4, (uint8) p5, (uint8) p6,
55  (uint8) p7, (uint8) p8, (uint8) p9, (uint8) p10 }};
56  return g;
57 }
58 
59 //==============================================================================
62 template <class ComClass>
63 class ComSmartPtr
64 {
65 public:
66  ComSmartPtr() throw() : p (0) {}
67  ComSmartPtr (ComClass* const obj) : p (obj) { if (p) p->AddRef(); }
68  ComSmartPtr (const ComSmartPtr<ComClass>& other) : p (other.p) { if (p) p->AddRef(); }
69  ~ComSmartPtr() { release(); }
70 
71  operator ComClass*() const throw() { return p; }
72  ComClass& operator*() const throw() { return *p; }
73  ComClass* operator->() const throw() { return p; }
74 
75  ComSmartPtr& operator= (ComClass* const newP)
76  {
77  if (newP != 0) newP->AddRef();
78  release();
79  p = newP;
80  return *this;
81  }
82 
83  ComSmartPtr& operator= (const ComSmartPtr<ComClass>& newP) { return operator= (newP.p); }
84 
85  // Releases and nullifies this pointer and returns its address
87  {
88  release();
89  p = 0;
90  return &p;
91  }
92 
93  HRESULT CoCreateInstance (REFCLSID classUUID, DWORD dwClsContext = CLSCTX_INPROC_SERVER)
94  {
95  HRESULT hr = ::CoCreateInstance (classUUID, 0, dwClsContext, __uuidof (ComClass), (void**) resetAndGetPointerAddress());
96  jassert (hr != CO_E_NOTINITIALIZED); // You haven't called CoInitialize for the current thread!
97  return hr;
98  }
99 
100  template <class OtherComClass>
101  HRESULT QueryInterface (REFCLSID classUUID, ComSmartPtr<OtherComClass>& destObject) const
102  {
103  if (p == 0)
104  return E_POINTER;
105 
106  return p->QueryInterface (classUUID, (void**) destObject.resetAndGetPointerAddress());
107  }
108 
109  template <class OtherComClass>
110  HRESULT QueryInterface (ComSmartPtr<OtherComClass>& destObject) const
111  {
112  return this->QueryInterface (__uuidof (OtherComClass), destObject);
113  }
114 
115 private:
116  ComClass* p;
117 
118  void release() { if (p != 0) p->Release(); }
119 
120  ComClass** operator&() throw(); // private to avoid it being used accidentally
121 };
122 
123 //==============================================================================
124 #define JUCE_COMRESULT HRESULT __stdcall
125 
126 //==============================================================================
127 template <class ComClass>
128 class ComBaseClassHelperBase : public ComClass
129 {
130 public:
131  ComBaseClassHelperBase (unsigned int initialRefCount) : refCount (initialRefCount) {}
133 
134  ULONG __stdcall AddRef() { return ++refCount; }
135  ULONG __stdcall Release() { const ULONG r = --refCount; if (r == 0) delete this; return r; }
136 
137 protected:
138  ULONG refCount;
139 
140  JUCE_COMRESULT QueryInterface (REFIID refId, void** result)
141  {
142  if (refId == IID_IUnknown)
143  return castToType <IUnknown> (result);
144 
145  *result = 0;
146  return E_NOINTERFACE;
147  }
148 
149  template <class Type>
150  JUCE_COMRESULT castToType (void** result)
151  {
152  this->AddRef(); *result = dynamic_cast <Type*> (this); return S_OK;
153  }
154 };
155 
158 template <class ComClass>
160 {
161 public:
162  ComBaseClassHelper (unsigned int initialRefCount = 1) : ComBaseClassHelperBase <ComClass> (initialRefCount) {}
164 
165  JUCE_COMRESULT QueryInterface (REFIID refId, void** result)
166  {
167  if (refId == __uuidof (ComClass))
168  return this->template castToType <ComClass> (result);
169 
171  }
172 };
173 
174 #endif // JUCE_WIN32_COMSMARTPTR_H_INCLUDED
ComBaseClassHelperBase(unsigned int initialRefCount)
Definition: juce_win32_ComSmartPtr.h:131
ComBaseClassHelper(unsigned int initialRefCount=1)
Definition: juce_win32_ComSmartPtr.h:162
ULONG refCount
Definition: juce_win32_ComSmartPtr.h:138
ComClass * operator->() const
Definition: juce_win32_ComSmartPtr.h:73
#define noexcept
Definition: juce_CompilerSupport.h:141
unsigned short uint16
Definition: juce_MathsFunctions.h:47
JUCE_COMRESULT QueryInterface(REFIID refId, void **result)
Definition: juce_win32_ComSmartPtr.h:140
ComSmartPtr()
Definition: juce_win32_ComSmartPtr.h:66
#define jassertfalse
Definition: juce_PlatformDefs.h:141
ComClass ** resetAndGetPointerAddress()
Definition: juce_win32_ComSmartPtr.h:86
ComSmartPtr(ComClass *const obj)
Definition: juce_win32_ComSmartPtr.h:67
Definition: juce_VST3Common.h:137
#define JUCE_COMRESULT
Definition: juce_win32_ComSmartPtr.h:124
virtual ~ComBaseClassHelperBase()
Definition: juce_win32_ComSmartPtr.h:132
GUID uuidFromString(const char *const s) noexcept
Definition: juce_win32_ComSmartPtr.h:41
JUCE_COMRESULT QueryInterface(REFIID refId, void **result)
Definition: juce_win32_ComSmartPtr.h:165
ULONG __stdcall Release()
Definition: juce_win32_ComSmartPtr.h:135
~ComSmartPtr()
Definition: juce_win32_ComSmartPtr.h:69
JUCE_COMRESULT castToType(void **result)
Definition: juce_win32_ComSmartPtr.h:150
HRESULT CoCreateInstance(REFCLSID classUUID, DWORD dwClsContext=CLSCTX_INPROC_SERVER)
Definition: juce_win32_ComSmartPtr.h:93
HRESULT QueryInterface(REFCLSID classUUID, ComSmartPtr< OtherComClass > &destObject) const
Definition: juce_win32_ComSmartPtr.h:101
#define jassert(a)
Definition: juce_PlatformDefs.h:146
ComSmartPtr(const ComSmartPtr< ComClass > &other)
Definition: juce_win32_ComSmartPtr.h:68
ULONG __stdcall AddRef()
Definition: juce_win32_ComSmartPtr.h:134
HRESULT QueryInterface(ComSmartPtr< OtherComClass > &destObject) const
Definition: juce_win32_ComSmartPtr.h:110
Definition: juce_win32_ComSmartPtr.h:128
Definition: juce_win32_ComSmartPtr.h:37
#define __uuidof(x)
Definition: juce_win32_ComSmartPtr.h:38
~ComBaseClassHelper()
Definition: juce_win32_ComSmartPtr.h:163
unsigned char uint8
Definition: juce_MathsFunctions.h:43
Definition: juce_win32_ComSmartPtr.h:159
ComClass & operator*() const
Definition: juce_win32_ComSmartPtr.h:72