[atlnew]
[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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #ifndef _atlcore_h
22 #define _atlcore_h
23
24 #include <malloc.h>
25 #include <olectl.h>
26 #include <ocidl.h>
27
28 #include <crtdbg.h>
29
30 #ifndef ATLASSERT
31 #define ATLASSERT(expr) _ASSERTE(expr)
32 #endif // ATLASSERT
33
34 namespace ATL
35 {
36
37 class CComCriticalSection
38 {
39 public:
40 CRITICAL_SECTION m_sec;
41 public:
42 CComCriticalSection()
43 {
44 memset(&m_sec, 0, sizeof(CRITICAL_SECTION));
45 }
46
47 ~CComCriticalSection()
48 {
49 }
50
51 HRESULT Lock()
52 {
53 EnterCriticalSection(&m_sec);
54 return S_OK;
55 }
56
57 HRESULT Unlock()
58 {
59 LeaveCriticalSection(&m_sec);
60 return S_OK;
61 }
62
63 HRESULT Init()
64 {
65 InitializeCriticalSection(&m_sec);
66 return S_OK;
67 }
68
69 HRESULT Term()
70 {
71 DeleteCriticalSection(&m_sec);
72 return S_OK;
73 }
74 };
75
76 class CComFakeCriticalSection
77 {
78 public:
79 HRESULT Lock()
80 {
81 return S_OK;
82 }
83
84 HRESULT Unlock()
85 {
86 return S_OK;
87 }
88
89 HRESULT Init()
90 {
91 return S_OK;
92 }
93
94 HRESULT Term()
95 {
96 return S_OK;
97 }
98 };
99
100 class CComAutoCriticalSection : public CComCriticalSection
101 {
102 public:
103 CComAutoCriticalSection()
104 {
105 HRESULT hResult;
106
107 hResult = CComCriticalSection::Init();
108 ATLASSERT(SUCCEEDED(hResult));
109 }
110 ~CComAutoCriticalSection()
111 {
112 CComCriticalSection::Term();
113 }
114 };
115
116 class CComSafeDeleteCriticalSection : public CComCriticalSection
117 {
118 private:
119 bool m_bInitialized;
120 public:
121 CComSafeDeleteCriticalSection()
122 {
123 m_bInitialized = false;
124 }
125
126 ~CComSafeDeleteCriticalSection()
127 {
128 Term();
129 }
130
131 HRESULT Lock()
132 {
133 ATLASSERT(m_bInitialized);
134 return CComCriticalSection::Lock();
135 }
136
137 HRESULT Init()
138 {
139 HRESULT hResult;
140
141 ATLASSERT(!m_bInitialized);
142 hResult = CComCriticalSection::Init();
143 if (SUCCEEDED(hResult))
144 m_bInitialized = true;
145 return hResult;
146 }
147
148 HRESULT Term()
149 {
150 if (!m_bInitialized)
151 return S_OK;
152 m_bInitialized = false;
153 return CComCriticalSection::Term();
154 }
155 };
156
157 class CComAutoDeleteCriticalSection : public CComSafeDeleteCriticalSection
158 {
159 private:
160 // CComAutoDeleteCriticalSection::Term should never be called
161 HRESULT Term();
162 };
163
164 struct _ATL_BASE_MODULE70
165 {
166 UINT cbSize;
167 HINSTANCE m_hInst;
168 HINSTANCE m_hInstResource;
169 bool m_bNT5orWin98;
170 DWORD dwAtlBuildVer;
171 GUID *pguidVer;
172 CRITICAL_SECTION m_csResource;
173 #ifdef NOTYET
174 CSimpleArray<HINSTANCE> m_rgResourceInstance;
175 #endif
176 };
177 typedef _ATL_BASE_MODULE70 _ATL_BASE_MODULE;
178
179 class CAtlBaseModule : public _ATL_BASE_MODULE
180 {
181 public :
182 static bool m_bInitFailed;
183 public:
184 HINSTANCE GetModuleInstance()
185 {
186 return m_hInst;
187 }
188
189 HINSTANCE GetResourceInstance()
190 {
191 return m_hInstResource;
192 }
193 };
194
195 extern CAtlBaseModule _AtlBaseModule;
196
197 }; // namespace ATL
198
199 #endif // _atlcore_h