OSDN Git Service

Windows APIを実装部に隠蔽してみた。まだ不完全だが。
[wintimer/wintimer.git] / wintimer / sf_com.h
1 #pragma once
2 /*
3   ==============================================================================
4
5    This file is part of the Shooting3
6    Copyright 2005-11 by Satoshi Fujiwara.
7
8    mini timer 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    mini timer 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 mini timer; 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 namespace sf 
30 {
31     enum com_init 
32     {
33         multi_threaded  = 0x0,
34         apartment_threaded = 0x2,
35         disable_ole1dde   = 0x4,
36         speed_over_memory = 0x8
37     };
38
39     struct com_initialize
40     {
41         struct impl;
42         com_initialize(void * reserved = NULL,unsigned int init = multi_threaded);
43         ~com_initialize() {};
44     private:
45         boost::shared_ptr<impl> m_impl;
46     };
47
48         template <typename ComClass,typename ComInterface> 
49                   boost::intrusive_ptr<ComInterface> create_instance()
50                   {
51                         ComInterface * com_ptr;
52                         CoCreateInstance( __uuidof(ComClass), NULL,
53                                          CLSCTX_ALL, __uuidof(ComInterface),
54                                          (void**)&com_ptr);
55                         return boost::intrusive_ptr<ComInterface>(com_ptr,false);
56
57                   };
58                         template <typename COMInterface> 
59                 struct IUnknownImpl : public COMInterface 
60                 {
61                         IUnknownImpl() : ref_(1) {}; 
62                         virtual ~IUnknownImpl() {};
63                         ULONG __stdcall AddRef()
64                         {
65                                 return InterlockedIncrement(&ref_);
66                         }
67
68                         ULONG __stdcall Release()
69                         {
70                                 ULONG ref = InterlockedDecrement(&ref_);
71                                 if (0 == ref)
72                                 {
73                                         delete this;
74                                 }
75                                 return ref;
76                         }
77
78                         HRESULT __stdcall QueryInterface(REFIID riid, VOID **ppObj)
79                         {
80                                 if (IID_IUnknown == riid)
81                                 {
82                                         AddRef();
83                                         *ppObj = (IUnknown*)this;
84                                 }
85                                 else if (__uuidof(COMInterface) == riid)
86                                 {
87                                         AddRef();
88                                         *ppObj = (COMInterface*)this;
89                                 }
90                                 else
91                                 {
92                                         *ppObj = NULL;
93                                         return E_NOINTERFACE;
94                                 }
95                                 return S_OK;
96                         }
97                 private:
98                         LONG ref_;
99                 };
100 }