1 /**
2  * This module contains a definition for the IUnknown interface, used with COM.
3  *
4  * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com.
5  *            All rights reserved.
6  * License:   BSD style: $(LICENSE)
7  * Authors:   Walter Bright, Sean Kelly
8  */
9 module tango.sys.win32.IUnknown;
10 
11 
12 private
13 {
14     import tango.sys.win32.Types;
15     extern (C) extern IID IID_IUnknown;
16 }
17 
18 
19 interface IUnknown
20 {
21     HRESULT QueryInterface( REFIID iid, out IUnknown obj );
22     ULONG AddRef();
23     ULONG Release();
24 }
25 
26 
27 /**
28  * This implementation may be mixed into COM classes to avoid code duplication.
29  */
30 template IUnknownImpl()
31 {
32     HRESULT QueryInterface( REFIID iid, out IUnknown obj )
33     {
34         if ( iid == &IID_IUnknown )
35         {
36             AddRef();
37             obj = this;
38             return S_OK;
39         }
40         else
41         {
42             obj = null;
43             return E_NOINTERFACE;
44         }
45     }
46 
47     ULONG AddRef()
48     {
49         return ++m_count;
50     }
51 
52     ULONG Release()
53     {
54         if( --m_count == 0 )
55         {
56             // free object
57             return 0;
58         }
59         return m_count;
60     }
61 
62 private:
63     ULONG m_count = 1;
64 }