OSDN Git Service

とりあえず実行できるようになった。
[winaudioj/stedx.git] / sf_com.h
1 #pragma once
2 /*
3   ==============================================================================
4
5    This file is part of the mfsample
6    Copyright 2005-11 by Satoshi Fujiwara.
7
8    mfsample can be redistributed and/or modified under the terms of the
9    GNU General Public License, as published by the Free Software Foundation;
10    either version 2 of the License, or (at your option) any later version.
11
12    async is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with async; if not, visit www.gnu.org/licenses or write to the
19    Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
20    Boston, MA 02111-1307 USA
21
22   ==============================================================================
23 */
24 /** @file
25  *  @brief util
26  *  @author S.F. (Satoshi Fujiwara)
27  */
28 #include "objbase.h"
29
30
31
32 namespace sf 
33 {
34   template <class COM_SMART_PTR > inline void safe_release(COM_SMART_PTR& ptr)
35   {
36     if(ptr)
37     {
38       ptr.Reset();
39     }
40   };
41     enum com_init 
42     {
43         multi_threaded  = 0x0,
44         apartment_threaded = 0x2,
45         disable_ole1dde   = 0x4,
46         speed_over_memory = 0x8
47     };
48
49     struct com_initialize
50     {
51         struct impl;
52         com_initialize(void * reserved ,unsigned int init);
53         com_initialize(){com_initialize(0,multi_threaded);};
54         ~com_initialize() {};
55     private:
56         std::shared_ptr<impl> m_impl;
57     };
58
59         template <typename ComClass,typename ComInterface> 
60                   boost::intrusive_ptr<ComInterface> create_instance()
61                   {
62                         ComInterface * com_ptr;
63                         CoCreateInstance( __uuidof(ComClass), NULL,
64                                          CLSCTX_ALL, __uuidof(ComInterface),
65                                          (void**)&com_ptr);
66                         return boost::intrusive_ptr<ComInterface>(com_ptr,false);
67
68                   };
69                         template <typename COMInterface> 
70                 struct IUnknownImpl : public COMInterface 
71                 {
72                         IUnknownImpl() : ref_(1) {}; 
73                         virtual ~IUnknownImpl() {};
74                         ULONG __stdcall AddRef()
75                         {
76                                 return InterlockedIncrement(&ref_);
77                         }
78
79                         ULONG __stdcall Release()
80                         {
81                                 ULONG ref = InterlockedDecrement(&ref_);
82                                 if (0 == ref)
83                                 {
84                                         delete this;
85                                 }
86                                 return ref;
87                         }
88
89                         HRESULT __stdcall QueryInterface(REFIID riid, VOID **ppObj)
90                         {
91                                 if (IID_IUnknown == riid)
92                                 {
93                                         AddRef();
94                                         *ppObj = (IUnknown*)this;
95                                 }
96                                 else if (__uuidof(COMInterface) == riid)
97                                 {
98                                         AddRef();
99                                         *ppObj = (COMInterface*)this;
100                                 }
101                                 else
102                                 {
103                                         *ppObj = NULL;
104                                         return E_NOINTERFACE;
105                                 }
106                                 return S_OK;
107                         }
108                 private:
109                         LONG ref_;
110                 };
111 }