[PSDK]
[reactos.git] / reactos / lib / atl / atlcore.h
1 /*
2 * ReactOS ATL
3 *
4 * Copyright 2009 Andrew Hill <ash77@reactos.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #pragma once
22
23 #include <malloc.h>
24 #include <olectl.h>
25 #include <ocidl.h>
26
27 #include <crtdbg.h>
28
29 #ifndef ATLASSERT
30 #define ATLASSERT(expr) _ASSERTE(expr)
31 #endif // ATLASSERT
32
33 namespace ATL
34 {
35
36 class CComCriticalSection
37 {
38 public:
39 CRITICAL_SECTION m_sec;
40 public:
41 CComCriticalSection()
42 {
43 memset(&m_sec, 0, sizeof(CRITICAL_SECTION));
44 }
45
46 ~CComCriticalSection()
47 {
48 }
49
50 HRESULT Lock()
51 {
52 EnterCriticalSection(&m_sec);
53 return S_OK;
54 }
55
56 HRESULT Unlock()
57 {
58 LeaveCriticalSection(&m_sec);
59 return S_OK;
60 }
61
62 HRESULT Init()
63 {
64 InitializeCriticalSection(&m_sec);
65 return S_OK;
66 }
67
68 HRESULT Term()
69 {
70 DeleteCriticalSection(&m_sec);
71 return S_OK;
72 }
73 };
74
75 class CComFakeCriticalSection
76 {
77 public:
78 HRESULT Lock()
79 {
80 return S_OK;
81 }
82
83 HRESULT Unlock()
84 {
85 return S_OK;
86 }
87
88 HRESULT Init()
89 {
90 return S_OK;
91 }
92
93 HRESULT Term()
94 {
95 return S_OK;
96 }
97 };
98
99 class CComAutoCriticalSection : public CComCriticalSection
100 {
101 public:
102 CComAutoCriticalSection()
103 {
104 HRESULT hResult;
105
106 hResult = CComCriticalSection::Init();
107 ATLASSERT(SUCCEEDED(hResult));
108 }
109 ~CComAutoCriticalSection()
110 {
111 CComCriticalSection::Term();
112 }
113 };
114
115 class CComSafeDeleteCriticalSection : public CComCriticalSection
116 {
117 private:
118 bool m_bInitialized;
119 public:
120 CComSafeDeleteCriticalSection()
121 {
122 m_bInitialized = false;
123 }
124
125 ~CComSafeDeleteCriticalSection()
126 {
127 Term();
128 }
129
130 HRESULT Lock()
131 {
132 ATLASSERT(m_bInitialized);
133 return CComCriticalSection::Lock();
134 }
135
136 HRESULT Init()
137 {
138 HRESULT hResult;
139
140 ATLASSERT(!m_bInitialized);
141 hResult = CComCriticalSection::Init();
142 if (SUCCEEDED(hResult))
143 m_bInitialized = true;
144 return hResult;
145 }
146
147 HRESULT Term()
148 {
149 if (!m_bInitialized)
150 return S_OK;
151 m_bInitialized = false;
152 return CComCriticalSection::Term();
153 }
154 };
155
156 class CComAutoDeleteCriticalSection : public CComSafeDeleteCriticalSection
157 {
158 private:
159 // CComAutoDeleteCriticalSection::Term should never be called
160 HRESULT Term();
161 };
162
163 struct _ATL_BASE_MODULE70
164 {
165 UINT cbSize;
166 HINSTANCE m_hInst;
167 HINSTANCE m_hInstResource;
168 bool m_bNT5orWin98;
169 DWORD dwAtlBuildVer;
170 GUID *pguidVer;
171 CRITICAL_SECTION m_csResource;
172 #ifdef NOTYET
173 CSimpleArray<HINSTANCE> m_rgResourceInstance;
174 #endif
175 };
176 typedef _ATL_BASE_MODULE70 _ATL_BASE_MODULE;
177
178 class CAtlBaseModule : public _ATL_BASE_MODULE
179 {
180 public :
181 static bool m_bInitFailed;
182 public:
183 CAtlBaseModule()
184 {
185 cbSize = sizeof(_ATL_BASE_MODULE);
186 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)this, &m_hInst);
187 m_hInstResource = m_hInst;
188 }
189
190 HINSTANCE GetModuleInstance()
191 {
192 return m_hInst;
193 }
194
195 HINSTANCE GetResourceInstance()
196 {
197 return m_hInstResource;
198 }
199 };
200
201 extern CAtlBaseModule _AtlBaseModule;
202
203 }; // namespace ATL