| 1 | /*  This file is part of the KDE project.
 | 
|---|
| 2 | 
 | 
|---|
| 3 | Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
 | 
|---|
| 4 | 
 | 
|---|
| 5 | This library is free software: you can redistribute it and/or modify
 | 
|---|
| 6 | it under the terms of the GNU Lesser General Public License as published by
 | 
|---|
| 7 | the Free Software Foundation, either version 2.1 or 3 of the License.
 | 
|---|
| 8 | 
 | 
|---|
| 9 | This library is distributed in the hope that it will be useful,
 | 
|---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 12 | GNU Lesser General Public License for more details.
 | 
|---|
| 13 | 
 | 
|---|
| 14 | You should have received a copy of the GNU Lesser General Public License
 | 
|---|
| 15 | along with this library.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 16 | */
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #ifndef PHONON_COMPOINTER_H
 | 
|---|
| 19 | #define PHONON_COMPOINTER_H
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include <windows.h>
 | 
|---|
| 22 | #include <dshow.h>
 | 
|---|
| 23 | 
 | 
|---|
| 24 | QT_BEGIN_NAMESPACE
 | 
|---|
| 25 | 
 | 
|---|
| 26 | namespace Phonon
 | 
|---|
| 27 | {
 | 
|---|
| 28 |     namespace DS9
 | 
|---|
| 29 |     {
 | 
|---|
| 30 |         template<class T> class ComPointer
 | 
|---|
| 31 |         {
 | 
|---|
| 32 |         public:
 | 
|---|
| 33 |             explicit ComPointer(T *t = 0) : m_t(t)
 | 
|---|
| 34 |             {
 | 
|---|
| 35 |             }
 | 
|---|
| 36 | 
 | 
|---|
| 37 |             explicit ComPointer( const IID &clsid, const IID &iid) : m_t(0)
 | 
|---|
| 38 |             {
 | 
|---|
| 39 |                 ::CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER, iid,
 | 
|---|
| 40 |                     reinterpret_cast<void**>(&m_t));
 | 
|---|
| 41 |             }
 | 
|---|
| 42 | 
 | 
|---|
| 43 |             explicit ComPointer(IUnknown *_unk, const GUID &guid) : m_t(0)
 | 
|---|
| 44 |             {
 | 
|---|
| 45 |                 if (_unk) {
 | 
|---|
| 46 |                     _unk->QueryInterface(guid, reinterpret_cast<void**>(&m_t));
 | 
|---|
| 47 |                 }
 | 
|---|
| 48 |             }
 | 
|---|
| 49 | 
 | 
|---|
| 50 |             ComPointer(const ComPointer<T> &other) : m_t(other.m_t)
 | 
|---|
| 51 |             {
 | 
|---|
| 52 |                 if (m_t) {
 | 
|---|
| 53 |                     m_t->AddRef();
 | 
|---|
| 54 |                 }
 | 
|---|
| 55 |             }
 | 
|---|
| 56 | 
 | 
|---|
| 57 |             ComPointer<T> &operator=(const ComPointer<T> &other)
 | 
|---|
| 58 |             {
 | 
|---|
| 59 |                 if (other.m_t) {
 | 
|---|
| 60 |                     other.m_t->AddRef();
 | 
|---|
| 61 |                 }
 | 
|---|
| 62 |                 if (m_t) { 
 | 
|---|
| 63 |                     m_t->Release();
 | 
|---|
| 64 |                 }
 | 
|---|
| 65 |                 m_t = other.m_t;
 | 
|---|
| 66 |                 return *this;
 | 
|---|
| 67 |             }
 | 
|---|
| 68 | 
 | 
|---|
| 69 |             T *operator->() const 
 | 
|---|
| 70 |             {
 | 
|---|
| 71 |                 return m_t;
 | 
|---|
| 72 |             }
 | 
|---|
| 73 | 
 | 
|---|
| 74 |             operator T*() const 
 | 
|---|
| 75 |             {
 | 
|---|
| 76 |                 return m_t; 
 | 
|---|
| 77 |             }
 | 
|---|
| 78 | 
 | 
|---|
| 79 |             //the following method first reinitialize their value to avoid mem leaks
 | 
|---|
| 80 |             T ** pparam()
 | 
|---|
| 81 |             {
 | 
|---|
| 82 |                 if (m_t) { 
 | 
|---|
| 83 |                     m_t->Release();
 | 
|---|
| 84 |                     m_t = 0;
 | 
|---|
| 85 |                 }
 | 
|---|
| 86 |                 return &m_t;
 | 
|---|
| 87 |             }
 | 
|---|
| 88 | 
 | 
|---|
| 89 |                         bool operator==(const ComPointer<T> &other) const
 | 
|---|
| 90 |             {
 | 
|---|
| 91 |                 return m_t == other.m_t;
 | 
|---|
| 92 |             }
 | 
|---|
| 93 | 
 | 
|---|
| 94 |             bool operator!=(const ComPointer<T> &other) const
 | 
|---|
| 95 |             {
 | 
|---|
| 96 |                 return m_t != other.m_t;
 | 
|---|
| 97 |             }
 | 
|---|
| 98 | 
 | 
|---|
| 99 |             ~ComPointer()
 | 
|---|
| 100 |             {
 | 
|---|
| 101 |                 if (m_t) { 
 | 
|---|
| 102 |                     m_t->Release();
 | 
|---|
| 103 |                 }
 | 
|---|
| 104 |             }
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         private:
 | 
|---|
| 107 |             T *m_t;
 | 
|---|
| 108 |         };
 | 
|---|
| 109 |     }
 | 
|---|
| 110 | }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | QT_END_NAMESPACE
 | 
|---|
| 113 | 
 | 
|---|
| 114 | #endif
 | 
|---|