|
16 | 16 | #define THROW_IF_FAILED(exp) { hr = exp; if (FAILED(hr)) { ::printf("FAILURE: 0x%08x = %s\n", hr, #exp); throw hr; } }
|
17 | 17 | #define THROW_FAIL_IF_FALSE(exp) { if (!(exp)) { ::printf("FALSE: %s\n", #exp); throw E_FAIL; } }
|
18 | 18 |
|
19 |
| -#include <map> |
20 | 19 | #include <string>
|
21 | 20 |
|
22 | 21 | namespace
|
23 | 22 | {
|
| 23 | + class DispIDToStringMap |
| 24 | + { |
| 25 | + struct Pair |
| 26 | + { |
| 27 | + DISPID id; |
| 28 | + WCHAR value[128]; |
| 29 | + }; |
| 30 | + Pair _pairs[8]; |
| 31 | + const Pair* _end; |
| 32 | + |
| 33 | + public: |
| 34 | + DispIDToStringMap() |
| 35 | + : _pairs{} |
| 36 | + , _end{ _pairs + ARRAYSIZE(_pairs) } |
| 37 | + { |
| 38 | + for (auto curr = _pairs; curr != _end; ++curr) |
| 39 | + curr->id = DISPID_UNKNOWN; |
| 40 | + } |
| 41 | + |
| 42 | + const WCHAR* Find(_In_ DISPID id) |
| 43 | + { |
| 44 | + for (auto curr = _pairs; curr != _end; ++curr) |
| 45 | + { |
| 46 | + if (curr->id == id) |
| 47 | + return curr->value; |
| 48 | + } |
| 49 | + |
| 50 | + return nullptr; |
| 51 | + } |
| 52 | + |
| 53 | + void Insert(_In_ DISPID id, _In_z_ const WCHAR* value) |
| 54 | + { |
| 55 | + if (id == DISPID_UNKNOWN) |
| 56 | + throw E_UNEXPECTED; |
| 57 | + |
| 58 | + for (auto curr = _pairs; curr != _end; ++curr) |
| 59 | + { |
| 60 | + if (curr->id == DISPID_UNKNOWN) |
| 61 | + { |
| 62 | + curr->id = id; |
| 63 | + size_t len = ::wcslen(value) + 1; // Include null |
| 64 | + ::memcpy(curr->value, value, len * sizeof(value[0])); |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + throw E_UNEXPECTED; |
| 70 | + } |
| 71 | + |
| 72 | + void Erase(_In_ DISPID id) |
| 73 | + { |
| 74 | + for (auto curr = _pairs; curr != _end; ++curr) |
| 75 | + { |
| 76 | + if (curr->id == id) |
| 77 | + { |
| 78 | + curr->id = DISPID_UNKNOWN; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + }; |
| 84 | + |
24 | 85 | class EventSink : public UnknownImpl, public TestingEvents
|
25 | 86 | {
|
26 |
| - std::map<DISPID, std::wstring> _firedEvents; |
| 87 | + DispIDToStringMap _firedEvents; |
27 | 88 |
|
28 | 89 | public:
|
29 | 90 | void ResetFiredState(_In_ DISPID id)
|
30 | 91 | {
|
31 |
| - _firedEvents.erase(id); |
| 92 | + _firedEvents.Erase(id); |
32 | 93 | }
|
33 | 94 |
|
34 | 95 | bool DidFire(_In_ DISPID id, _Out_ std::wstring& message)
|
35 | 96 | {
|
36 |
| - auto iter = _firedEvents.find(id); |
37 |
| - if (iter == std::end(_firedEvents)) |
| 97 | + auto value = _firedEvents.Find(id); |
| 98 | + if (value == nullptr) |
38 | 99 | return false;
|
39 | 100 |
|
40 |
| - message = iter->second; |
| 101 | + message = value; |
41 | 102 | return true;
|
42 | 103 | }
|
43 | 104 |
|
@@ -112,7 +173,7 @@ namespace
|
112 | 173 | if (msgMaybe->vt != VT_BSTR)
|
113 | 174 | return E_INVALIDARG;
|
114 | 175 |
|
115 |
| - _firedEvents.insert({ dispId, msgMaybe->bstrVal }); |
| 176 | + _firedEvents.Insert(dispId, msgMaybe->bstrVal); |
116 | 177 | return S_OK;
|
117 | 178 | }
|
118 | 179 |
|
|
0 commit comments