summaryrefslogtreecommitdiff
path: root/audio/sysvad/APO/Inc/CommonMacros.h
blob: 097c6d473595d9d9528d71759d9b3417560157b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//**@@@*@@@****************************************************
//
// Microsoft Windows
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//**@@@*@@@****************************************************

//
// FileName:    CommonMacros.h
//
// Abstract:    Useful macros
//
// ----------------------------------------------------------------------------


#pragma once
#include <windef.h>
#include <windows.h>


//-------------------------------------------------------------------------
// Description:
//
// If the condition evaluates to TRUE, jump to the given label.
//
// Parameters:
//
//      condition - [in] code that fits in if statement
//      label - [in] label to jump if condition is met
//
#define IF_TRUE_JUMP(condition, label)                          \
    if (condition)                                              \
    {                                                           \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// If the condition evaluates to FALSE, jump to the given label.
//
// Parameters:
//
//      condition - [in] code that fits in if statement
//      label - [in] label to jump if condition is met
//
#define IF_FALSE_JUMP(condition, label)                         \
    if (!condition)                                             \
    {                                                           \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// If the hresult passed FAILED, jump to the given label.
//
// Parameters:
//
//      _hresult - [in] Value to check
//      label - [in] label to jump if condition is met
//
#define IF_FAILED_JUMP(_hresult, label)                         \
    if (FAILED(_hresult))                                       \
    {                                                           \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// If the hresult passed SUCCEEDED, jump to the given label.
//
// Parameters:
//
//      _hresult - [in] Value to check
//      label - [in] label to jump if condition is met
//
#define IF_SUCCEEDED_JUMP(_hresult, label)                      \
    if (SUCCEEDED(_hresult))                                    \
    {                                                           \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// If the condition evaluates to TRUE, perform the given statement
// then jump to the given label.
//
// Parameters:
//
//      condition - [in] Code that fits in if statement
//      action - [in] action to perform in body of if statement
//      label - [in] label to jump if condition is met
//
#define IF_TRUE_ACTION_JUMP(condition, action, label)           \
    if (condition)                                              \
    {                                                           \
        action;                                                 \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// If the hresult FAILED, perform the given statement then jump to
// the given label.
//
// Parameters:
//
//      _hresult - [in] Value to check
//      action - [in] action to perform in body of if statement
//      label - [in] label to jump if condition is met
//
#define IF_FAILED_ACTION_JUMP(_hresult, action, label)          \
    if (FAILED(_hresult))                                       \
    {                                                           \
        action;                                                 \
        goto label;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// Closes a handle and assigns NULL.
//
// Parameters:
//
//      h - [in] handle to close
//
#define SAFE_CLOSE_HANDLE(h)                                    \
    if (NULL != h)                                              \
    {                                                           \
        CloseHandle(h);                                         \
        h = NULL;                                               \
    }

//-------------------------------------------------------------------------
// Description:
//
// Addref an interface pointer
//
// Parameters:
//
//      p - [in] object to addref
//
#define SAFE_ADDREF(p)                                          \
    if (NULL != p)                                              \
    {                                                           \
        (p)->AddRef();;                                         \
    }

//-------------------------------------------------------------------------
// Description:
//
// Releases an interface pointer and assigns NULL.
//
// Parameters:
//
//      p - [in] object to release
//
#define SAFE_RELEASE(p)                                         \
    if (NULL != p)                                              \
    {                                                           \
        (p)->Release();                                         \
        (p) = NULL;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// Deletes a pointer and assigns NULL. Do not check for NULL because
// the default delete operator checks for it.
//
// Parameters:
//
//      p - [in] object to delete
//
#define SAFE_DELETE(p)                                          \
    delete p;                                                   \
    p = NULL;

//-------------------------------------------------------------------------
// Description:
//
// Deletes an array pointer and assigns NULL. Do not check for NULL because
// the default delete operator checks for it.
//
// Parameters:
//
//      p - [in] Array to delete
//
#define SAFE_DELETE_ARRAY(p)                                    \
    delete [] p;                                                \
    p = NULL;

//-------------------------------------------------------------------------
// Description:
//
// Frees a block of memory allocated by CoTaskMemAlloc and assigns NULL to
// the pointer
//
// Parameters:
//
//      p - [in] Pointer to memory to free
//
#define SAFE_COTASKMEMFREE(p)                                   \
    if (NULL != p)                                              \
    {                                                           \
        CoTaskMemFree(p);                                       \
        (p) = NULL;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
// Frees a DLL loaded with LoadLibrary and assigns NULL to the handle
//
// Parameters:
//
//      h - [in] Handle to DLL to free
//
#define SAFE_FREELIBRARY(h)                                     \
    if (NULL != h)                                              \
    {                                                           \
        FreeLibrary(h);                                         \
        (h) = NULL;                                             \
    }

//-------------------------------------------------------------------------
// Description:
//
//  Used to validate a read pointer
//
// Parameters:
//
//     p - [in] read pointer.
//     s - [in] size of memory in bytes pointed to by p.
//
#define IS_VALID_READ_POINTER(p, s)     ((NULL != p) || (0 == s))

//-------------------------------------------------------------------------
// Description:
//
//  Used to validate a write pointer
//
// Parameters:
//
//     p - [in] write pointer.
//     s - [in] size of memory in bytes pointed to by p.
//
#define IS_VALID_WRITE_POINTER(p, s)    ((NULL != p) || (0 == s))

//-------------------------------------------------------------------------
// Description:
//
//  Used to validate a read pointer of a particular type
//
// Parameters:
//
//     p - [in] typed read pointer
//
#define IS_VALID_TYPED_READ_POINTER(p)  IS_VALID_READ_POINTER((p), sizeof *(p))

//-------------------------------------------------------------------------
// Description:
//
//  Used to validate a write pointer of a particular type
//
// Parameters:
//
//     p - [in] typed write pointer
//
#define IS_VALID_TYPED_WRITE_POINTER(p) IS_VALID_WRITE_POINTER((p), sizeof *(p))

// ---------------------------------------------------------------------------
// Macros that wrap windows messages.  Similar to those in windowsX.h and
// commctrl.h
//
#if !defined Static_SetIcon
#define Static_SetIcon(hwnd, hi) \
            (BOOL)SNDMSG((hwnd), STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)(hi))
#endif

#define TrackBar_SetTickFrequency(hwnd, f) \
            (BOOL)SNDMSG((hwnd), TBM_SETTICFREQ, (WPARAM)(f), 0)

#define TrackBar_SetBuddy(hwnd, f, hbud) \
            (HWND)SNDMSG((hwnd), TBM_SETBUDDY, (WPARAM)(f), (LPARAM)hbud)

#define TrackBar_GetPos(hwnd) \
            (int)SNDMSG((hwnd), TBM_GETPOS, 0, 0)

#define TrackBar_SetPos(hwnd, pos) \
            SNDMSG((hwnd), TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pos)

#define TrackBar_SetRange(hwnd, min, max) \
            SNDMSG((hwnd), TBM_SETRANGE , (WPARAM)TRUE, (LPARAM) MAKELONG(min, max))

#define TrackBar_SetThumbLength(hwnd, l) \
            SNDMSG((hwnd), TBM_SETTHUMBLENGTH, (WPARAM)l, 0);

#define TrackBar_SetPageSize(hwnd, n) \
            SNDMSG((hwnd), TBM_SETPAGESIZE, 0, (LPARAM)n)

#define Window_GetFont(hwnd) \
            (HFONT)SNDMSG((hwnd), WM_GETFONT, 0, 0)

#define Window_SetFont(hwnd, font) \
            SNDMSG((hwnd), WM_SETFONT, (WPARAM)font, FALSE)


// ----------------------------------------------------------------------
// A struct for holding a rect in easier terms than a RECT struct
//
struct SRECT    
{
    int x, y, w, h;
    SRECT()
    {
        x = y = w = h = 0;
    }
    SRECT(int X, int Y, int W, int H)
    {
        x = X; y = Y; w = W; h = H;
    }
    SRECT(RECT* prc)
    {
        x = prc->left;
        y = prc->top;
        w = prc->right - prc->left;
        h = prc->bottom - prc->top;
    }
};

#define HNS_PER_SECOND (10ull * 1000ull * 1000ull)