-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComUtils.h
146 lines (124 loc) · 2.93 KB
/
ComUtils.h
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
#pragma once
struct SourceLocation
{
SourceLocation(int line, const TCHAR* file, const TCHAR* function, const TCHAR* funcsig)
: line(line)
, file(file)
, function(function)
, funcsig(funcsig)
{
}
int line;
const TCHAR* file;
const TCHAR* function;
const TCHAR* funcsig;
};
#define SRC_LOC SourceLocation(__LINE__, _T(__FILE__), _T(__FUNCTION__), _T(__FUNCSIG__))
inline bool Validate(HRESULT hr, const LPCTSTR msg, const SourceLocation& src_loc)
{
if (FAILED(hr))
{
_ftprintf(stderr, _T("FAILED [%s(%d)] %#10.8x: %s\n"), src_loc.file, src_loc.line, hr, msg);
return false;
}
else
return true;
}
inline bool ValidateIgnore(HRESULT hr, HRESULT ignore, const LPCTSTR msg, const SourceLocation& src_loc)
{
return hr == ignore ? true : Validate(hr, msg, src_loc);
}
#define CHECK(x, m, r) if (!Validate(x, m, SRC_LOC)) return r;
#define CHECK_IGNORE(x, i, m, r) if (!ValidateIgnore(x, i, m, SRC_LOC)) return r;
template<class T>
struct ObjectArrayIndex
{
UINT i;
IObjectArray* pObjectArray;
ObjectArrayIndex& operator++()
{
++i;
return *this;
}
CComPtr<T> operator*()
{
CComPtr<T> p;
if (FAILED(pObjectArray->GetAt(i, IID_PPV_ARGS(&p))))
return nullptr;
return p;
}
};
template<class T>
bool operator!=(const ObjectArrayIndex<T>& l, const ObjectArrayIndex<T>& r)
{
assert(l.pObjectArray == r.pObjectArray);
return l.i != r.i;
}
template<class T>
class ObjectArrayRange
{
private:
IObjectArray* pObjectArray;
public:
ObjectArrayRange(IObjectArray* pObjectArray)
: pObjectArray(pObjectArray)
{
}
ObjectArrayIndex<T> begin() const
{
return { 0, pObjectArray };
}
ObjectArrayIndex<T> end() const
{
UINT count;
if (FAILED(pObjectArray->GetCount(&count)))
count = 0;
return { count, pObjectArray };
}
};
template<class T>
struct ObjectArrayIndexRev
{
UINT i;
IObjectArray* pObjectArray;
ObjectArrayIndexRev& operator++()
{
--i;
return *this;
}
CComPtr<T> operator*()
{
CComPtr<T> p;
if (FAILED(pObjectArray->GetAt(i - 1, IID_PPV_ARGS(&p))))
return nullptr;
return p;
}
};
template<class T>
bool operator!=(const ObjectArrayIndexRev<T>& l, const ObjectArrayIndexRev<T>& r)
{
assert(l.pObjectArray == r.pObjectArray);
return l.i != r.i;
}
template<class T>
class ObjectArrayRangeRev
{
private:
IObjectArray* pObjectArray;
public:
ObjectArrayRangeRev(IObjectArray* pObjectArray)
: pObjectArray(pObjectArray)
{
}
ObjectArrayIndexRev<T> begin() const
{
UINT count;
if (FAILED(pObjectArray->GetCount(&count)))
count = 0;
return { count, pObjectArray };
}
ObjectArrayIndexRev<T> end() const
{
return { 0, pObjectArray };
}
};